Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GUI - MouseListener and ActionListener are possible to in same class?

CircleListener is an inner class in my panel class and it involves MouseListener interface now. MouseRelased method checks if the clicked area is encircled by a circle and if so it sets that shape to selected and removes the selected ones.

Now I need to an ActionListener to add random sized circles to this panel with a "timer" object. Question: Is it possible to implement "ActionListener" to CircleListener or it is better to create another inner class for "ActionListener"?

Thanks in advance

private class CircleListener implements MouseListener
{
    ShapesCanvas canvas;
    ShapeContainer container;
    Shape possibleShape;

    private CircleListener(ShapesCanvas canv, ShapeContainer cont)
    {
        this.canvas = canv;
        this.container = cont;
    }

    public void MouseRelased (MouseEvent e)
    {
        possibleShape = container.contains( e.getX(), e.getY());

        if( possibleShape != null)
        {
            ( (Selectable)possibleShape).setSelected(true);
            container.removeSelected();
        }
        canvas.repaint(); //repaints the last situation
    }
like image 636
Mustafa Avatar asked Dec 17 '22 01:12

Mustafa


1 Answers

It is definetly possible, just declare

private class CircleListener implements MouseListener, ActionListener

You can create two classes instead, and that is actually what I prefer, because then you have two distinct entities with clearly defined purposes. Each entity is responsible for just one function.

But both approaches are valid.

like image 120
Jakub Zaverka Avatar answered Apr 06 '23 00:04

Jakub Zaverka