简体   繁体   中英

mouse listener does not listen

I have a school assignment that I'm stuck with.

I'm aiming for a MVC implementation but right now I'm just doing it all in the view (just to make thing simple for now).

So - I have a frame and it has a panel.

The panel has a list of shapes. Whenever the user is pressing the addLine/addRect buttons - an event is raised and a line/rect is added to this list.

The paintComponent function draws all the shapes in the list (all shapes knows how to draw themselves).

So far so good - it works!

The only other demand in this assignment is that whenever the user clicks on a point in the drawing area - all shapes that contain this point are deleted. each shape has its own Contains(p) function. so I've decided to add a MouseListener to the panel that will get the X,Y coordinates of the click and will remove the relevant shapes from the list of shapes.

I don't know if this is a good idea or not but this is not my problem at the moment.

My problem is that the MouseListener does not respond to the clicking - I know that because I have a breakpoint on the inside of the implementation of mouseClicked and the debugger never got to that breakpoint.

My question is why?

Here is my code: it has some more issues I need to address but for now they don't concern me

//MyFrame.java

public class MyFrame extends JFrame {
    private MyJPanel panel ;

    public MyFrame() throws MyShape.IllegalArgumentException {
        initUI();
    }

    private void initUI() throws IllegalArgumentException {
        setLayout(new FlowLayout());
        panel = new MyJPanel();
        add(panel);
        setTitle("Shapes Editor");
        setSize(600, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        setVisible(true);
    }
}

//MyPanel.java

public class MyJPanel extends JPanel {

    ArrayList<MyShapeAbstract> pic;
    DrawingArea drawingArea;
    ButtonsPanel buttonsPanel;

    public ButtonsPanel getButtonsPanel() {
        return buttonsPanel;
    }
    public void setButtonsPanel(ButtonsPanel buttonsPanel) {
        this.buttonsPanel = buttonsPanel;
    }

    public ArrayList<MyShapeAbstract> getPic() {
        return pic;
    }
    public void setPic(ArrayList<MyShapeAbstract> pic) {
        this.pic = pic;
    }

    public MyJPanel() throws MyShape.IllegalArgumentException {
        initUI();
    }

    private void initUI() throws MyShape.IllegalArgumentException {
        setLayout(new FlowLayout());
        pic = new ArrayList<MyShapeAbstract>();
        add(buttonsPanel = new ButtonsPanel(this));
        add(drawingArea = new DrawingArea(this));
        drawingArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Point p = null;
                try {
                    p = new Point(e.getX(), e.getY());
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
                Iterator<MyShapeAbstract> iter = pic.iterator();
                while (iter.hasNext()){
                    MyShapeAbstract shape = iter.next();
                    if(shape.contains(p))
                    {
                        iter.remove();
                    }
                }
                drawingArea.repaint();
                pic.clear();
                repaint();
            }
        });

        JButton addLineButton = buttonsPanel.getAddLineButton();
        addLineButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    addLine();
                    drawingArea.repaint();
                    repaint();
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
            }
        });
        add(addLineButton);

        JButton addRectButton = buttonsPanel.getAddRectButton();
        addRectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    addRect();
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
                drawingArea.repaint();
                repaint();
            }
        });
        add(addRectButton);
    }

    @Override
    public void repaint() {
        Graphics g = getGraphics();
        if(g!=null && pic != null)
            for(MyShapeAbstract shape : pic){
                shape.draw(g);
            }
    }

    private void addRect() throws IllegalArgumentException {
        Random r = new Random();
        pic.add(new MyRectangle(new Point(r.nextInt(200), r.nextInt(200)), new Point(r.nextInt(200),r.nextInt(200))));
    }
    private void addLine() throws IllegalArgumentException {
        Random r = new Random();
        pic.add(new MyLine(new Point(r.nextInt(200), r.nextInt(200)), new Point(r.nextInt(200),r.nextInt(200))));
    }
}

class DrawingArea extends JPanel{
    public DrawingArea(MyJPanel parent) {
        this.parent = parent;
        setLayout(new FlowLayout());
    }

    @Override
    public void repaint() {
        Graphics g = getGraphics();
        if(g!=null && parent.getPic() != null)
            for(MyShapeAbstract shape : parent.getPic()){
                shape.draw(g);
            }
    }

    MyJPanel parent;
}

class ButtonsPanel extends JPanel{
    //ArrayList<JButton> buttons = new ArrayList<JButton>();

    MyJPanel parent;
    private JButton addLineButton  = new JButton("addLineButton");
    private JButton addRectButton = new JButton("addRectButton");


    ButtonsPanel(final MyJPanel parent){
        this.parent = parent;
        setLayout(new FlowLayout());
        add(addLineButton);
        add(addRectButton);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Point p = null;
                try {
                    p = new Point(e.getX(), e.getY());
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
                Iterator<MyShapeAbstract> iter = parent.getPic().iterator();
                while (iter.hasNext()){
                    MyShapeAbstract shape = iter.next();
                    if(shape.contains(p))
                    {
                        iter.remove();
                    }
                }
                repaint();
            }
        });
    }

    @Override
    public void repaint() {
        Graphics g = getGraphics();
        if(g!=null && parent.getPic() != null)
            for(MyShapeAbstract shape : parent.getPic()){
                shape.draw(g);
            }
    }

    public JButton getAddLineButton() {
        return addLineButton;
    }

    public void setAddLineButton(JButton addLineButton) {
        this.addLineButton = addLineButton;
    }

    public JButton getAddRectButton() {
        return addRectButton;
    }

    public void setAddRectButton(JButton addRectButton) {
        this.addRectButton = addRectButton;
    }
}

I added the following to your Button Panel:

 ButtonsPanel(final MyJPanel parent){
    this.parent = parent;
    setLayout(new FlowLayout());
    add(addLineButton);
    add(addRectButton);
    //show me the Panel size :)
    this.setBackground(Color.RED);

The small red rectangle is the area where your mouse listeners work. So maybe you mean parent.addMouseListener or drawingArea..addMouseListener (However your drawingArea is also kinda small)? Just use the trick with the Background and check your area and adjust the size or the Panel you really want to have the Listener. Maybe also add MyShapeAbstract,MyLine, MyRectangle so its possible to test the complete code.

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