Java GUI Lesson 12 | Moving Graphics with arrow keys

preview_player
Показать описание
teaching how to move a circle around with the arrow keys
Рекомендации по теме
Комментарии
Автор

A 12 year old tutorial helped me out. Thanks!

trueguide
Автор

If you don't care if the object continues going to the side, you don't need veld = 0. Furthermore, you set vely=-1.5 instead of 1.5 because the y coordinates are "flipped" in java and many other languages.

macheads
Автор

i think it means it is void (doesn't have/is without) of a type, as opposed to it being a method of type "int" or "String", which would use "return" to display something its respective variable type

MegaToasts
Автор

Just for people who are watching this now and want to make it so that *It only moves when you are holding the keys and stops when ur not*
make methods just like the up, down, left or right, but this time, make all the velX and velY doubles/integers(Depending on what you made it) as 0 and rename them to something like "up2" or "leftReleased" and add all these to the keyReleased method. heres a code of how I did it (you can add this into your class where you have the movement):


//the methods
public void (up2)
{
velX = 0;
velY = 0;
}
public void (down2)
{
velX = 0;
velY = 0;
}
public void (left2)
{
velX = 0;
velY = 0;
}
public void (right2)
{
velX = 0;
velY = 0;
}

//now add it in keyReleased
public void KeyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == ActionEvent.VK_UP)
{
up2();
}
if(key == ActionEvent.VK_DOWN)
{
down2();
}
if(key == ActionEvent.VK_LEFT)
{
left2();
}
if(key == ActionEvent.VK_RIGHT)
{
right2();
}
}

PS - This was so wierd to write this in youtube lol and sorry for my wierd style of code writing

sidelymations
Автор

@ComputerKing240 Just have an integer variable that you increment (add one to) every time the key event is fired.

macheads
Автор

Thank you! I spent the last hour trying to figure this out!

neocarbunkle
Автор

And heres a question, for example, you go up
public void up()
{
vely = -1.5;
velx = 0;
}
how comes the velocity of y equals to -1.5 directly, why not -=1.5?
And what's the reason to set velx = 0?

jackyzou
Автор

@scottyy888 If by application you mean class, you do not have to. Your main class could be a subclass of JFrame, but in general this is not good practice. Therefore, it's best to split your code up between multiple classes.

macheads
Автор

@malakov5 no i go over keeping it from going off screen in the previous tutorial

macheads
Автор

how to make it move in a non straight direction by using two buttons? 🤔

فيافيالتأملمهمةإصلاح
Автор

Eclipse doesn't like the start() method for t. I don't know why, but I personally don't see a start() method it can refer to. It says "The method start() is undefined for the type Timer".

elStusso
Автор

Remember to call .start(); you might not be using Swing timers. Try replacing "Timer" with "javax.swing.Timer".

macheads
Автор

This is good, but I'm trying to make a square instead of a circle so what do I have to change? Oh, and is it possible to change the color of the shape as well?

TehPiemaygor
Автор

can you make a tutorial of how to do a 2D GAME LIKE A FROGGER WITH A GRID

grandorottcod
Автор

I have a question, why did you need to have another application out for the JFrame and main method? you cant combine it all into one application?

Scott
Автор

i was wondering if u can move it diagonally?

kanekiannx
Автор

how comes mine doesn't move properly? Mine moves and fill the place that the shape moves to. It paints all over the place and all I want is to move the shape, not painting everywhere.

jackyzou
Автор

Do you mean like a stop key or just make it stop when the arrow key(s) is released?

Hwyadylaw
Автор

what if I want to add 2 graphics to the same frame?

LupuAlex
Автор

Hi I am facing a minor problem with this code, its like when I am done writing both the classes the class second has an error before it, and it says that some methods have not been implemented what should I do about that?

AngelBeatz
visit shbcf.ru