user3696672
user3696672

Reputation: 51

how to implement mouselistener on a particular shape?

i have created an circular strip using swing in java, and now i want to display some text on mouse click clicking a particular region of the strip such as region between 45 degrees and 135 degrees central angle can anybody help me out?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame implements MouseListener
{
//public static final int MOUSE_CLICKED;

public static void main(String[] args)
{
    new Test();
}
public Test()
{
    this.setSize(400,400);
    this.setVisible(true);

    addMouseListener(this);
}

public void paint(Graphics g)
{
    g.fillArc(50,50,230,270,45,90);
    g.setColor(Color.red);
     double radius1 = 230;
     double theta=90;
    double a1 = (Math.PI * radius1 * radius1*theta)/360;
    System.out.println("area"+a1);
     double radius2 = 200;

    double a2 = (Math.PI * radius2 * radius2*theta)/360;
    System.out.println("area2"+a2);
    double a=a1-a2;
     System.out.println("fin area"+a);

    g.fillArc(50,50,230,270,135,90);
    g.setColor(Color.blue);

    g.fillArc(50,50,230,270,225,90);
    g.setColor(Color.yellow);

    g.fillArc(50,50,230,270,315,90);
    g.setColor(Color.magenta);

    Graphics2D comp2D=(Graphics2D)g;
    comp2D.setColor(Color.white);
    Ellipse2D.Float sign=new Ellipse2D.Float(90F,90F,150F,200F);
    comp2D.fill(sign);

    {
        //System.out.println("hello");
    }
}

//}

}

Upvotes: 2

Views: 2462

Answers (1)

Marco13
Marco13

Reputation: 54631

Don't extend JFrame and don't override paint. Instead, you should extend JPanel and override paintComponent. Additionally, you should create the GUI on the Event Dispatch Thread.

Concerning the actual question: There are different possible solutions for this. You'll have to implement the MouseListener interface in any case. And after a mouse click, you have to check whether the mouse position is contained in the respective region. You could to this manually, by comparing coordinates and computing angles, but this could be a hassle. It should be much easier to create Shape objects that you fill with the respective colors, and then just check whether any of the shapes contains the mouse position.

This approach is roughly sketched here, although I did not reproduce your exact shapes. (You may have to create them with help of the Area class and its methods for union and intersection).

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ShapeClickTest {


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new ShapeClickTestPanel());
        f.setSize(400, 400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

class ShapeClickTestPanel extends JPanel implements MouseListener {
    private final List<Shape> shapes;
    private final List<Color> colors;

    public ShapeClickTestPanel() {
        addMouseListener(this);

        shapes = new ArrayList<Shape>();
        colors = new ArrayList<Color>();

        shapes.add(new Arc2D.Double(50, 50, 230, 270, 45, 90, Arc2D.OPEN));
        colors.add(Color.RED);

    }

    @Override
    protected void paintComponent(Graphics gr) {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D) gr;

        for (int i = 0; i < shapes.size(); i++) {
            Shape shape = shapes.get(i);
            Color color = colors.get(i);
            g.setColor(color);
            g.fill(shape);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        for (int i = 0; i < shapes.size(); i++) {
            Shape shape = shapes.get(i);
            if (shape.contains(e.getPoint())) {
                System.out.println("Clicked shape " + i);
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}

Upvotes: 5

Related Questions