Java Tutorial 45 (GUI) - Moving Object with Keyboard Inputs (KeyListener)

preview_player
Показать описание
In this tutorial, you will learn how to create a user controlled object. KeyListener is a must in games.
Рекомендации по теме
Комментарии
Автор

Very good tutorial! you can also use getHeight - (size of your rectangle) and getWidth - (size of your rectangle) for the borders collision, that way you can make the screen bigger or smaller and it will still collide.

Alepizzaa
Автор

Hey dude, can you provide a link so that everyone has access to your code.
It would be much appreciated

deanmurray
Автор

He's using Eclipse. Eclipse is currently the most popular program used in Java/C++ casual/semi-professional programming. If you are new to programming, I recommend starting with a simpler interface, such as BlueJ.

danielleholzberger
Автор

Very useful! I am doing my java final game project, and this helps a lot.

larryli
Автор

I was literately banging my head on the keyboard and searching character by character to figure out what I was doing wrong. I am just happy that I have started to read the comments on these videos. It is amazing how the position of one line can mess up the entire code. Especially when it was working in the video but not for me. I was like, "Well, it's working for him. I must have done something else wrong somewhere else." ThePanerz saved my sanity and my code. So Thanks Again!

reginahilt
Автор

Holy crap! Thank you so much. I never would have figured that out by myself. I was wondering what the heck happened and trying to fix it for at least an hour. You are a genius!! Thank YOU!!!!

reginahilt
Автор

i got everything correct :) here is my code just copy it!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Gamara extends JPanel implements ActionListener, KeyListener{
Timer tm=new Timer(2, this);

int x=0, y=0, velX=0, velY=0;
public Gamara ()
{
tm.start(); //starts timer
addKeyListener(this); //this referring to KeyListener
setFocusable(true); //enable KeyListener
//shift or tab is not use so F
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);

g.setColor(Color.red);
g.fillRect(x, y, 50, 30);
}
public void actionPerformed(ActionEvent e)
{

if(x < 0)
{
velX=0;
x = 0;
}

if(x > 500)
{
velX=0;
x = 500;
}

if(y < 0)
{
velY=0;
y = 0;
}

if(y > 300)
{
velY=0;
y = 300;
}
x=x+velX;
y=y+velY;
repaint();


}
public void keyPressed(KeyEvent e)
{
int c = e.getKeyCode();
if (c == KeyEvent.VK_LEFT) // VK_Left is left arrow
{
velX = -3;
velY = 0;
}
if (c == KeyEvent.VK_UP) // VK_UP is up arrow
{
velX = 0;
velY = -3; // means up
}
if (c == KeyEvent.VK_RIGHT)
{
velX = 3;
velY = 0;
}

if (c == KeyEvent.VK_DOWN)
{
velX = 0;
velY = 3;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
velX=0;
velY=0;



}
/*public static void main(String [] args)
{

JFrame f=new JFrame("vivek");

Gamara a =new Gamara ();
f.add(a);
f.setSize(600, 400);
f.setVisible(true);
}
}*/

public static void main(String[] args)
{
Gamara g = new Gamara();
JFrame jf = new JFrame();
jf.setTitle("Tutorial");
jf.setSize(600, 400);
jf.setVisible(true);

Gamara g1 = new Gamara();
jf.add(g1);

}




}

viveksaikode
Автор

Great Tutorial. Thanks, ThePanerz for your comment- move the jf.setVisible(true); statement to the end after jf.add(t); statement in the main method. I was stuck with it and couldn't figure out why the object won't move reliably.  Thanks for the Tutorial. 

rockinghorse
Автор

You are like my hero by the way you took time to make these videos for people

jaybartgis
Автор

These tutorials are awesome. Thank you a lot

alierencelik
Автор

hey man great tutorials have served me so I hope you continue

estiben
Автор

wow, been stuck for a while and your advice is exactly what I needed

geekycanucks
Автор

you save me! I've been about 2 days wondering what da hell what was wrong! and it's just the "implements KeyListener" haha

openmind
Автор

Awesome! Thanks for the tut helped me a lot!

andreastijerina
Автор

if i keep pressed the button left or any, the rectangle increasing the speed,

how can i set the speed to be allays the same?

StefanBanu
Автор

awesome i love to learn from you friend

fishandfisherman
Автор

Thanks for this tutorial. This really helped a lot. Like.

cravingbulletsphan
Автор

it is very useful, thank you so much. :)))

Автор

Thanks a bunch sir, this tutorial really helped me out

AngryChihuahua
Автор

can you list full source I am missing something

elliehubb