简体   繁体   English

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

[英]Java - Drawing Shapes with paintComponent and Polymorphism

I have an assignment in which I have to draw 6 shapes on the same Panel. 我有一个作业,必须在同一面板上绘制6个形状。 I have tried several things, but I couldn't find a way to draw the Shapes on the same panel, but only on diffrent Panels. 我已经尝试了几种方法,但是找不到在同一面板上绘制形状的方法,而只能在不同的面板上绘制。

I have classes for the 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

In these classes I have not written a paintComponent Method , but I have written it in a diffreent class, which recieves an Array of Shapes as an attribute: 在这些类中, 我没有编写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);

        }

    }

}

My Problem is that the paintComponent method does not work - The program does not draw a single shape . 我的问题是paintComponent方法不起作用-程序无法绘制单个形状

After running the program I get an empty Frame named "Shape" - The frame does work , but without Shapes. 运行该程序后,我得到一个名为“ Shape”的空框架- 该框架可以工作 ,但是没有Shapes。

Why doesn't the paintComponent work? 为什么paintComponent不起作用?

Thanks! 谢谢!

This: 这个:

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);
    }
}

ignores the layout manager that JFrame (and all top-level windows) uses by default, BorderLayout. 忽略默认情况下JFrame(和所有顶级窗口)使用的布局管理器BorderLayout。 While you may be adding Shapes.size() components to the JFrame, only the last one is visible, since by adding them in a default fashion, the BorderLayout covers all previously added components with the one added last. 当您可能在Shapes.size()组件中添加JFrame时,只有最后一个组件可见,因为以默认方式添加它们后,BorderLayout会覆盖所有先前添加的组件,最后添加一个组件。

Possible Solutions: 可能的解决方案:

  • Use only one drawing JPanel and override its paintComponent method. 仅使用一个绘图JPanel并覆盖其paintComponent方法。
  • Add only this one single drawing JPanel to the BorderLayout.CENTER (default position) of your JFrame 仅将此一张图形JPanel添加到JFrame的BorderLayout.CENTER(默认位置)
  • Make your MyShape class a non-GUI non-component logical class, one with code that allows it to be drawn by the single drawing JPanel above. 使您的MyShape类成为非GUI非组件逻辑类,该类具有允许通过上面的单个绘制JPanel绘制的代码。
  • Give this class a method, say public void draw(Graphics g) that the drawing JPanel will call within its paintComponent 给此类提供一个方法,例如,绘图JPanel将在其paintComponent中调用的public void draw(Graphics g)
  • Have the drawing JPanel iterate through a List of MyShape objects in a for-loop within its paintComponent method, calling the draw(g) method within the loop. 让绘图JPanel在其paintComponent方法内的for循环中迭代MyShape对象的列表,并在循环内调用draw(g)方法。

For more detail to this answer, do consider creating and posting a valid MCVE program with your question. 有关此答案的更多详细信息,请考虑使用您的问题创建并发布有效的MCVE程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM