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

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ธ.ค. 2024

ความคิดเห็น • 125

  • @danielleholzberger4177
    @danielleholzberger4177 11 ปีที่แล้ว +1

    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.

  • @deanmurray7626
    @deanmurray7626 9 ปีที่แล้ว +28

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

  • @Alepizzaa
    @Alepizzaa 10 ปีที่แล้ว +9

    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.

  • @reginahilt
    @reginahilt 11 ปีที่แล้ว

    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
    @reginahilt 11 ปีที่แล้ว

    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!!!!

  • @larryli2752
    @larryli2752 5 ปีที่แล้ว +2

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

  • @StefanBanu
    @StefanBanu 12 ปีที่แล้ว +1

    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?

  • @geekycanucks
    @geekycanucks 11 ปีที่แล้ว

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

  • @alierencelik2188
    @alierencelik2188 8 ปีที่แล้ว +1

    These tutorials are awesome. Thank you a lot

  • @viveksaikode8039
    @viveksaikode8039 7 ปีที่แล้ว +7

    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);
    }
    }

    • @iram1103
      @iram1103 6 ปีที่แล้ว

      pass me on my email
      iram1103@gmail.com

    • @thomasjohnson7563
      @thomasjohnson7563 2 ปีที่แล้ว

      Thank you very much! You saved me a lot of time and grief. 🙂

  • @jaybartgis5148
    @jaybartgis5148 7 ปีที่แล้ว +1

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

  • @estiben000
    @estiben000 13 ปีที่แล้ว

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

  • @annaobretencheva7285
    @annaobretencheva7285 4 ปีที่แล้ว

    Best Channel Ever

  • @openmind706
    @openmind706 12 ปีที่แล้ว +1

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

  • @kylemanley915
    @kylemanley915 8 ปีที่แล้ว +2

    Such a good tutorial man! I subbed!!!

  • @rockinghorse225
    @rockinghorse225 11 ปีที่แล้ว

    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.

  • @kylemanley915
    @kylemanley915 8 ปีที่แล้ว +1

    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!

  • @Miner493R
    @Miner493R 12 ปีที่แล้ว +1

    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 :)?

  • @billyremake8090
    @billyremake8090 8 ปีที่แล้ว

    what do i have to change when i whant the object to move 30 fields instandly instead of the timer?

  • @dylanz1133
    @dylanz1133 5 ปีที่แล้ว

    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

  • @PirhanaGuy
    @PirhanaGuy 10 ปีที่แล้ว +1

    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?

  • @xXNiftyLlamaXx
    @xXNiftyLlamaXx 12 ปีที่แล้ว

    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!

  • @25zsams
    @25zsams 8 ปีที่แล้ว

    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?

  • @andreastijerina7631
    @andreastijerina7631 7 ปีที่แล้ว

    Awesome! Thanks for the tut helped me a lot!

  • @elliehubb
    @elliehubb 11 ปีที่แล้ว +2

    can you list full source I am missing something

  • @thomasvanroy
    @thomasvanroy 13 ปีที่แล้ว

    @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...

  • @mcbalasubramaniam4690
    @mcbalasubramaniam4690 10 ปีที่แล้ว

    can u explain the repositioning thing again (when applying restrictions)

  • @tinky946
    @tinky946 11 ปีที่แล้ว

    Why do you make the bounds for x on the right side 530? Shouldn't it be 550? Why is it 530?

  • @fishandfisherman
    @fishandfisherman 12 ปีที่แล้ว

    awesome i love to learn from you friend

  • @cocoabeangamer690
    @cocoabeangamer690 3 ปีที่แล้ว

    i know this is much later but can you pleeease put the code in the description?

  • @jordangabriel8031
    @jordangabriel8031 8 ปีที่แล้ว +1

    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

    • @fuzedcable5612
      @fuzedcable5612 4 ปีที่แล้ว

      If u want I can make a video on how

  • @Istlemin
    @Istlemin 12 ปีที่แล้ว

    I'm also wondering for this. I'm never delete the "footprints" but it does that automatically

  • @AliFX
    @AliFX 5 ปีที่แล้ว

    You don’t need to create a Rectangle object??

  • @alvinzheng9401
    @alvinzheng9401 8 ปีที่แล้ว +1

    I just used auto generate and @override from the unimplemented method

  • @averyaldridge5336
    @averyaldridge5336 11 ปีที่แล้ว

    But I have a problem any button I press on the key board the rectangle gos down only.

  • @idkmynameattall
    @idkmynameattall 8 ปีที่แล้ว

    whenever i tried to follow this everything typed got red marked and importing libraries did nothing

  • @AngryChihuahua9
    @AngryChihuahua9 11 ปีที่แล้ว

    Thanks a bunch sir, this tutorial really helped me out

  • @brandonpassetti6145
    @brandonpassetti6145 6 ปีที่แล้ว

    Can you make a video on collision detection?

  • @mattrowlands5751
    @mattrowlands5751 11 ปีที่แล้ว

    that is so helpful :) but my game glitches and each time you press the key it gets a further distance apart than it should ???

  • @ezzaefah880
    @ezzaefah880 7 ปีที่แล้ว

    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

  • @cravingbulletsphan1333
    @cravingbulletsphan1333 12 ปีที่แล้ว

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

  • @alicenomshado3387
    @alicenomshado3387 9 ปีที่แล้ว

    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

  • @zoeymally6938
    @zoeymally6938 11 ปีที่แล้ว

    Thanks for the tutorial. Does anyone know how to keep the rectangle from leaving "footsteps" behind itself?

    • @TheApplesauce120
      @TheApplesauce120 10 ปีที่แล้ว

      You probably know now but you do repaint();

    • @jpd75programming89
      @jpd75programming89 10 ปีที่แล้ว +1

      repaint();

    • @TheApplesauce120
      @TheApplesauce120 10 ปีที่แล้ว +1

      Java Tutorials umm guy, look above your comment

    • @fontanot
      @fontanot 4 ปีที่แล้ว

      Just use a timer

  • @Shahshshshshshsh
    @Shahshshshshshsh 8 ปีที่แล้ว

    Will this work with Android Studio?

  • @lednewhafalla9597
    @lednewhafalla9597 9 ปีที่แล้ว +2

    Nothing happens in rectangle....why??/how tp fix it?

    • @double_hh
      @double_hh 9 ปีที่แล้ว

      Same thing to me , When I debug it works , when I run it normally it doesn't work

    • @double_hh
      @double_hh 9 ปีที่แล้ว

      +Lednew Hafalla Type ( name of your jframe.addKeyListener(Name of your Main class Or as he Tutorial); )

    • @double_hh
      @double_hh 8 ปีที่แล้ว

      Yeah Don't worry I got it fixed from long time 👍

    • @double_hh
      @double_hh 8 ปีที่แล้ว

      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);}

  • @supergamerboss123
    @supergamerboss123 6 ปีที่แล้ว

    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

    • @fajardohansalexander3949
      @fajardohansalexander3949 6 ปีที่แล้ว

      yup i got that too but play with the codes and change it a little bit.. and it will work fine

    • @supergamerboss123
      @supergamerboss123 6 ปีที่แล้ว

      Hans Fajardo funny thing is, it only freezes with the awsd keys and not the arrow keys

  • @FallingScales145
    @FallingScales145 11 ปีที่แล้ว +1

    When I try to do it a empty JFrame Pops up, why?

    • @TunaAlert
      @TunaAlert 11 ปีที่แล้ว

      maybe you haven't set the bounds of the rectangle right

    • @kakoru0505
      @kakoru0505 10 ปีที่แล้ว

      i have the same issue :(

  • @rajheer7529
    @rajheer7529 7 ปีที่แล้ว

    we have to take jframe and then take rectangle .where we take rectangle it is work on netbeans

  • @ahuynh359
    @ahuynh359 7 ปีที่แล้ว

    why we need to setFocusable is true???

  • @hamzasiddiqui9610
    @hamzasiddiqui9610 6 ปีที่แล้ว

    Got stuck in error .i checked many times it keep showing that your class is not abstract.anyone please?

  • @creeperops6063
    @creeperops6063 8 ปีที่แล้ว

    I did everything right and when I let go of a key it keeps going Please help thanks! 2016

  • @Istlemin
    @Istlemin 12 ปีที่แล้ว

    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.

  • @mr.509
    @mr.509 8 ปีที่แล้ว

    when i goes down or right, the rectangle goes outside the bound. Can anyone help me how to fix it?

    • @mr.509
      @mr.509 8 ปีที่แล้ว

      I did it. bt this does not change anything

  • @orikosary2826
    @orikosary2826 7 ปีที่แล้ว

    how can i add another object

  • @Stat1onary
    @Stat1onary 4 ปีที่แล้ว

    My rectangle disappears after I move it :(

  • @double_hh
    @double_hh 9 ปีที่แล้ว

    +John Gizdich NOTHING MOVES , HOW TO FIX IT

    • @viveksaikode8039
      @viveksaikode8039 7 ปีที่แล้ว

      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);
      }
      }

  •  11 ปีที่แล้ว

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

  • @knodelcrafter6888
    @knodelcrafter6888 8 ปีที่แล้ว +1

    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?

    • @knodelcrafter6888
      @knodelcrafter6888 8 ปีที่แล้ว

      +Knödelcrafter I found a solution. I added jpanel.addkeylistener(this)

    • @ederm2111
      @ederm2111 8 ปีที่แล้ว +1

      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

  • @RepulsionVideos2000
    @RepulsionVideos2000 10 ปีที่แล้ว

    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

  • @flame8272
    @flame8272 11 ปีที่แล้ว

    what program are you using?

    • @nonsens4825
      @nonsens4825 4 ปีที่แล้ว

      i'ts called java eclipse

  • @GPiroote
    @GPiroote 11 ปีที่แล้ว

    Thank you maaan

  • @WolFX_FPS
    @WolFX_FPS 3 ปีที่แล้ว

    Good tut

  • @Istlemin
    @Istlemin 12 ปีที่แล้ว

    Can anyone give me the code for this. I have try to copy but it don't work.
    please

  • @akashshivshukla
    @akashshivshukla 6 ปีที่แล้ว

    Thanks for the video

  • @Miner493R
    @Miner493R 12 ปีที่แล้ว

    in the method paintComponent(Graphics g) you forgot to type:
    super.paintComponent(g)

  • @grupohebi
    @grupohebi 11 ปีที่แล้ว +12

    comeon man give us the code

  • @chg_m
    @chg_m 9 ปีที่แล้ว

    nice one

  • @Miner493R
    @Miner493R 12 ปีที่แล้ว

    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!!!

  • @NaelAlian
    @NaelAlian 10 ปีที่แล้ว

    really thanks for you

  • @arnoldbermeli9181
    @arnoldbermeli9181 10 ปีที่แล้ว

    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.

    • @mrwess1927
      @mrwess1927 5 ปีที่แล้ว

      Arnold Bermeli you need to implement all of the methods from the interface you implemented

  • @artmasterpl
    @artmasterpl 5 ปีที่แล้ว

    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

  • @pancakesimone
    @pancakesimone 12 ปีที่แล้ว

    You really should be using multiple classes. Also, x = x + velx is just lazy, use x += velx.

  • @averyaldridge5336
    @averyaldridge5336 11 ปีที่แล้ว

    Thank you!

  • @thomasvanroy
    @thomasvanroy 13 ปีที่แล้ว

    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!

  • @daychusu1206
    @daychusu1206 10 ปีที่แล้ว

    thank you ...

  • @SunnyGamingMemes
    @SunnyGamingMemes 2 ปีที่แล้ว

    oh it works

  • @viveksaikode8039
    @viveksaikode8039 7 ปีที่แล้ว

    thanks man!:)

  • @RockerNamedJoe
    @RockerNamedJoe 13 ปีที่แล้ว

    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?

  • @carlo29092
    @carlo29092 11 ปีที่แล้ว

    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

  • @pakxdabu7447
    @pakxdabu7447 8 ปีที่แล้ว

    thnx man

  • @averyaldridge5336
    @averyaldridge5336 11 ปีที่แล้ว

    Cool.

  • @Roger4313
    @Roger4313 12 ปีที่แล้ว

    the Timer code now doesn't work and so as some of them, I think Java did some shit

  • @meetpatel2853
    @meetpatel2853 7 ปีที่แล้ว

    first public class is error

  • @simsiewchee6131
    @simsiewchee6131 9 ปีที่แล้ว

    Hello

  • @fajardohansalexander3949
    @fajardohansalexander3949 6 ปีที่แล้ว

    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) {
    }
    }

    • @supremedeity9003
      @supremedeity9003 6 ปีที่แล้ว

      public void actionPerformed(ActionEvent arg0) {
      if(y < 0)
      {
      thats the prob

    • @fajardohansalexander3949
      @fajardohansalexander3949 6 ปีที่แล้ว

      what?? but why what is the prob..pls help me... Thanks in advance

  • @simsiewchee6131
    @simsiewchee6131 9 ปีที่แล้ว

    @OneDirection i wan some bigbang

  • @Roger4313
    @Roger4313 12 ปีที่แล้ว

    nevermind, I know how it works now. Im such a dumbass!

  • @RetroGamingClashOfClans
    @RetroGamingClashOfClans 7 ปีที่แล้ว

    10 "wanna be programmers" disliked lol

  • @MuresanVladMihail
    @MuresanVladMihail 11 ปีที่แล้ว

    lol?

  • @darrylclose4864
    @darrylclose4864 8 ปีที่แล้ว

    When I press the arrow keys ,the square doesn't move .How do I fix that