0

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();
            };

    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
tester7281
  • 13
  • 2
  • This looks to be grossly over-complicated: why even use a separate Thread? Why worry about deleting the Rectangle? Why not simply use a single Rectangle field that is drawn, and then when the GUI is clicked, change the object referenced by the field and call repaint? Why are you using a Canvas in a Swing application and drawing with a BufferStrategy object, rather more simply drawing within the paintComponent method of a JPanel? – Hovercraft Full Of Eels Dec 26 '17 at 01:15
  • .... and get most of your code out of the static world into the instance world. – Hovercraft Full Of Eels Dec 26 '17 at 01:22
  • is thread because that all i know how to create custom rectangle with java in eclipse. i never heard of paintComponent before thats why i didnt use it. – tester7281 Dec 26 '17 at 01:57
  • i didnt think my question is duplicate of the one you mention. his problem is that he couldnt draw rectangle. mine did create rectangle but i want everytime i click it will delete the rectangle that i previously made then make a new one at the location that i click. – tester7281 Dec 26 '17 at 02:01
  • could you please change the status of this question as a duplicate. i found the answer and i want to share it. – tester7281 Dec 26 '17 at 02:10

0 Answers0