代码之家  ›  专栏  ›  技术社区  ›  Arhuro

动态绘制

  •  1
  • Arhuro  · 技术社区  · 7 年前

    我想创建一个程序,用户点击窗口,第一次鼠标按下创建一个三角形对象,其中p[0](一个点2D。浮点变量)有一个从鼠标按下位置设置的值,然后绘制一个小点,第二次按下从第二次鼠标按下的位置创建一个点(p[1]),然后从第一次按下的点到第二次按下的点二绘制一条线。第三次按下创建第三个点(p[2]),然后从按下鼠标的所有3个点绘制一条线-创建一个三角形。

    到目前为止,我只做了一个三角形,但我想动态地制作多个三角形。

    import javax.swing.JFrame;
    
      public class Main {
        public static void main(String[] args){
    
        Window window = new Window();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(700,500);
        window.setLocation(400,100);
        window.setTitle("Draw");
        window.initialize();
        window.setVisible(true);
      }
    }
    

    三角形类

     import java.awt.Color;
     import java.awt.Graphics;
     import java.awt.Graphics2D;
     import java.awt.Point;
     import java.awt.geom.Ellipse2D;
     import java.awt.geom.Point2D;
     import javax.swing.JPanel;
     public class Triangle extends JPanel {
      private Point2D.Float[] p = new Point2D.Float[3];
      private Color color;
    
    public Triangle(int x, int y, int index){
         p[index] = new Point2D.Float(x,y);
    }
    public void setColor(Color c){
        color = c;
    }
    public Color getColor(){
        return color;
    }
    public void setPoint(int x, int y, int index){
        p[index] = new Point2D.Float(x,y);
    }
    public void draw(Graphics2D g){  
        g.setColor(color);
        if(p[0] != null && p[1] == null && p[2] == null){
            g.fill(new Ellipse2D.Float((int)p[0].x,(int)p[0].y,3,3));
        }else if(p[0] != null && p[1] != null && p[2] == null){
            g.drawLine((int)p[0].x, (int)p[0].y, (int)p[1].x,(int)p[1].y);
        }else if(p[0] != null && p[1] != null && p[2] != null){
            g.drawLine((int)p[0].x, (int)p[0].y, (int)p[1].x,(int)p[1].y);
            g.drawLine((int)p[0].x, (int)p[0].y, (int)p[2].x,(int)p[2].y);
            g.drawLine((int)p[1].x, (int)p[1].y, (int)p[2].x,(int)p[2].y);
        }
        repaint();
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        draw(g2);
     }
    }
    

    窗口类

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.geom.Point2D;
    import java.util.ArrayList;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    
    public class Window extends JFrame {
    private JRadioButton redRadioButton, blueRadioButton, greenRadioButton, 
    blackRadioButton;
    private Container contentPane;
    private Triangle triangle;
    private JPanel colorPanel;
    private JPanel clearPanel;
    private JButton clearButton;
    private ButtonGroup buttonGroup;
    private int index = 0;
    private int mousePressed = 0;
    private Color c;
    public void initialize(){
        contentPane = getContentPane();
        buttonGroup = new ButtonGroup();
        colorPanel = new JPanel();
        clearPanel = new JPanel();
        clearButton = new JButton("CLEAR");
        triangle = new Triangle();
        triangle.addMouseListener(new MouseGuide());
        clearButton.setFocusable(false);
        ButtonListener buttonListener = new ButtonListener();
    
        clearButton.addActionListener(buttonListener);
        redRadioButton = new JRadioButton("RED");
        redRadioButton.setFocusable(false);
        redRadioButton.setSelected(true);
        redRadioButton.addActionListener(buttonListener);
        blueRadioButton = new JRadioButton("BLUE");
        blueRadioButton.setFocusable(false);
        blueRadioButton.addActionListener(buttonListener);
        greenRadioButton = new JRadioButton("GREEN");
        greenRadioButton.setFocusable(false);
        greenRadioButton.addActionListener(buttonListener);
        blackRadioButton = new JRadioButton("BLACK");
        blackRadioButton.setFocusable(false);
        blackRadioButton.addActionListener(buttonListener);
    
        buttonGroup.add(redRadioButton);
        buttonGroup.add(blueRadioButton);
        buttonGroup.add(greenRadioButton);
        buttonGroup.add(blackRadioButton);
        colorPanel.add(redRadioButton);
        colorPanel.add(blueRadioButton);
        colorPanel.add(greenRadioButton);
        colorPanel.add(blackRadioButton);
        clearPanel.add(clearButton);
        colorPanel.setBorder(new TitledBorder(new EtchedBorder(), "Pick a Color"));
        contentPane.add("North", colorPanel);
        contentPane.add("Center", triangle);
        contentPane.add("South",clearPanel);
        System.out.println(index);
    }
    public void setShapeColor(){
         if(redRadioButton.isSelected()){
                c = Color.RED;
                contentPane.revalidate();
                contentPane.repaint();
            }else if(blueRadioButton.isSelected()){
                c = Color.BLUE;
                triangle.setColor(c);
                contentPane.revalidate();
                contentPane.repaint();
            }else if(greenRadioButton.isSelected()){
                c = Color.GREEN;
                contentPane.revalidate();
                contentPane.repaint();
            }else if(blackRadioButton.isSelected()){
                c = Color.BLACK;
                contentPane.revalidate();
                contentPane.repaint();
            }
    }
    public class ButtonListener implements ActionListener{
    
        @Override
        public void actionPerformed(ActionEvent ae) {
             setShapeColor();
             Object source = ae.getSource();
             if(source == clearButton){
                mousePressed = 0;
                index = 0;
             }
        }
    
    }
    public class MouseGuide implements MouseListener{
    
        @Override
        public void mouseClicked(MouseEvent me) {
        }
    
        @Override
        public void mousePressed(MouseEvent me) {
            mousePressed++;
            if(mousePressed == 1){            
                triangle.setPoint(me.getX(),me.getY(),index);
                contentPane.validate();
                contentPane.repaint();
            }else if(mousePressed == 2){
                index++;
                triangle.setPoint(me.getX(), me.getY(), index);
                contentPane.validate();
                 contentPane.repaint();
            }else if(mousePressed == 3){
                index++;
                triangle.setPoint(me.getX(),me.getY(),index);
                contentPane.validate();
                contentPane.repaint();
                mousePressed = 0;
                index = 0;
            }
            System.out.println("Mouse Pressed: " + mousePressed + ", [" + triangle.getPoint(0) + "]," + "[" + triangle.getPoint(1) + "]" + "[" + triangle.getPoint(2) + "]");
        }
    
        @Override
        public void mouseReleased(MouseEvent me) {
        }
    
        @Override
        public void mouseEntered(MouseEvent me) {
        }
    
        @Override
        public void mouseExited(MouseEvent me) {
        }
    }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrew Thompson    7 年前
    public class Triangle extends JPanel { 
    

    此类不应扩展 JComponent 喜欢 JPanel .

    draw(Graphics) 在需要时绘制三角形的方法。然后存储一个列表(例如。 ArrayList )其中 Triangle 对象,并将其置于自定义绘制的范围内 面板