简体   繁体   中英

MouseListener doesn't recognize first click

Here is my code. This code is working with 1 click delay, but i don't know why. Can't find any reason why it's working this way. Can it be something with ArrayList or paintComponent method ?

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.util.ArrayList;

class PaintWindow {
public void createGUI() {
    JFrame f = new JFrame("My Canvas");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new MyPanel());
    f.setSize(800, 400);
    f.setResizable(false);
    f.setVisible(true);
    f.setLocationRelativeTo(null);
}

}
  class MyPanel extends JPanel {
public Point mousePos;
Timer animTimer;
ArrayList<ObjRectangle> arrForRect = new ArrayList<ObjRectangle>();
ObjRectangle ObjRect1;

public MyPanel() {
    final ActionListener taskPerformer=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i=0;i<arrForRect.size()-1;i++){
                arrForRect.get(i).animation();
                repaint();
            }
        }
    };

    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            System.out.println(getMousePosition());
            animTimer=new Timer(100,taskPerformer);
            animTimer.start();
            mousePos = getMousePosition();
            ObjRect1 = new ObjRectangle();
            arrForRect.add(ObjRect1);
            repaint();
        }
    });


}
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(arrForRect.size()==0){
        return;
    }
    arrForRect.get(arrForRect.size() - 1).drawObject(mousePos);
    for (int i = 0; i < arrForRect.size() - 1; i++) {
        arrForRect.get(i).paintSquare(g);
    }
}  }

class ObjRectangle extends JPanel {
    int x, y = 0;
    int width = 50;
    int height = 20;


    public void drawObject(Point coordinates) {
        this.x = coordinates.x;
        this.y = coordinates.y;
    }

    public void animation() {
        width++;
    }

    public void paintSquare(Graphics g) {
        g.setColor(Color.BLACK);
        g.drawRect(x, y, width, height);
    }
}

public class MainClass {
    public static void main(String[] args) {
    PaintWindow kazo=new PaintWindow();
    kazo.createGUI();
}}

Would appreciate any help.

As shown by your println, the click detection is fine. Silly bug here:

for (int i=0;i<arrForRect.size()-1;i++){
    arrForRect.get(i).animation();
    repaint();
}

arrForRect.size()-1 should be arrForRect.size() of course.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM