"Vsync" simulado para evitar el shutering

This commit is contained in:
2025-02-11 13:58:10 +01:00
parent d0ec48927e
commit e6ab87defa
2 changed files with 18 additions and 4 deletions

View File

@@ -142,7 +142,7 @@ public class Arkanoid {
for (Actor actor : actores) {
actor.actua();
}
canvas.repaint();
canvas.pintar();
}
long tiempoTranscurrido = new Date().getTime() - tiempoInicio;

View File

@@ -3,11 +3,14 @@ package net.h4ckx0r;
import net.h4ckx0r.actores.Actor;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.List;
public class MiCanvas extends Canvas {
List<Actor> actores = null;
private BufferStrategy strategy = null;
private int[][] estrellitas = null;
public MiCanvas(List<Actor> actores) {
@@ -24,9 +27,18 @@ public class MiCanvas extends Canvas {
}
}
@Override
public void paint(Graphics g) {
this.setBackground(Color.BLACK);
public void pintar() {
if (this.strategy == null) {
this.createBufferStrategy(2);
strategy = getBufferStrategy();
Toolkit.getDefaultToolkit().sync();
}
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0, this.getWidth(), this.getHeight());
for (int[] estrella : estrellitas) {
g.setColor(Color.GRAY);
@@ -36,6 +48,8 @@ public class MiCanvas extends Canvas {
for (Actor actor : actores) {
actor.paint(g);
}
strategy.show();
}