Java GUI Lesson 12 | Moving Graphics with arrow keys
Hey, this is Matt heads 101. My name is Jake, and in this job, okay with you? I'm gonna be teaching you how to move a graphic around with the arrow keys. In this case, it's just going to be a simple oval, which is what I'm going to be using for the next couple of tutorials just because it's easy. And yeah, so if you didn't watch my last tutorial, I'm just making a move. You should probably watch that first before we learn how to make a move with the arrow keys.
So how we're going to be doing this is we're using something called a key listener and the action listener. Um, had one method that it implements and the key listener has three, so it can detect on the keys pressed, released, or typed. And so we're going to make four methods: up, down, left, and right. Those are going to change the X and Y velocities accordingly so that will move up, down, left, to right. We're going to make the key listener detect, like, let's say if the up key is pressed, to call the up method. So that's how we're going to be doing this.
And you see, only imports child X Wing estrus AWT Astor's AWT that event either ukg on, so I'm just going to minimize those. Done. First thing I want to do is implement two things, and we're going to implement the action listener because we've been using a timer, and the key listener because we're going to be using key listen, like I just said before. So now again we're going to make two things. One is our timer, timer t evil new timer, and every five seconds, it's going to call that action listener.
And to make some doubles, we're screaming X, Y velocity, X velocity, Y, which is going to be the X and Y locations, and set everything to 0 initially, so it'll just be in the top-left corner just kind of chillin' there. Next, I'm going to make our constructor, so public second, and in here I'm going to start the timer t dot start and add key listener this, which means add this key listener to this song, went to this J panel is extending.
Next, set focusable true, which means it sets the focus to this, which just allows us really to use the key listener, and set focus traversal keys enabled false. And what that means is like there's some keys that act like bizarre, like the tab key and things, and this is just going to make the app normal by setting focused traversal keys enabled to false. Next, we're going to make the paint component method, which allows us to draw graphics on the screen.
I went over all this before, so I'm not really going to go over it now. So super got paint component G graphics to the gene, and now I'm just going to G to dot fill new ellipse to the dot double on X Y and only 40 by 40, so that's going to draw up to the X and Y coordinates.
Okay, so now public void action perform action event E and here I'm going to repaint so I can redraw this when the x and y values become new coordinates and change them. And y plus equals velocity Y, and now it's for my last tutorial. That just basically changes the x coordinates and the y coordinates by adding to them, and then it redraws it at the new coordinates. This is where we paint us; we can do this every 5 milliseconds.
Next, public void up, and watch what I do in here. So velocity of y equals negative 1.5 and velocity of x equals zero. This is the up method, and we're going to call this when the up key is pressed. And so, X velocity will be zero, so it's not going to move left or right, and the Y velocity is going to be negative 1.5. And that's because the values decrease as you go up, so I'm decreasing this value will move up.
So now, public void down, velocity y equals regular 1.5 and velocity x equals 0. Then I have public void left, and that I'll set the x equals a negative 1.5, a whole bunch of to never been a little too slow, and velocity of y is equal to zero because the value two decreases as you go to the left, and we don't want up-and-down move, and if we're just moving left.
And public void right, and we're going to have a velocity x equal 1.5 and velocity y equals zero. Now we're going to make three methods, and this key listener, even if you're only using one of the methods of the key list...