java - PaintComponent 中的 Object.draw 本身不起作用

标签 java swing paintcomponent

PaintComponent 不绘制图形。只是什么也没发生,干净的 Jframe 出现了。 我认为列表或我调用方法的方式有问题 List 与 Paint 组件在类中

public class Paint extends JPanel implements ActionListener {
     List<Figures> figuresList = new ArrayList<Figures>();
     Timer t = new Timer(5, this);

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    for (Figures figure : figuresList) {
        figure.drawItself(g, figure.getLocationX(), figure.getLocationY());
    }
    t.start();
}

@Override
public void actionPerformed(ActionEvent e) {

    {
        for (Figures figure : figuresList) {
            if (figure.getLocationX() < 0 || figure.getLocationX() > 540) {
                figure.setVelocityX(-figure.getVelocityX());
            }
            if (figure.getLocationY() < 0 || figure.getLocationX() > 220) {
                figure.setVelocityY(-figure.getVelocityY());
            }
            figure.setLocationX(figure.getLocationX()
                    + figure.getVelocityX());
            figure.setLocationY(figure.getLocationY()
                    + figure.getVelocityY());
        }
    }
    repaint();

}

并绘制自己:

public class Circle implements Figures {    
    public int locationX = 12;
    public int locationY = 12;
    public int velocityX =1;
    public int velocityY =1;


    public void drawItself(Graphics g, int locationX, int locationY){
        this.locationX = locationX;
        this.locationY = locationY;
        g.drawOval(locationX, locationY, 40, 40);  
        g.fillOval(locationX, locationY, 40, 40);
    }

主要:

public static void main(String[] args) {

    Circle c = new Circle();
    Quadrat q = new Quadrat();
    Paint p = new Paint();
    p.figuresList.add(c);
    p.figuresList.add(q);
    GUI.Configuration();


    }

图形用户界面

public class GUI {


    public static void Configuration(){
        JFrame frame = new JFrame("Figures Animation");
        frame.setSize(600,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new Paint();
        frame.getContentPane().add(BorderLayout.CENTER, panel);
    }

最佳答案

您可以在此处创建并添加一个 Paint 实例:

public class GUI {
    public static void Configuration(){
        JFrame frame = new JFrame("Figures Animation");
        frame.setSize(600,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new Paint(); // *** new Paint is here, but nothing is added
        frame.getContentPane().add(BorderLayout.CENTER, panel);
    }

但是没有添加任何有用的东西。所有重要的内容都添加到一个完全不同的 Paint JPanel 中,该面板永远不会显示:

public static void main(String[] args) {
    Circle c = new Circle();
    Quadrat q = new Quadrat();
    Paint p = new Paint();  // **** ANOTHER new Paint is here, and it gets goodies
    p.figuresList.add(c);
    p.figuresList.add(q);

    // but is never added to a JFrame and is never displayed.

    GUI.Configuration();
}

不要这样做。创建一个 Paint JPanel,仅一个,向其中添加重要组件,然后仅将该组件添加到JFrame 中。最重要的是,不要只是输入代码,在将程序提交给代码之前先思考和规划你的程序,这样你就不会看到这样的错误。

另外,不要从 PaintComponent 中启动计时器,也不要在那里创建 Circle。您可以在 PaintComponent 中绘制 Circle 实例,但在 Paint 构造函数中创建它并启动计时器。

关于java - PaintComponent 中的 Object.draw 本身不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34468056/

相关文章:

java - PaintComponent 内部的 if 语句使其不绘制

Java程序在eclipse中执行但不在终端中执行

JavaFX - 在表格 View 中移动列

java - JFrame、JPanel、paintComponent 如何

java - JComboBox还涵盖其他东西?

java - 如何设置 JToggleButton 的切换状态?

java - Xor 如何交换值?

java - 如何使用嵌套数组解析 JSON 到对象

java - 如何让 Java GUI 面板组件 hibernate ?

根据用于源和目标的路径复制文件/目录时出现 java.lang.NullPointerException 错误