how to make an object move with arrow keys in java
Java
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.awt.event.*;
import javax.swing.*;
public class HandlingEvents implements Runnable {
JFrame frame;
int myX = 400;
int myY = 400;
Canvas canvas;
BufferStrategy bufferStrategy;
boolean running = true;
public HandlingEvents() {
frame = new JFrame("Basic Game");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(500, 500));
panel.setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, 500, 500);
canvas.setIgnoreRepaint(true);
panel.add(canvas);
canvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
moveIt(evt);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
canvas.requestFocus();
}
public void run() {
while (running = true) {
Paint();
try {
Thread.sleep(25);
} catch (InterruptedException e) {
}
}
}
public static void main(String[] args) {
HandlingEvents ex = new HandlingEvents();
new Thread(ex).start();
}
public void Paint() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, 500, 500);
Paint(g);
bufferStrategy.show();
}
protected void Paint(Graphics2D g) {
g.fillOval(myX, myY, 30, 30);
}
public void moveIt(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
myY += 5;
break;
case KeyEvent.VK_UP:
myY -= 5;
break;
case KeyEvent.VK_LEFT:
myX -= 5;
break;
case KeyEvent.VK_RIGHT:
myX += 5;
break;
}
}
}
Also in Java:
- Title
- rewrite file java
- Category
- Java
- Title
- java swing change label icon
- Category
- Java
- Title
- simple calculator program in java
- Category
- Java
- Title
- java read string input
- Category
- Java
- Title
- java scanner input int
- Category
- Java
- Title
- read and write files pattern java
- Category
- Java
- Title
- runtime java examples
- Category
- Java
- Title
- print int in java
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- json request body not getting parsed by spring boot controller
- Category
- Java
- Title
- how to make a loop in java
- Category
- Java
- Title
- gson parse json
- Category
- Java
- Title
- keep jframe on top
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- java string length validation regex
- Category
- Java
- Title
- get long from date java
- Category
- Java
- Title
- how to use spring in java
- Category
- Java
- Title
- crit chance in java
- Category
- Java
- Title
- java singleton implementation
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- find duplicate elements in array in java
- Category
- Java
- Title
- random processing
- Category
- Java
- Title
- get today date in java 8
- Category
- Java
- Title
- creating thread in java example
- Category
- Java
- Title
- java how to program
- Category
- Java
- Title
- how to make a text field required in android studio
- Category
- Java
- Title
- how to take max value from priority queue in java
- Category
- Java
- Title
- java 8 find in list by property
- Category
- Java
- Title
- isnumber java
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- how to call a static method in java
- Category
- Java
- Title
- how to add jar in maven java application in netbeans
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- java execute jar from main
- Category
- Java
- Title
- java map geeksforgeeks
- Category
- Java
- Title
- java singleton
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- excel data formatter in java
- Category
- Java
- Title
- init admob
- Category
- Java
- Title
- bigdecimal divide java
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- how to change maven java version in po,
- Category
- Java
- Title
- arrays.fill java
- Category
- Java
- Title
- swapping of two numbers in java
- Category
- Java
- Title
- how to change top of window in java
- Category
- Java
- Title
- processing sound android
- Category
- Java
- Title
- java round double to 2 decimal places
- Category
- Java
- Title
- bukkit java set leather armor color from hex
- Category
- Java
- Title
- java stack
- Category
- Java
- Title
- jsonobject to java object
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- String array into LinkedList java
- Category
- Java
- Title
- transformer un string en double java
- Category
- Java
- Title
- java string reverse
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- difference between java and javax
- Category
- Java
- Title
- bootstrap center text vertically
- Category
- Java
- Title
- javadoc reference general toString
- Category
- Java
- Title
- java timestamp
- Category
- Java
- Title
- expression régulière seulement un espace java
- Category
- Java
- Title
- get method of a class which I only have string to
- Category
- Java
- Title
- java jpa criteriabuilder in xample
- Category
- Java
- Title
- how to collect objective in java
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- remove last character from string java
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- split with scquere braket in java
- Category
- Java
- Title
- how to print to console in java
- Category
- Java
- Title
- indexof java
- Category
- Java
- Title
- sc.nextline skips
- Category
- Java
- Title
- java previous permutation
- Category
- Java
- Title
- what is a method example in java
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- java printf tab
- Category
- Java
- Title
- tostring() java example
- Category
- Java
- Title
- check if object is empty java 8
- Category
- Java
- Title
- how to create a Rectangle in java
- Category
- Java
- Title
- executors java
- Category
- Java
- Title
- remove first character from string
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- new hashmap java
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- converting char array to string
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- android how to know when snackbar is done
- Category
- Java
- Title
- nullpointer extension
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- string contains java
- Category
- Java
- Title
- how to find complement of a number in java
- Category
- Java
- Title
- FORM EN JAVA SCRIPT
- Category
- Java
- Title
- java for loop add multiple declarations
- Category
- Java
- Title
- java public static void main
- Category
- Java