动态绘制

时间:2017-10-04 03:09:18

标签: java swing java-2d

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

到目前为止我只用了一个三角形,但我想动态制作超过1个三角形。

我怎么能这样做?我只是在寻求任何帮助,需要建议。

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 个答案:

答案 0 :(得分:1)

public class Triangle extends JPanel { 

此类不应扩展JComponent JPanel

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