JavaI在形状Arraylist中添加了一个矩形,但该形状不会显示在面板上
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
@SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener, MouseListener {
private boolean drawLine = false;
private boolean drawRec = false;
private boolean drawOval = false;
private final JButton line;
private final JButton oval;
private final JButton rectangle;
private final JPanel buttonPanel;
public DrawStuff drawPanel = new DrawStuff();
public int x1;
public int x2;
public int y1;
public int y2;
public int click;
public GUI() {
super("Graphics IO");
this.click = 1;
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 3));
line = new JButton("Line");
line.addActionListener(this);
buttonPanel.add(line);
oval = new JButton("Oval");
oval.addActionListener(this);
buttonPanel.add(oval);
rectangle = new JButton("Rectangle");
rectangle.addActionListener(this);
buttonPanel.add(rectangle);
Container contentPane = this.getContentPane();
contentPane.add(buttonPanel, BorderLayout.SOUTH);
//add(drawPanel);
addMouseListener((MouseListener) this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == line) {
drawLine = true;
repaint();
} else if (source == oval) {
drawOval = true;
repaint();
} else if (source == rectangle) {
drawRec = true;
repaint();
}
}
public static void main(String[] args) {
GUI guiIO = new GUI();
}
class DrawStuff extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLUE);
ArrayList<Shape> shapes = new ArrayList<>();
if (drawLine) {
drawLine = false;
} else if (drawOval) {
//no clue how to add an oval
drawOval = false;
} else if (drawRec) {
Rectangle rec = new Rectangle(x1, y1,Math.abs(x2-x1) , Math.abs(y2-y1));
shapes.add(rec);
drawRec = false;
}
Graphics2D j = (Graphics2D)g;
shapes.stream().map((s) -> {
((Graphics2D) j).draw((Shape) s);
return s;
}).forEach((_item) -> {
repaint();
});
}
}
@Override
public void mousePressed(MouseEvent me) {
if (click == 1){
x1 = me.getX();
y1 = me.getY();
System.out.println(x1);
System.out.println(y1);
click = 2;
}else if (click == 2) {
x2 = me.getX();
y2 = me.getY();
System.out.println(x2);
System.out.println(y2);
click = 1;
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
}
好的,所以我必须制作一个程序,使用两个鼠标点击创建形状,然后能够导出/导入它们。我正在尝试使用arraylist,但我很难让它正常工作。我创建的矩形将不会显示在面板上。我做错了什么?请帮帮我
# 1 楼答案
让我们从一个事实开始:
DrawStuff
实际上并没有被添加到任何能够绘制它的东西中DrawStuff#paintComponent
应该决定更新shapes
{ActionListener
和MouseListener
应该做出这些决定(添加什么、在哪里以及修改什么),面板应该只绘制Shape
列表中的内容您也不应该从
paintComponent
中修改组件的状态。调用setBackground
之类的东西可能会设置重复的重绘请求,如果不是您的PC,这可能会破坏您的应用程序修改}作为实例字段,以便您可以更轻松地从
DrawStuff
,使其拥有自己的MouseListener
和方法,允许您的GUI
请求它创建新形状。将shapes
{DrawStuff
中进行管理类似于
比如说。你总是需要问自己“谁负责做什么”。在这种情况下
DrawStuff
面板实际上负责确定绘制某物的“位置”,但它需要关于“内容”的更多信息,然后根据这些信息它可以执行实际操作