Hello, I made a program that you move a ball with the arrow keys that only flashes the screen when it moves. I've seen other programs in this kind of mine that just do not blink. If anyone can help me... (the background stays normal but the ball while moving that flashes)
code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Cobrinha extends JFrame implements Runnable {
private Thread th = new Thread(this);
private int posEsq = 300;
private int posTop = 300;
private int ultTecla = 37;
public Cobrinha() {
super("Cobrinha");
this.setSize(600,600);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
mover(e.getKeyCode());
}
});
this.getContentPane().setBackground(Color.GREEN);
th.start();
this.setVisible(true);
}
public void mover(int tecla) {
ultTecla = tecla;
}
public void run() {
while (true) {
switch (ultTecla) {
case 38: posTop--; break;
case 40: posTop++; break;
case 37: posEsq--; break;
case 39: posEsq++; break;
}
try { th.sleep(6); } catch (InterruptedException e) { }
this.repaint();
}
}
public void paint(Graphics g) {
super.paint(g);
g.fillOval(posEsq, posTop, 20, 20);
}
public static void main(String [] args) {
new Cobrinha();
}
}