I know this is all related to double buffering and stuff but I’m trying to use createVolatileImage to draw some stuff on screen, however - there are trails being left behind. I’ve tried to get rid of them by clearing over the background image over the drawing but it doesn’t work.
package _game;
import java.awt.*;
import javax.swing.JFrame;
public class _game extends Canvas implements Runnable {
*/
private Thread t = new Thread(this);
private boolean running;
private static int width = 160;
private static int height = 120;
private static int scale = 4;
public mouse mouse = new mouse();
private Screen screen = new Screen();
private long lastTime;
public static int tick;
public _game(){
int w = width * scale;
int h = height * scale;
setPreferredSize(new Dimension(w, h));
setMaximumSize(new Dimension(w, h));
setMinimumSize(new Dimension(w, h));
setBackground(Color.BLACK);
addMouseListener(mouse);
addMouseMotionListener(mouse);
this.setFocusable(true);
}
public void start(){
lastTime = System.currentTimeMillis();
running = true;
t.start();
}
public static void main(String[] args){
_game ga = new _game();
JFrame frame = new JFrame("derr");
frame.add(ga);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
ga.start();
}
public synchronized void stop(){
running = false;
}
public void paint(Graphics g)
{
}
public void update(Graphics g)
{
render(g);
}
public void render(Graphics g){
}
@Override
public void run() {
Image image = null;
while(running){
tick = ((int)(System.currentTimeMillis() - lastTime)/100);
if(image == null){
image = createVolatileImage(width * scale, height * scale);
}
Graphics g = image.getGraphics();
screen.render(g, mouse.mx, mouse.my);
g = getGraphics();
g.drawImage(image, 0, 0, width * scale, height * scale, 0, 0, width * scale, height * scale, null);
g.dispose();
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
screen.render(g) just draws rectangles at my mouse pos (when it is dragged)