如何在2D图形java中检查鼠标单击。

如何在2D图形java中检查鼠标单击。,java,netbeans,graphics2d,Java,Netbeans,Graphics2d,我需要做一个代码来画一个三角形。然后用户将单击三角形的内侧或外侧。如果是,将显示一个对话框“单击位于三角形内” 我有画三角形的代码,这是代码,现在做什么。我不知道。如果有人帮忙,那就请 我试过axis base,但没有达到要求的结果 public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.draw(new Line2D.Dou

我需要做一个代码来画一个三角形。然后用户将单击三角形的内侧或外侧。如果是,将显示一个对话框“单击位于三角形内”

我有画三角形的代码,这是代码,现在做什么。我不知道。如果有人帮忙,那就请

我试过axis base,但没有达到要求的结果

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)); 
        }  
输出在这里。
您想要使用的是
形状
类。创建一个表示三角形的
形状
对象,而不是手工绘制三角形(每行使用单独的
绘制
语句)。多边形
足以创建三角形

与其更改JFrame的绘制,不如使用自定义绘制的面板并将其添加到框架中

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");
        }
    }
}        

这应该可以完成任务。这不是最好的选择,也不是性能最好的选择,但它确实有效

// The main class which makes the frame and adds the necessary stuff
class Mainclass {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CPanel panel = new CPanel();

        // I like colours
        panel.setBackground(new Color(255,255,255,255));
        frame.add(panel);

        frame.addMouseListener(new Mouse(panel));

        frame.setVisible(true);
    }
}

// The MouseListener which checks if the mouse is clicked
class Mouse implements MouseListener {

    CPanel panel;

    public Mouse(CPanel panel) {
        this.panel=panel;
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        int x=e.getX();
        int y=e.getY();

        boolean inside=false;

        ArrayList<Integer> Ys = panel.coordinates.get(x);
        if(Ys != null) {
            if(panel.coordinates.get(x).contains(y)) {
                inside=true;
            }
        }

        if(inside) {
            System.out.println("You clicked in the triangle");
        } else {
            System.out.println("You clicked out of the triangle");
        }

    }

    @Override   public void mousePressed  (MouseEvent e) {}
    @Override   public void mouseReleased (MouseEvent e) {}
    @Override   public void mouseEntered  (MouseEvent e) {}
    @Override   public void mouseExited   (MouseEvent e) {}
}

// The panel
class CPanel extends JPanel {

    public int minX=100;
    public int maxX=200;
    public int minY=100;
    public int maxY=200;

    // All the pixels in the triangle
    HashMap<Integer, ArrayList<Integer>> coordinates = new HashMap<Integer, ArrayList<Integer>>();


    public void paintComponent(Graphics G) {
        super.paintComponent(G);

        /** For that one downvoter: I made it G2D, even though it makes no difference **/
        Graphics2D g = (Graphics2D) G;

        // Drawing a centered triangle
        int xCen=(int)Math.round((minX+maxX)/2.0);

        // I like colours
        g.setColor(new Color(0,0,255,128));

        for(int y=0; y<=maxY-minY; y++) {

            int x0=xCen-y;
            int x1=xCen+y;

            int y0=y+minY;

            for(int x=x0; x<=x1; x++) {
                // Adding all pixels in this row to 'coordinates'
                ArrayList<Integer> Ys = coordinates.get(x);

                if(Ys==null) {
                    coordinates.put(x, new ArrayList<Integer>());
                    Ys = coordinates.get(x);
                }

                Ys.add(y0);
                coordinates.put(x, Ys);
            }

            // Draw the row
            g.drawLine(x0,y0,x1,y0);
        }

        // Output the coordinates for debugging purposes
        System.out.println(coordinates);
    }
}
//制作框架并添加必要内容的主类
类主类{
公共静态void main(字符串[]args){
JFrame=新JFrame();
框架。设置尺寸(400400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CPanel面板=新CPanel();
//我喜欢颜色
面板.立根背景(新颜色(255255));
框架。添加(面板);
frame.addMouseListener(新鼠标(面板));
frame.setVisible(true);
}
}
//用于检查鼠标是否被单击的鼠标侦听器
类Mouse实现MouseListener{
CPanel小组;
公用鼠标(CPanel面板){
这个面板=面板;
}
@凌驾
公共无效mouseClicked(MouseEvent e){
int x=e.getX();
int y=e.getY();
布尔内部=假;
ArrayList Ys=panel.coordinates.get(x);
如果(Ys!=null){
if(panel.coordinates.get(x).contains(y)){
内=真;
}
}
如果(内部){
System.out.println(“您在三角形中单击”);
}否则{
System.out.println(“您从三角形中单击了”);
}
}
@重写公共无效mousePressed(MouseEvent e){}
@重写公共无效MouseEvent(MouseEvent e){}
@重写公共void mouseEntered(MouseEvent e){}
@重写公共void mouseExited(MouseEvent e){}
}
//专家组
类CPanel扩展了JPanel{
公共整数minX=100;
公共整数maxX=200;
公共整数minY=100;
公共整数maxY=200;
//三角形中的所有像素
HashMap坐标=新的HashMap();
公共组件(图形G){
超级组件(G);
/**对于这一点:我把它做成了G2D,尽管它没有什么区别**/
图形2d g=(图形2d)g;
//画一个中心三角形
intxcen=(int)数学圆((minX+maxX)/2.0);
//我喜欢颜色
g、 setColor(新颜色(0,0255128));

对于(int y=0;y在@Mshink帮助下的最终代码

 public class Triangle_shape extends JFrame {
        /**
         * @param args the command line arguments
         */
        public Triangle_shape(){
            //Create and add trianglePanel

          //  setVisible(true);
        //    setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
      public static void main(String[] args) {

            // TODO code application logic here
          TrianglePanel t= new TrianglePanel();
        JFrame frame = new JFrame ();  
        final int FRAME_WIDTH = 500;  
        final int FRAME_HEIGHT = 500;

        frame.setSize (FRAME_WIDTH, FRAME_HEIGHT);           

        frame.setLayout(new BorderLayout());
          frame.add(t);
        //    frame.add(new TrianglePanel(), BorderLayout.CENTER);
            frame.pack();
            frame.repaint();
            frame.setTitle("A Test Frame");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setVisible(true);  

        }
       public static 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");
            }




        }
    }

这应该会有帮助:我没有得到它,但让我试试。然后我会问我是否有问题。谢谢你把三角形画成一个面板。如果我编码它,我会这样做。这些行有错误,没有addPoint函数…triangle.addPoint(150,200);triangle.addPoint(100,100);triangle.addPoint(200,100);啊,对了。我犯了一个愚蠢的错误。将三角形的静态类型更改为多边形。此代码工作正常。但现在如何将其添加到框架中?您能指导吗?三角形绘图代码,它是如何工作的?我需要制作大约27个三角形(Sierpinski三角形).loop不是画的好选择…@AdnanAli这是我说过两次的,它不是表现最好的,但是你知道怎么画了。