Search code examples
javagraphicsmouseeventawtmouseclick-event

JAVA Mouse Event


In my program after selecting the option from pie menu related shape drawn around pie menu. what i want is when user select the option and then click anywhere in the screen related shape will draw there in the screen. can anyone tell me how can i achieve this?

Mouse click event code

public void mouseClicked(MouseEvent e) 
{
    double base=x1-s;
    double prep=as-y1;
    double angle=Math.atan2(prep, base);
    angle=((angle*180)/Math.PI);

    if(angle==0)
    {

    }
    else if(angle>0&&angle<45)
    {

        p.mc1=e.getX()+100;
        p.mc2=e.getY()-25;
        p.repaint();        

    }

Pie Menu
Pie Menu Drawing


Solution

  • Not sure how to make a shape exactly but a rectangle will help. Calculate the co-ordinates of each part of the pie chart and approximate it to a rectangle. For example, if the exit part in the pie chart is from (20,80), create a rectangle from (20,80) and width and height as 20 pixels.Create 9 rectangle objects like

    Rectangle r1=new Rectangle(int startX,int startY,int width,int height);

    And using mouseListener, you can easily check which rectangle is selected by using

    public void mousePressed(MouseEvent me){
        if(r1.contains(me.getPoint){
             selected=1;
        }
    

    Use the selected value in a switch case and implement the methods for whatever u want to.