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.
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.
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!
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!!!!
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.
I started to do a lot of projects with key listener and I often encounter the problem that sometimes the keylistener fails to work and the program runs but is unresponsive until I run it over and over until it finally works, any ideas on how to fix it? I'm in an AP Computer science class and one of my classmates suggested using an @override; statement, I tried it and maybe I did something wrong but I had the same problem. Would you be able to help me out if I sent you the program as source code or as a compiled .jav file? thank you!
Ok so I copied the code EXACTLY(except for some variables) and my square/rectangle wont move, I dont know if I copied something wrong or what but my error is on my class name (fourth_2) the error is: "The type Fourth_2 must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)" somebody help me! please :)?
Awesome tutorial! but why is the moving so laggy, each time I try to go another direction it stops for 2 seconds then go the direction Iam asking it to go...anyway to make movement smoother?
Okay, I saw your animation video. I'm (trying) to make a mario like game. So I did everything you told me to do. But I made a platform for the character to walk on and disallowed up/down movement. I even added a little obsticle with collision detection,. Now, here is where it stumps me. I added a jump feature with the space bar. Once I pressed it though, it goes infinatly upwards. I tried adding collision detection at 280 pxls like this if(y > 280)velY = 0;y = 200; It won't work! Help!
btw great video. the keylistener work BEFORE clicking on the contents of a droplist. afterward the keys ONLY applies to the droplist list. is there a way to fix it? is there a way to make the keylistener absolute?
Hey thank you for this video.., but i have a problem.. can you help me i'm new in java.. i did all what you said on the video but when i run the program it didn't move the object.. i don't know what is wrong.. can someone help me
hye, i wanna ask on hoe to make a pendulum clock move and stop by using any key from the keyboard. if u could help me build a coding for this question i will be grateful hehe thank u
hy guys can someone help me with this line of code using blue j - public class Tutorial extends JPanel implements ActionListener,KeyListener. I struggle to call Tutorial class it says it is not an abstract method and does not Override abstract method. #please_help
Ok abdo 😉 , basically in your main void : public static void main ( Strings [] args ) { jframe j = new jframe(); // create your jframe here , size , visibility ...etc then jframe.addKeyListener(this);}
so i got it working but for some reason, when i hold down the buttons to move and then press a different button, it would freeze. for example, if i hold the down button and then quickly press the right button, it would freeze
my code for keyListener is: public void keyPressed(KeyEvent e) { int c = e.getKeyCode(); if(c == KeyEvent.VK_DOWN) { steg_y = 1; steg_x = 0; } etc... my program paints a ball but it dont happens anything when i press a arrow key. I have tested to change the velX to 1 and then my ball is moving. Can anyone help me please.
In my case The keyPressed Method is never called. I looked the video 3 times now and I don´t know what I did wrong D; Can anyone say me how that is possible?
I had a problem with the first public class being underlined and I just added /** * */ private static final long serialVersionUID = -3456806747373786506L; and it decided to work
Never mind, in the methods keypressed and keyrealeased, i put a capital K insteaed of a lower case k, for all who see this comment remember... JAVA IS CASE SENSITIVE!!!
Hello and Thank you for this tutorial actually i have problem with this code ,cause show me this error message: Test is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener.
i did the exact same thing but it won't work sucks :s the error is definetly not in your video; if you have some time could you check my code for errors? :) only if u have time? thanks for the vid!
i think the velX and velY is the controller of the speed :) , try to the the formula . Hope It Helps . Sorry im not really sure , because i didnt watch the video :) . hahaha Poor Me LOL
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.
Hey dude, can you provide a link so that everyone has access to your code.
It would be much appreciated
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.
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!
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!!!!
Very useful! I am doing my java final game project, and this helps a lot.
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?
wow , been stuck for a while and your advice is exactly what I needed
These tutorials are awesome. Thank you a lot
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 java.awt.event.ActionListener;
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
setFocusTraversalKeysEnabled(false); //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");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Gamara g1 = new Gamara();
jf.add(g1);
}
}
pass me on my email
iram1103@gmail.com
Thank you very much! You saved me a lot of time and grief. 🙂
You are like my hero by the way you took time to make these videos for people
hey man great tutorials have served me so I hope you continue
Best Channel Ever
you save me! I've been about 2 days wondering what da hell what was wrong! and it's just the "implements KeyListener" haha
Such a good tutorial man! I subbed!!!
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.
I started to do a lot of projects with key listener and I often encounter the problem that sometimes the keylistener fails to work and the program runs but is unresponsive until I run it over and over until it finally works, any ideas on how to fix it? I'm in an AP Computer science class and one of my classmates suggested using an @override; statement, I tried it and maybe I did something wrong but I had the same problem. Would you be able to help me out if I sent you the program as source code or as a compiled .jav file? thank you!
Ok so I copied the code EXACTLY(except for some variables) and my square/rectangle wont move, I dont know if I copied something wrong or what but my error is on my class name (fourth_2) the error is: "The type Fourth_2 must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)" somebody help me! please :)?
what do i have to change when i whant the object to move 30 fields instandly instead of the timer?
I got the rectangle to move but it doesn’t stop the rectangle when you release a key.. i have that part of the code and all the brackets are the same
Awesome tutorial! but why is the moving so laggy, each time I try to go another direction it stops for 2 seconds then go the direction Iam asking it to go...anyway to make movement smoother?
Okay, I saw your animation video. I'm (trying) to make a mario like game. So I did everything you told me to do. But I made a platform for the character to walk on and disallowed up/down movement. I even added a little obsticle with collision detection,. Now, here is where it stumps me. I added a jump feature with the space bar. Once I pressed it though, it goes infinatly upwards. I tried adding collision detection at 280 pxls like this if(y > 280)velY = 0;y = 200;
It won't work! Help!
btw great video. the keylistener work BEFORE clicking on the contents of a droplist. afterward the keys ONLY applies to the droplist list. is there a way to fix it? is there a way to make the keylistener absolute?
Awesome! Thanks for the tut helped me a lot!
can you list full source I am missing something
@BEBigT i found my error: with the declaration of int x , y , velx, vely = 0; i replaced it with int x = (int) = 0, y = (int) 0, etc...
can u explain the repositioning thing again (when applying restrictions)
Why do you make the bounds for x on the right side 530? Shouldn't it be 550? Why is it 530?
awesome i love to learn from you friend
i know this is much later but can you pleeease put the code in the description?
Hey thank you for this video.., but i have a problem.. can you help me i'm new in java.. i did all what you said on the video but when i run the program it didn't move the object..
i don't know what is wrong.. can someone help me
If u want I can make a video on how
I'm also wondering for this. I'm never delete the "footprints" but it does that automatically
You don’t need to create a Rectangle object??
I just used auto generate and @override from the unimplemented method
But I have a problem any button I press on the key board the rectangle gos down only.
whenever i tried to follow this everything typed got red marked and importing libraries did nothing
Thanks a bunch sir, this tutorial really helped me out
Can you make a video on collision detection?
that is so helpful :) but my game glitches and each time you press the key it gets a further distance apart than it should ???
hye, i wanna ask on hoe to make a pendulum clock move and stop by using any key from the keyboard. if u could help me build a coding for this question i will be grateful hehe thank u
Thanks for this tutorial. This really helped a lot. Like.
hy guys can someone help me with this line of code using blue j - public class Tutorial extends JPanel implements ActionListener,KeyListener. I struggle to call Tutorial class it says it is not an abstract method and does not Override abstract method. #please_help
Thanks for the tutorial. Does anyone know how to keep the rectangle from leaving "footsteps" behind itself?
You probably know now but you do repaint();
repaint();
Java Tutorials umm guy, look above your comment
Just use a timer
Will this work with Android Studio?
Nothing happens in rectangle....why??/how tp fix it?
Same thing to me , When I debug it works , when I run it normally it doesn't work
+Lednew Hafalla Type ( name of your jframe.addKeyListener(Name of your Main class Or as he Tutorial); )
Yeah Don't worry I got it fixed from long time 👍
Ok abdo 😉 , basically in your main void : public static void main ( Strings [] args ) {
jframe j = new jframe();
// create your jframe here , size , visibility ...etc then
jframe.addKeyListener(this);}
so i got it working but for some reason, when i hold down the buttons to move and then press a different button, it would freeze. for example, if i hold the down button and then quickly press the right button, it would freeze
yup i got that too but play with the codes and change it a little bit.. and it will work fine
Hans Fajardo funny thing is, it only freezes with the awsd keys and not the arrow keys
When I try to do it a empty JFrame Pops up, why?
maybe you haven't set the bounds of the rectangle right
i have the same issue :(
we have to take jframe and then take rectangle .where we take rectangle it is work on netbeans
why we need to setFocusable is true???
Got stuck in error .i checked many times it keep showing that your class is not abstract.anyone please?
I did everything right and when I let go of a key it keeps going Please help thanks! 2016
my code for keyListener is:
public void keyPressed(KeyEvent e)
{
int c = e.getKeyCode();
if(c == KeyEvent.VK_DOWN)
{
steg_y = 1;
steg_x = 0;
}
etc...
my program paints a ball but it dont happens anything when i press a arrow key.
I have tested to change the velX to 1 and then my ball is moving. Can anyone help me please.
when i goes down or right, the rectangle goes outside the bound. Can anyone help me how to fix it?
I did it. bt this does not change anything
how can i add another object
My rectangle disappears after I move it :(
+John Gizdich NOTHING MOVES , HOW TO FIX IT
it does move just follow this code:import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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
setFocusTraversalKeysEnabled(false); //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");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Gamara g1 = new Gamara();
jf.add(g1);
}
}
it is very useful, thank you so much. :)))
In my case The keyPressed Method is never called.
I looked the video 3 times now and I don´t know what I did wrong D;
Can anyone say me how that is possible?
+Knödelcrafter I found a solution. I added jpanel.addkeylistener(this)
That's true you gave me the clue thanks, but I had to add it to the JFrame ---> jf.addKeyListener(t); thats how it worked to me
I had a problem with the first public class being underlined and I just added
/**
*
*/
private static final long serialVersionUID = -3456806747373786506L;
and it decided to work
what program are you using?
i'ts called java eclipse
Thank you maaan
Good tut
Can anyone give me the code for this. I have try to copy but it don't work.
please
Thanks for the video
in the method paintComponent(Graphics g) you forgot to type:
super.paintComponent(g)
comeon man give us the code
nice one
Never mind, in the methods keypressed and keyrealeased, i put a capital K insteaed of a lower case k, for all who see this comment remember... JAVA IS CASE SENSITIVE!!!
really thanks for you
Hello and Thank you for this tutorial actually i have problem with this code ,cause show me this error message:
Test is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener.
Arnold Bermeli you need to implement all of the methods from the interface you implemented
damm i waste like 3h to figure out why my object didnt move ... I had to press tab key each time I lunch the app in other to move the object
You really should be using multiple classes. Also, x = x + velx is just lazy, use x += velx.
Thank you!
i did the exact same thing but it won't work sucks :s the error is definetly not in your video; if you have some time could you check my code for errors? :) only if u have time?
thanks for the vid!
thank you ...
oh it works
thanks man!:)
Sorry i have little time to watch the full video. How do u make it move but without making "footprints" of itself in its path behind?
i think the velX and velY is the controller of the speed :) ,
try to the the formula . Hope It Helps .
Sorry im not really sure , because i didnt watch the video :) .
hahaha Poor Me LOL
thnx man
Cool.
the Timer code now doesn't work and so as some of them, I think Java did some shit
first public class is error
please roy!!
reply!
Hello
Guys this is my code.... sometimes it works but sometimes it don't...WHY?!?!?!? HELP PLEASEEEEE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Main extends JPanel implements KeyListener, ActionListener{
Timer timer = new Timer(5, this);
int y = 310, vely = 0;
public static void main(String[] arge) {
Main main = new Main();
JFrame window = new JFrame("Kill It");
window.setBounds(420, 150, 1080, 720);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
window.add(main);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(20, y, 50, 50);
}
public Main() {
timer.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(y < 0)
{
vely=0;
y = 0;
}
if(y > 640)
{
vely=0;
y = 640;
}
y += vely;
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_W){
vely = -1;
}
if (code == KeyEvent.VK_S) {
vely = 1;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
vely = 0;
}
@Override
public void keyTyped(KeyEvent arg0) {
}
}
public void actionPerformed(ActionEvent arg0) {
if(y < 0)
{
thats the prob
what?? but why what is the prob..pls help me... Thanks in advance
@OneDirection i wan some bigbang
nevermind, I know how it works now. Im such a dumbass!
10 "wanna be programmers" disliked lol
lol?
When I press the arrow keys ,the square doesn't move .How do I fix that