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?