绘制固定多边形的java程序

时间:2015-09-14 00:38:25

标签: java eclipse

我正在编写一个程序,用一定的点绘制一个固定的多边形,并告诉鼠标点是否在多边形的内部或外部。我不知道我需要在哪里移动代码,以便我可以修复这些错误。我很确定有些东西只是在错误的地方。这就是我现在所拥有的:

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Polygon;

public class Ch3PA extends JFrame {

    Polygon polygon = new Polygon();
    Point point = new Point(0, 0);
    boolean inside = false;
    private PaintPanel paintPanel = new PaintPanel();

    public PaintPanel()
    {
        add(paintPanel);

        static class PaintPanel extends JPanel
        {       
            polygon.addPoint(40, 20);
            polygon.addPoint(70, 40);
            polygon.addPoint(60, 80);
            polygon.addPoint(45, 45);
            polygon.addPoint(20, 60);

             @addMouseMotionListener(new MouseAdapter()
             {
                public void mouseMoved(MouseEvent e)
                {
                    point.x = e.getX();
                    point.y = e.getY();
                    if (polygon.contains(e.getX(), e.getY()))
                    {
                        inside = true;
                        repaint();
                    }
                    else 
                    {
                        inside = false;
                        repaint();              
                    }
                }
            });
        }
    }

    protected void paintComponent(Graphics g){
        super.paintComponents(g);
        g.drawPolygon(polygon);
        if (inside)
        {
            g.drawString(inside ? "Mouse point is in the polygon"    :          "Mouse point is not in the polygon", point.x, point.y);
            repaint();
        }               
    }

    public static void main(String[] args) {
        Ch3PA frame = new Ch3PA();
        frame.setTitle("Ch3PA");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);         
    }   
}

1 个答案:

答案 0 :(得分:2)

此...

public class Ch3PA extends JFrame {

    //...
    public PaintPanel()

没有任何意义。您已经定义了一个名为Ch3PA的类,但为其定义了一个名为PaintPanel的构造函数

然后在构造函数中定义一个类....

static class PaintPanel extends JPanel
{

    polygon.addPoint(40, 20);
    polygon.addPoint(70, 40);
    polygon.addPoint(60, 80);
    polygon.addPoint(45, 45);
    polygon.addPoint(20, 60);



 @addMouseMotionListener(new MouseAdapter()
 {
    public void mouseMoved(MouseEvent e)
    {
        point.x = e.getX();
        point.y = e.getY();

        if (polygon.contains(e.getX(), e.getY()))
        {
            inside = true;
            repaint();
        }
            else 
            {
                inside = false;
                repaint();

        }
    }

});

即使这是合法的,polygon.addPoint也行不通,因为你试图在可执行的上下文之外执行代码(方法,构造函数,static块)

@addMouseMotionListener是非法语法,您不应该使用@

不要在任何paint方法中调用repaint();,这将消耗你的CPU,永无止境重绘

您正在呼叫super.paintComponents< - 请注意s,这不是您正在寻找的方法。您应该调用super.paintComponent,但我怀疑您的IDE是合适的,并且不知道如何解决它。有@Override注释是如此有用,但我怀疑,鉴于所有其他问题,它确实会有所帮助

首先定义你PaintPanel,定义它的操作和职责......

public class PaintPanel extends JPanel {

    Polygon polygon = new Polygon();
    Point point = new Point(0, 0);
    boolean inside = false;

    public PaintPanel() {

        polygon.addPoint(40, 20);
        polygon.addPoint(70, 40);
        polygon.addPoint(60, 80);
        polygon.addPoint(45, 45);
        polygon.addPoint(20, 60);

        addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                point.x = e.getX();
                point.y = e.getY();

                if (polygon.contains(e.getX(), e.getY())) {
                    inside = true;
                    repaint();
                } else {
                    inside = false;
                    repaint();

                }
            }

        });
    }

    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        g.drawPolygon(polygon);

        if (inside) {
            g.drawString(inside ? "Mouse point is in the polygon" : "Mouse point is not in the polygon", point.x, point.y);
        }

    }

}

现在,将其添加到某个容器

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new PaintPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});

可运行的示例......

import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Ch3PA extends JFrame {

    public static void main(String[] args) {
        new Ch3PA();
    }

    public Ch3PA() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new PaintPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
    }

    public class PaintPanel extends JPanel {

        Polygon polygon = new Polygon();
        Point point = new Point(0, 0);
        boolean inside = false;

        public PaintPanel() {

            polygon.addPoint(40, 20);
            polygon.addPoint(70, 40);
            polygon.addPoint(60, 80);
            polygon.addPoint(45, 45);
            polygon.addPoint(20, 60);

            addMouseMotionListener(new MouseAdapter() {
                public void mouseMoved(MouseEvent e) {
                    point.x = e.getX();
                    point.y = e.getY();

                    if (polygon.contains(e.getX(), e.getY())) {
                        inside = true;
                        repaint();
                    } else {
                        inside = false;
                        repaint();

                    }
                }

            });
        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            g.drawPolygon(polygon);

            if (inside) {
                g.drawString(inside ? "Mouse point is in the polygon" : "Mouse point is not in the polygon", point.x, point.y);
            }

        }

    }
}