gpt4 book ai didi

Java - 使用paintComponent和多态性绘制形状

转载 作者:行者123 更新时间:2023-12-02 02:30:07 24 4
gpt4 key购买 nike

我有一项作业,必须在同一个面板上绘制 6 个形状。我尝试了几种方法,但找不到在同一面板上绘制形状的方法,而只能在不同的面板上绘制形状。

我有形状类(class):

public abstract class MyShape extends JPanel
public abstract class MyBoundedShape extends MyShape
public class MyOval extends MyBoundedShape
public class MyRectangle extends MyBoundedShape
public class MyLine extends MyShape

在这些类中我没有编写paintComponent方法,但我已将其编写在不同的类中,该类接收形状数组作为属性:

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class DrawingShapes extends JPanel implements Cloneable{
private ArrayList<MyShape> Shapes;
public DrawingShapes(ArrayList<MyShape> Shapes){
this.Shapes=Shapes;
initilizePaintComponent(); //draws a frame for the paintComponent
}


@Override
public void paintComponent(Graphics g){
super.paintComponent(g);

for (int i = 0; i <Shapes.size(); i++) {
g.setColor(Shapes.get(i).get_color());
if (Shapes.get(i) instanceof MyRectangle){
if (((MyRectangle) Shapes.get(i)).get_isFilled()){
g.fillRect(Shapes.get(i).get_x1(),Shapes.get(i).get_y1(),
Shapes.get(i).get_width(),Shapes.get(i).get_height());
}
else
g.drawRect(Shapes.get(i).get_x1(), Shapes.get(i).get_y1(),
Shapes.get(i).get_width(), Shapes.get(i).get_height());
}
if (Shapes.get(i) instanceof MyOval){
if (((MyRectangle) Shapes.get(i)).get_isFilled()){
g.fillOval(Shapes.get(i).get_x1(),Shapes.get(i).get_y1(),
Shapes.get(i).get_width(),Shapes.get(i).get_height());
}
else
g.drawOval(Shapes.get(i).get_x1(), Shapes.get(i).get_y1(),
Shapes.get(i).get_width(), Shapes.get(i).get_height());

}
else
g.drawLine(Shapes.get(i).get_x1(), Shapes.get(i).get_y1(),
Shapes.get(i).get_width(), Shapes.get(i).get_height());

}

}
public void initilizePaintComponent(){
JFrame frame = new JFrame("Shapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
for (int i = 0; i < Shapes.size(); i++) {
frame.add(Shapes.get(i));
frame.setVisible(true);

}

}

}

我的问题是 PaintComponent 方法不起作用 - 程序不绘制单个形状

运行程序后,我得到一个名为“Shape”的空框架 - 框架确实可以工作,但没有形状。

为什么paintComponent不起作用?

谢谢!

最佳答案

这个:

public void initilizePaintComponent(){
JFrame frame = new JFrame("Shapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
for (int i = 0; i < Shapes.size(); i++) {
frame.add(Shapes.get(i));
frame.setVisible(true);
}
}

忽略 JFrame(和所有顶级窗口)默认使用的布局管理器 BorderLayout。虽然您可能向 JFrame 添加 Shapes.size() 组件,但只有最后一个可见,因为通过以默认方式添加它们,BorderLayout 会覆盖所有先前添加的组件以及最后添加的组件。

可能的解决方案:

  • 仅使用一个绘图JPanel并重写其paintComponent方法。
  • 仅将这一个绘图 JPanel 添加到 JFrame 的 BorderLayout.CENTER(默认位置)
  • 使您的 MyShape 类成为一个非 GUI 非组件逻辑类,该类的代码允许它由上面的单个绘图 JPanel 进行绘制。
  • 为此类提供一个方法,例如绘图 JPanel 将在其 PaintComponent 中调用的 public void draw(Graphics g)
  • 让绘图 JPanel 在其 PaintComponent 方法内的 for 循环中迭代 MyShape 对象列表,并在循环内调用 draw(g) 方法。

有关此答案的更多详细信息,请考虑创建并发布有效的 MCVE程序与你的问题。

关于Java - 使用paintComponent和多态性绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com