重绘线程不会重绘内部类JPanel

时间:2019-09-13 08:44:57

标签: java swing repaint

我想在秋千上制作一些下雨程序,但是由于某些原因,我无法从另一个班级重新粉刷面板。这次我尝试为面板使用内部类,但是似乎无法从另一个类/线程重新粉刷它。有人知道为什么吗?

sscce:

import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class UI extends JFrame {

    public static void main(String[] args) {
        UI myProgram = new UI();
        myProgram.setVisible(true);
    }

    public UI() {
        this.setSize(new Dimension(500,300));
        this.setBackground(Color.WHITE);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        UserPanel p = new UserPanel(this);
    }

    public class UserPanel extends JPanel implements ActionListener {

        private Timer time = new Timer(1, this);
        private UI myFrame;

        public UserPanel(UI myFrame) {
            this.myFrame = myFrame;
            this.setSize(myFrame.getSize());

            time.start();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.println("painting");
            g.setColor(Color.BLACK);
            g.fillRect(this.getWidth()/2, this.getHeight()/2, 50,50);
        }
    }
}

UI类(带有内部类JPanel):

package Rain;

import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;

public class UI extends JFrame {


    public UI() {
        this.setSize(new Dimension(500,300));
        this.setBackground(Color.WHITE);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        UserPanel p = new UserPanel(this);
    }


    private class UserPanel extends JPanel implements ActionListener {

        private Timer time = new Timer(1, this);
        private UI myFrame;
        private ArrayList<Raindrop> rain = new ArrayList<Raindrop>();
        private static final int AMOUNT = 50;
        private Random rand = new Random();

        public UserPanel(UI myFrame) {
            this.myFrame = myFrame;
            this.setSize(myFrame.getSize());

            for(int i = 0; i < AMOUNT; i++) {
                createRain();
            }
            new Painter(this);
            time.start();
        }

        public void createRain() {
            float distance = rand.nextFloat() * 90 + 10;
            int x = rand.nextInt(this.getWidth());
            int y = 100;

            rain.add(new Raindrop(distance,x,y));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("tick");
            for(Raindrop r : rain) {
                r.fall();
            }
        }

        public void paintComponent(Graphics g) {
            System.out.println("painting");
            g.setColor(this.getBackground());
            g.fillRect(0,0,this.getWidth(),this.getHeight());
            for(Raindrop r : rain) {
                r.draw(g);
            }
        }

    }

}

画家:

package Rain;

import javax.swing.JPanel;

public class Painter extends Thread {

    private JPanel p;

    public Painter(JPanel p) {
        this.p = p;
        this.start();
    }

    public void run() {
        while(true) {
            System.out.println("trying to paint..");
            p.repaint();
        }
    }

}

控制台输出:

  

试图绘画..

     

勾选

     

试图绘画..

     

勾选

     

...

预期输出:

  

试图绘画..

     

绘画

     

勾选

     

试图绘画..

     

...

该线程确实起作用,但是它从未在面板中调用paintComponent(Graphics g)函数

1 个答案:

答案 0 :(得分:0)

所有Swing应用程序必须在自己的线程上运行,称为EDT。 (希望您通过调用SwingUtilities#invokelater方法来启动应用程序)。因此,在事件调度线程之外重新绘制组件确实是一个糟糕的想法。无需创建Type: 'AWS::SecretsManager::Secret' Properties: Description: 'Description' GenerateSecretString: SecretStringTemplate: '{"username": "username"}' GenerateStringKey: 'password' PasswordLength: 40 ExcludeCharacters: '"@/\' ,而是在new Thread的动作监听器中重新绘制组件,因为它将在EDT中运行。

javax.swing.Timer

此外,当您使用@Override public void actionPerformed(ActionEvent e) { System.out.println("tick"); for(Raindrop r : rain) { r.fall(); } repaint(); //repaint in EDT } @Override方法时,请始终先调用paintComponent;

super.paintComponent(g)

在SSCCE之后更新

要绘制一个组件,它必须有一个父对象。您public void paintComponent(Graphics g) { super.paintComponent(g);//let component get painted normally System.out.println("painting"); g.setColor(this.getBackground()); g.fillRect(0,0,this.getWidth(),this.getHeight()); for(Raindrop r : rain) { r.draw(g); } } ,但从未将其添加到框架中:

UserPanel p = new UserPanel(this);

完整的SSCCE:

UserPanel p = new UserPanel(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p);

请不要忽略public class UI extends JFrame { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { //Run in EDT UI myProgram = new UI(); myProgram.setVisible(true); }); } public UI() { super("title");//call super for frame this.setSize(new Dimension(500, 300)); this.setBackground(Color.WHITE); this.setDefaultCloseOperation(EXIT_ON_CLOSE); UserPanel p = new UserPanel(this); //Use border layout to make p fit the whole frame getContentPane().setLayout(new BorderLayout()); getContentPane().add(p, BorderLayout.CENTER); } public class UserPanel extends JPanel implements ActionListener { private Timer time = new Timer(1, this); private UI myFrame; public UserPanel(UI myFrame) { this.myFrame = myFrame; this.setSize(myFrame.getSize()); time.start(); } @Override public void actionPerformed(ActionEvent e) { repaint(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("painting"); g.setColor(Color.BLACK); g.fillRect(this.getWidth() / 2, this.getHeight() / 2, 50, 50); } } }