我可以使用 getComponents() 而不是保留组件的 ArrayList 吗?
Can I use getComponents() instead of keeping an ArrayList of components?
我有一个应用程序,用户可以在其中通过单击位置 (x,y) 来动态添加形状,比方说圆形(为简单起见)。我一直在保留一个 ArrayList of Circles,以便通过 paintComponent() 方法将它们重新添加到 JPanel,但是我怀疑这可能是多余的,因为保留了一个幕后数组,可以通过内置的 getComponents() 方法。我对吗?我的代码的关键部分如下:
public class DrawingPanel extends JPanel implements Constants {
Point point;
Figure figure;
public DrawingPanel() {
point = new Point(0, 0);
figure = new Circle(point, defaultSize);
setLayout(null);
setBackground(Color.white);
setPreferredSize(new Dimension(450, 450));
addMouseListener(new ActionHandler());
}
public void paintComponent(Graphics page) {
super.paintComponent(page);
add(figure);
}
private class ActionHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
Point point = e.getPoint();
System.out.println("Mouse pressed at (" + e.getX() + ", " + e.getY() + ")");
String figureState = itemHandler.getFigureState();
String figureActionState = itemHandler.getFigureActionState();
if (figureActionState.equals("None")) {
switch (figureState) {
case "Circle" -> {
figure = new Circle(point, defaultSize);
figure.setBounds((int) point.getX(), (int) point.getY(), 50, 50);
figure.addMouseListener(new ActionHandler());
}
case "Square" -> {
figure = new Rect(point, defaultSize);
figure.setBounds((int) point.getX(), (int) point.getY(), 50, 50);
figure.addMouseListener(new ActionHandler());
}
case "Cross" -> {
figure = new Cross(point, defaultSize);
figure.setBounds((int) point.getX(), (int) point.getY(), 50, 50);
figure.addMouseListener(new ActionHandler());
}
}
}
repaint();
}
}
}
public class Figure extends JComponent {
Point position;
Dimension size;
public Figure(Point position, Dimension size) {
this.position = position;
this.size = size;
}
public void paintComponent(Graphics page) {
super.paintComponent(page);
}
}
public interface Constants {
ItemHandler itemHandler = new ItemHandler();
Dimension defaultSize = new Dimension(50, 50);
}
您有整体设计问题:
绘画方法不应改变 class 的状态。绘制方法应该只绘制 class 的当前状态。因此,您不应在 paintComponent() 方法中向面板添加组件。
我看不出保留这两个 ArrayList 的理由。组件应该知道如何绘制自身并包含绘制自身的所有必要信息。
也许您可以不使用实际组件而只绘制 Shape
。查看 。它保留了所有要绘制的 Shapes
的 ArrayList。此 ArrayList 用于标识要拖动的 Shape
。您需要用现有的鼠标处理逻辑替换拖动逻辑。
我有一个应用程序,用户可以在其中通过单击位置 (x,y) 来动态添加形状,比方说圆形(为简单起见)。我一直在保留一个 ArrayList of Circles,以便通过 paintComponent() 方法将它们重新添加到 JPanel,但是我怀疑这可能是多余的,因为保留了一个幕后数组,可以通过内置的 getComponents() 方法。我对吗?我的代码的关键部分如下:
public class DrawingPanel extends JPanel implements Constants {
Point point;
Figure figure;
public DrawingPanel() {
point = new Point(0, 0);
figure = new Circle(point, defaultSize);
setLayout(null);
setBackground(Color.white);
setPreferredSize(new Dimension(450, 450));
addMouseListener(new ActionHandler());
}
public void paintComponent(Graphics page) {
super.paintComponent(page);
add(figure);
}
private class ActionHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
Point point = e.getPoint();
System.out.println("Mouse pressed at (" + e.getX() + ", " + e.getY() + ")");
String figureState = itemHandler.getFigureState();
String figureActionState = itemHandler.getFigureActionState();
if (figureActionState.equals("None")) {
switch (figureState) {
case "Circle" -> {
figure = new Circle(point, defaultSize);
figure.setBounds((int) point.getX(), (int) point.getY(), 50, 50);
figure.addMouseListener(new ActionHandler());
}
case "Square" -> {
figure = new Rect(point, defaultSize);
figure.setBounds((int) point.getX(), (int) point.getY(), 50, 50);
figure.addMouseListener(new ActionHandler());
}
case "Cross" -> {
figure = new Cross(point, defaultSize);
figure.setBounds((int) point.getX(), (int) point.getY(), 50, 50);
figure.addMouseListener(new ActionHandler());
}
}
}
repaint();
}
}
}
public class Figure extends JComponent {
Point position;
Dimension size;
public Figure(Point position, Dimension size) {
this.position = position;
this.size = size;
}
public void paintComponent(Graphics page) {
super.paintComponent(page);
}
}
public interface Constants {
ItemHandler itemHandler = new ItemHandler();
Dimension defaultSize = new Dimension(50, 50);
}
您有整体设计问题:
绘画方法不应改变 class 的状态。绘制方法应该只绘制 class 的当前状态。因此,您不应在 paintComponent() 方法中向面板添加组件。
我看不出保留这两个 ArrayList 的理由。组件应该知道如何绘制自身并包含绘制自身的所有必要信息。
也许您可以不使用实际组件而只绘制 Shape
。查看 Shapes
的 ArrayList。此 ArrayList 用于标识要拖动的 Shape
。您需要用现有的鼠标处理逻辑替换拖动逻辑。