在JComponent中存储形状

时间:2016-02-27 17:09:03

标签: java swing arraylist

我不确定如何使用一个类型形状的arraylist存储多个不同的形状。这是主要的。

public class A1 {
public static boolean rDraw = false;
public static boolean eDraw = false;
public static boolean lDraw = false;
public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton rect = new JButton("Rectangle");
    rect.addActionListener(new ActionListener (){
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==rect){
                rDraw = true;
                eDraw = false;
                lDraw = false;
            }
        }
    });
    JButton ellipse = new JButton("Ellipse");
    ellipse.addActionListener(new ActionListener (){
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==ellipse){
                rDraw = false;
                eDraw = true;
                lDraw = false;      
            }
        }
    });
    JButton edge = new JButton("Edge");
    edge.addActionListener(new ActionListener (){
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == edge){
                rDraw = false;
                eDraw = false;
                lDraw = true;       
            }
        }
    });
    JButton label = new JButton("Label");
    JTextField labelField = new JTextField(20);
    JPanel panel = new JPanel();
    panel.add(rect);
    panel.add(ellipse);
    panel.add(edge);
    panel.add(label);
    panel.add(labelField);
    frame.add(panel,BorderLayout.NORTH);
    JComponent canvas = new Canvas();
    frame.add(canvas,BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setTitle("Graph Draw");
    frame.setVisible(true);
    }
}

在我的画布中,我不确定如何使用drawShape?以及要在paintComponent中存储形状的内容。

public class Canvas extends JComponent {
private Point p,p2;
private Rectangle r;
private Ellipse2D ep;
private Line2D l;
ArrayList<Shape> shapes = new ArrayList<Shape>();

public Canvas(){
    class MyMouseListener implements MouseListener{
        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mousePressed(MouseEvent e) {
            if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
                p = e.getPoint();
                r = new Rectangle(p.x, p.y, p.x - p.x, p.y - p.y);  
            }
            else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
                p = e.getPoint();
                ep = new Ellipse2D.Double(p.x, p.y, p.x - p.x, p.y - p.y);
            }
            else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
                p = e.getPoint();
                l = new Line2D.Double(p.x, p.y, p.x - p.x, p.y - p.y);
            }
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
                if (r.width != 0 || r.height != 0)
                {
                    addRectangle(r);
                }

                r = null;
            }
            else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
                if (ep.getWidth() != 0 || ep.getHeight() != 0){
                    addEllipse(ep);
                }
                ep = null;
            }
            else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
                if (l.getP1() != null || l.getP2() != null){
                    addLine(l);
                }
                l = null;
            }
        }
    }
    class MyMouseMotionListener implements MouseMotionListener{
        @Override
        public void mouseDragged(MouseEvent e) {
            if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
                int x = Math.min(p.x, e.getX());
                int y = Math.min(p.y, e.getY());
                int width = Math.abs(p.x - e.getX());
                int height = Math.abs(p.y - e.getY());
                r.setBounds(x, y, width, height);
                repaint();
            }
            else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
                int x = Math.min(p.x, e.getX());
                int y = Math.min(p.y, e.getY());
                int width = Math.abs(p.x - e.getX());
                int height = Math.abs(p.y - e.getY());
                ep.setFrame(x, y, width, height);
                repaint();
            }
            else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
                p2 = e.getPoint();
                l.setLine(p, p2);
                repaint();
            }
        }
        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
    }
    MouseListener mListener = new MyMouseListener();
    addMouseListener(mListener);
    MouseMotionListener mmListener = new MyMouseMotionListener();
    addMouseMotionListener(mmListener);
}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    for (Shape shape:shapes){

    }
    if(A1.rDraw == true && A1.eDraw == false && A1.lDraw == false){
        g2d.draw(r);
    }
    else if (A1.rDraw == false && A1.eDraw == true && A1.lDraw == false){
        g2d.draw(ep);
    }
    else if (A1.rDraw == false && A1.eDraw == false && A1.lDraw == true){
        g2d.draw(l);
    }
}
public void addRectangle(Rectangle rect){
    if(r != null){
        Rectangle cR = new Rectangle(rect);
        shapes.add(cR);
        repaint();
    }
}
public void addEllipse(Ellipse2D ellipse){
    if (ep != null){
        Ellipse2D cEp = new Ellipse2D.Double(ellipse.getX(),ellipse.getY(),ellipse.getWidth(),ellipse.getHeight());
        shapes.add(cEp);
        repaint();
    }
}
public void addLine(Line2D line){
    if(l != null){
        Line2D cL = new Line2D.Double(line.getP1(), line.getP2());
        shapes.add(cL);
        repaint();
    }
}
}

1 个答案:

答案 0 :(得分:3)

你的paintComponent方法似乎做得太多而且太少:太少,因为它没有绘制Shapes,而且太多了,因为它试图用不应该的静态布尔字段做事。

  

“......以及要在paintComponent中存储形状的内容。”

这不应该是paintComponent方法的一部分。此方法用于在组件上绘制项目,仅此而已。它不应该用于程序逻辑。存储形状的代码应该在鼠标侦听器类型代码中,而不是在paintComponent中。

我认为paintComponent所需要的只是:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    for (Shape shape : shapes) {
        g2d.draw(shape); // !! added
    }
}

作为几个旁注:

  • 你的A1类中有太多静态代码,包括静态字段,并且大多数都应该是实例代码,并且main方法中的大多数代码都应该被删除并放入构造函数中。主要方法应该很小。
  • 您需要将Canvas类重命名为不同的东西,因为这会与核心Java类java.awt.Canvas创建潜在的名称冲突,并且如果您将此类移动到另一个包,可能会使其他人甚至编译器感到困惑在将来。
  • 您可以将MouseListener和MouseMotionListener代码组合到一个扩展MouseAdapter的类中。这将简化您的代码,并允许您避免发布空方法停止。
  • 由于您的绘图选择是互斥的,我会使用单个ButtonGroup中保存的JRadioButtons或JToggleButtons,以便用户可以轻松查看当前活动的选项,并选择一个按钮将关闭所有选择他人。