【问题标题】:Java - Drawing Shapes with paintComponent and PolymorphismJava - 使用paintComponent和多态性绘制形状
【发布时间】:2017-11-11 22:23:35
【问题描述】:

我有一个任务,我必须在同一个面板上绘制 6 个形状。 我尝试了几件事,但我找不到在同一个面板上绘制形状的方法,但只能在不同的面板上。

我有 Shapes 类:

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 不起作用?

谢谢!

【问题讨论】:

  • 请注意,当询问“为什么这段代码不起作用”时,您几乎必须创建并发布一个有效的minimal reproducible example。否则我们只能猜测。

标签: java swing polymorphism paintcomponent


【解决方案1】:

这个:

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 绘制。
  • 给这个类一个方法,比如public void draw(Graphics g),绘图 JPanel 将在它的paintComponent 中调用
  • 让绘图 JPanel 在其 paintComponent 方法中的 for 循环中遍历 MyShape 对象列表,在循环中调用 draw(g) 方法。

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

【讨论】:

  • 感谢您的回答。如果我理解正确,我会在每次迭代中创建一个新的 JPanel?我会在 draw 方法中写什么 - 例如 drawOval() 方法?
  • 我的问题始终是单个 JPanel。我可以在面板上绘图,当我必须为绘图组合类时问题就开始了。
  • @Alan:同样,如果您需要进一步的帮助,请创建并发布有效的minimal reproducible example。有关如何执行此操作的更多信息,请查看链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2020-11-23
相关资源
最近更新 更多