ok i have this thread in java where if you click on the frame it will create a green rectangle on the place that you clicked with mouse. the more you clicked the more rectangle will be shown. and my goal is to delete the previous rectangle for the most recent one so there will be one rectangle. every time i clicked it will delete the previous rectangle and replace it with the new one. here is the code so you can see how the code work.
the important code is in getInput() this is where the code find the location of mouse click and then create a rectangle based on the location where the mouse clicked. how do i delete the old rectangle and just replace it with the new location for every click.
public class main implements Runnable {
// need thread,frame,run()
private static Thread thread;
private static JFrame frame;
private static Canvas canvas;
private boolean running = false;
private static BufferStrategy bs;
private static Graphics g;
private static int width = 1000;
private static int height = 800;
private static Random rand = new Random();
public static void main(String[] args) {
// TODO Auto-generated method stub
frame = new JFrame();
frame.setResizable(true);
frame.setTitle("Hello world");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width,height));
canvas.setMaximumSize(new Dimension(width,height));
canvas.setMinimumSize(new Dimension(width,height));
canvas.setBackground(Color.cyan);
frame.add(canvas);
frame.pack();
main main = new main();
main.start();
}
private static void update() {
}
private static void render() {
BufferStrategy bs = canvas.getBufferStrategy();
if (bs == null) {
canvas.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
getInput();
g.dispose();
bs.show();
}
private static void getInput() {
canvas.addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent me) { }
public void mouseReleased(MouseEvent me) { }
public void mouseEntered(MouseEvent me) { }
public void mouseExited(MouseEvent me) { }
public void mouseClicked(MouseEvent me) {
if(me.getButton() == MouseEvent.BUTTON1) {
BufferStrategy bs = canvas.getBufferStrategy();
if (bs == null) {
canvas.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect( me.getX(), me.getY(), 50, 50);
g.dispose();
}
if(me.getButton() == MouseEvent.BUTTON2) {
}
if(me.getButton() == MouseEvent.BUTTON3) {
}
}
});
}
private static void getBoxes() {
BufferStrategy bs = canvas.getBufferStrategy();
if (bs == null) {
canvas.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect(rand.nextInt(800) + 1, rand.nextInt(500) + 1, 50, 50);
}
private void init() {
}
public void run() {
init();
while (running) {
update();
render();
}
stop();
}
public synchronized void start() {
if(running) {
return;
}
running =true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if(running == false) {
return;
}
running = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}