yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java GUI Lesson 12 | Moving Graphics with arrow keys


3m read
·Nov 3, 2024

Processing might take a few minutes. Refresh later.

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

More Articles

View All
How I Invest In Crypto & DeFi
I thought it’s such a piece of trash. What a stupid idea, and it did a ton. I’m going to go to every beach on Earth. I just want to go to every beach on Earth, and I did. It took me three years, and at the end of it, I was bored out of my [ __ ] mind. I m…
Salman Rushdie: ISIS' Most Dangerous Weapon Is Media | Big Think
Well, aside from the obvious thoughts of ISIS, which is horror and disgust, I think we have to look at the fact that they’re so unusually good at using the media. That’s a great deal of their unique power: it’s the way in which they can create themselves …
Interpretting exponential expression
The expression ( 5 * 2^T ) gives the number of leaves in a plant as a function of the number of weeks since it was planted. What does two represent in this expression? So pause this video and see if you can figure it out on your own. All right, so let’s …
Testing Tesla on the Deadliest Road in America 🐉
This is my dad’s Tesla and I’m going to take it driving on… the Deadliest Road in America. But! I’m not going to be driving because I’ve just installed the full self-driving, pre-release BETA software. It’s only allowed right now on a tiny number of cars …
Why the Parker Solar Probe is NASA's most exciting mission | Michelle Thaller | Big Think
One of the most exciting things that’s going on at NASA right now is that we have a probe that’s actually orbiting very close to the sun. And over the next years, it’s going to get closer, and closer, and closer. It’s called the Parker Solar Probe, and th…
Unboxing: Hawking HWUG1 Wifi Adapter
Hey guys, this is mackinz101, and today I’m going to be doing an unboxing video of my new Hawking wireless adapter. So, it came today in the mail from Hawking Technology, and it says, of course, works with Windows 7. But, um, I’m going to be working it w…