Java 动态绘图

Java 动态绘图,java,swing,java-2d,Java,Swing,Java 2d,我想创建一个程序,用户点击窗口,第一次按下鼠标创建一个三角形对象,其中p[0](Point2D.Float变量)具有从鼠标按下位置设置的值,然后绘制一个小点,第二次按下鼠标创建一个点(p[1])从第二次鼠标按下的位置,然后从第一次按下的点1到第二次按下的点2绘制一条线。第三次按下创建第三个点(p[2]),然后从按下鼠标的所有3个点绘制一条线-创建一个三角形 到目前为止,我只做了一个三角形,但我想动态地制作多个三角形 我怎么能做到呢?我只是在寻求任何帮助,需要的建议 import javax.sw

我想创建一个程序,用户点击窗口,第一次按下鼠标创建一个三角形对象,其中p[0](Point2D.Float变量)具有从鼠标按下位置设置的值,然后绘制一个小点,第二次按下鼠标创建一个点(p[1])从第二次鼠标按下的位置,然后从第一次按下的点1到第二次按下的点2绘制一条线。第三次按下创建第三个点(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) {
    }
}
}
此类不应像
JPanel
那样扩展
JComponent


相反,它应该提供一个
draw(Graphics)
方法,在需要时绘制三角形。然后存储这些
三角形
对象的列表(例如
ArrayList
),并将其放在同时绘制所有对象的自定义绘制
JPanel
的范围内。自定义绘制的面板还应返回首选尺寸的合理尺寸(考虑要绘制的整个三角形列表的边界)。

如果我有错误,很抱歉,JPanel没有提供绘制方法,或者我正在考虑paintComponent。三角形类如何在不扩展JPanel的情况下提供绘图(图形)方法。对于ArrayList,我会将其实现到我的自定义JPanel类中?哦,天哪,我爱你。它工作得很好。非常感谢你。
public class Triangle extends JPanel {