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
The Harsh Truth About Women | Nietzsche
Role-playing speech: [Music] They lied to you. Society, history, even your own desires, wrapped in Illusions. Women are not what they told you, not Angels, not villains, but something far more unsettling and far more powerful. Over a century ago, n saw t…
Derivative of log_x (for any positive base a­1) | AP Calculus AB | Khan Academy
I know from previous videos that the derivative with respect to x of the natural log of x is equal to 1 / x. What I want to do in this video is use that knowledge that we’ve seen in other videos to figure out what the derivative with respect to x is of a…
We WILL Fix Climate Change!
Our home is burning. Rapid climate change is destabilizing our world. It seems our emissions will not fall quickly enough to avoid runaway warming, and we may soon hit tipping points that will lead to the collapse of ecosystems and our civilization. While…
Watch Koko the Gorilla Use Sign Language in This 1981 Film | National Geographic
[Music] Near San Francisco, California, a fascinating and now controversial experiment has been underway since 1972. Research psychologist Penny Patterson is teaching lowland gorillas Koko the American Sign Language of the deaf. Dr. Patterson claims Koko …
How to renovate your private jet
I was going to say, like, what’s a turnaround time for the change of an interior? Also, for example, if you put half a million into renovating the interior, there are three things you could do to the interior: the soft goods, which is all the fabrics, whe…
Eyeballs vs. Bullets in SlowMo - Smarter Every Day 26
[music] Hey, it’s me, Destin. When my granddad was young, he lost an eye playing cowboys and Indians, so tonight I’m gonna show you why you should always wear safety glasses or goggles when you’re doing an experiment. This isn’t very popular because I’ve …