0

我想要一个 Java swing 程序来用鼠标绘制不重叠的 n 边多边形、圆形和椭圆。

在用户创建之前,多边形不应添加最后一个点和第一个点本身。

4

1 回答 1

1

这是一个非常基本的例子

在此处输入图像描述

 public class DrawPolygon {

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

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

                     JFrame frame = new JFrame("Test");
                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     frame.setLayout(new BorderLayout());
                     frame.add(new PolyPane());
                     frame.pack();
                     frame.setLocationRelativeTo(null);
                     frame.setVisible(true);
                }

           });
      }

      public static class PolyPane extends JPanel {

           private MouseHandler mouseHandler;
           private Path2D currentShape;
           private List<Path2D> lstPloys;
           private Point lastPoint;
           private Point currentPoint;

           public PolyPane() {
                lstPloys = new ArrayList<>();
           }

           @Override
           public Dimension getPreferredSize() {
                return new Dimension(200, 200);
           }

           @Override
           public void addNotify() {
                super.addNotify();
                addMouseListener(getMouseHandler());
                addMouseMotionListener(getMouseHandler());
           }

           @Override
           public void removeNotify() {
                removeMouseListener(getMouseHandler());
                removeMouseMotionListener(getMouseHandler());
                super.removeNotify();
           }

           public MouseHandler getMouseHandler() {
                if (mouseHandler == null) {
                     mouseHandler = new MouseHandler();
                }
                return mouseHandler;
           }

           @Override
           protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                if (lastPoint != null) {
                     g2d.setColor(Color.RED);
                     g2d.fillOval(lastPoint.x - 2, lastPoint.y - 2, 4, 4);
                }
                if (currentShape != null) {
                     g2d.setColor(Color.RED);
                     g2d.draw(currentShape);
                     if (lastPoint != null && currentPoint != null) {
                          System.out.println(lastPoint + " - " + currentPoint);
                          g2d.setColor(new Color(255, 0, 0, 64));
                          g2d.draw(new Line2D.Float(lastPoint, currentPoint));
                     }
                }
                g2d.setColor(Color.BLACK);
                for (Shape shape : lstPloys) {
                     g2d.draw(shape);
                }
                g2d.dispose();
           }

           public class MouseHandler extends MouseAdapter {

                @Override
                public void mouseClicked(MouseEvent e) {
                     if (e.getButton() == MouseEvent.BUTTON1) {
                          if (e.getClickCount() == 1) {
                               Point p = e.getPoint();
                               lastPoint = p;
                               if (currentShape == null) {
                                    currentShape = new Path2D.Float();
                                    currentShape.moveTo(p.x, p.y);
                               } else {
                                    currentShape.lineTo(p.x, p.y);
                               }
                               repaint();
                          } else if (e.getClickCount() == 2) {
                               currentShape.closePath();
                               lstPloys.add(currentShape);
                               currentShape = null;
                               lastPoint = null;
                               repaint();
                          }
                     }
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                     if (currentShape != null) {
                          currentPoint = e.getPoint();
                          repaint();
                     } else {
                          currentPoint = null;
                     }
                }

           }

      }

 }

你会想花时间通读

因为这些将涵盖实现您想要做的事情所需了解的基础知识。

更新

我更新了示例,包括使用MosueMotionListener

于 2012-11-22T09:26:43.140 回答