I am creating a program in Java where you move a square. However, the square does not move. I have tried a lot of things, but none of them work. It seems to be a problem with repaint. How could I solve this?
package movingSquare;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class MovingSquare extends JComponent {
private static final long serialVersionUID = -3778627464016140311L;
public static JFrame f = new JFrame("Moving Square");
public static int x;
public static int y;
public static Rectangle r = new Rectangle(x, y, 20, 20);
public static MovingSquare mv = new MovingSquare();
public static KeyListener kl = new KeyListener() {
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode() == KeyEvent.VK_UP) {
y += 1;
r.setLocation(x, y);
mv.repaint(r);
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN) {
y -= 1;
r.setLocation(x, y);
mv.repaint();
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT) {
x -= 1;
r.setLocation(x, y);
mv.repaint();
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
x += 1;
r.setLocation(x, y);
mv.repaint();
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}};
public static void main(String[] args) {
// TODO Auto-generated method stub
f.setBackground(Color.BLUE);
f.setMinimumSize(new Dimension(720, 720));
f.setResizable(false);
f.setFocusable(true);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.addKeyListener(kl);
f.add(new MovingSquare());
f.pack();
f.setVisible(true);
boolean e = true;
while(e) {
System.out.println("x: " + x + " y: " + y);
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g1 = (Graphics2D) g;
g1.setColor(Color.BLUE);
g1.fillRect(0, 0, 720, 720);
g1.setColor(Color.RED);
g1.fill(r);
}
}