Thread from a flashing point

3

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(); 
    }
}
    
asked by anonymous 22.03.2016 / 02:16

1 answer

3

To use the double buffer in Swing, you can have your class inherit from JPanel, and then override the paintComponent method. After that, just insert your class into a JFrame:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Cobrinha extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread th = new Thread(this);
    private int posEsq = 300;
    private int posTop = 300;
    private int ultTecla = 37;

    public Cobrinha() {
        setSize(600, 600);
        setBackground(Color.GREEN);
        th.start();
        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 {
                Thread.sleep(6);
            } catch (InterruptedException e) {
            }
            repaint();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillOval(posEsq, posTop, 20, 20);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Cobrinha");
        Cobrinha cobrinha = new Cobrinha();

        frame.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                cobrinha.mover(e.getKeyCode());
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(cobrinha);
        frame.setBounds(0, 0, 600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
    
22.03.2016 / 14:23