Problem with BufferStrategy and JPanel

1

Good, I'm trying to make a program in which you can use Graphics with BufferStrategy, so you can draw for example one circle on top of another and respect the order without oscillating. And also be able to use the Jpanel type components in which you can go putting buttons and toggle between panels. The problem I have now is that I can not make the panel stop blinking.

Here is the code:

public class Partida implements Runnable{

    private JFrame fondo;
    private JPanel panelAbajo;

    public final static int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
    public final static int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
    private Thread game;
    private boolean running;

    private Principal principal;

    private Jugador rojo;
    private Jugador azul;

    public Partida(Principal principal, Jugador rojo, Jugador azul) {
        this.rojo = rojo;
        this.azul = azul;
        this.fondo = new JFrame();
        this.panelAbajo = new JPanel();
        init();
    }

    private void init() {
        fondo.setSize(WIDTH,HEIGHT);
        fondo.setResizable(false);
        fondo.setUndecorated(true);
        fondo.setVisible(true);
        fondo.getContentPane().setLayout(null);
        fondo.getContentPane().setBackground(new Color(0));

        panelAbajo.setSize(1000, HEIGHT-600);
        panelAbajo.setVisible(true);
        panelAbajo.setLocation(50, 600);
        panelAbajo.setBackground(new Color(255));
        panelAbajo.setDoubleBuffered(true);
        fondo.add(panelAbajo);
        start();
    }

    public void tick() {

    }

    public void render() {
        BufferStrategy bs = fondo.getBufferStrategy();
        if (bs == null) {
            fondo.createBufferStrategy(2);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(50, 50, 1000, 550);
        panelAbajo.repaint();


        g.dispose();
        bs.show();
    }

    public void run() {
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60D;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;

        while (running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            if (delta >= 1) {
                tick();
                delta--;
            }
            render();
        }
        stop();
    }

    public synchronized void start() {
        if (running)
            return;
        running = true;
        game = new Thread(this, "game");
        game.start();
    }

    public synchronized void stop() {
        if (running)
            return;
        running = false;
        System.exit(0);
    }

}

If it can not be done like this, to do something like this, how should I approach it?

    
asked by David 29.05.2017 в 09:10
source

1 answer

1

Remove the line

panelAbajo.repaint()

and use a javax.swing.Timer to call the repaint periodically.

By calling the repaint within a cycle with such a small delay, it makes it work very heavy at RepaintManager .

In fact if you move the mouse quickly on the screen, you will notice that the panel will stop repainting for a short time, since it is so busy repainting that the simple processing of the mouse movement ends up overloading the processor .

When using the Timer, the repainting of the main thread is detached, and with this you can configure the repainting timer with a higher delay and not load the processor so much, fulfilling the same function.

    
answered by 31.05.2017 в 06:46