Search code examples
javanetbeansgraphics2d

How to Check Mouse Click in 2D Graphics java.


I need to make a code that will Draw a triangle. then user will click the inside or outside the triangle. and A dialogue box will be shown that "Click is inside the triangle" if it is.

I have code to draw triangle, here is the code, now what to do. i dont know. if any one help then please.

I have tried axis base but i did not come up with required result.

public void paintComponent(Graphics g)  
        {  
            Graphics2D g2 = (Graphics2D) g;  
            g2.draw(new Line2D.Double (150, 200, 200, 100)); 
            g2.draw(new Line2D.Double (100, 100, 150, 200));           
            g2.draw(new Line2D.Double (100, 100, 200, 100)); 
        }  

Output is here. enter image description here


Solution

  • The Shape class is what you want to use. Instead of drawing the triangle by hand (with individual draw statements for each line), create a Shape object representing the triangle. A Polygon will suffice for creating triangles.

    Instead of changing the painting of the JFrame, it's a better idea to have a custom painted panel and add that to the frame.

    public class YourFrame extends JFrame { //Replace with your class name, obviously
    
        public YourFrame(){
            //Create and add trianglePanel
            setLayout(new BorderLayout());
            add(new TrianglePanel(), BorderLayout.CENTER);
            pack();
            repaint();
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        class TrianglePanel extends JPanel implements MouseListener{
            private Polygon triangle;
    
            public TrianglePanel(){
                //Create triangle
                triangle = new Polygon();
                triangle.addPoint(150, 200);
                triangle.addPoint(100, 100);
                triangle.addPoint(200, 100);
    
                //Add mouse Listener
                addMouseListener(this);
    
                //Set size to make sure that the whole triangle is shown
                setPreferredSize(new Dimension(300, 300));
            }
    
            /** Draws the triangle as this frame's painting */
            public void paintComponent(Graphics g){
                Graphics2D g2d = (Graphics2D)g;
                g2d.draw(triangle);
            }
    
            //Required methods for MouseListener, though the only one you care about is click
            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
    
            /** Called whenever the mouse clicks.
              * Could be replaced with setting the value of a JLabel, etc. */
            public void mouseClicked(MouseEvent e) {
                Point p = e.getPoint();
                if(triangle.contains(p)) System.out.println("Triangle contains point");
                else System.out.println("Triangle Doesn't contain point");
            }
        }
    }