简体   繁体   English

如何重新运行paint方法,使JPanel具有动画效果?

[英]How do I re run the paint method so the JPanel is animated?

I think I need to put some code where the comment is (or maybe use non static method but I am not sure). 我想我需要在注释所在的地方放置一些代码(或者可以使用非静态方法,但我不确定)。 The main method creates the window and then starts the graphics method. main方法创建窗口,然后启动图形方法。 I would like the blue square to flash. 我希望蓝色方块闪烁。

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class paintTest extends JPanel{
private static JFrame theWindow = new JFrame("Window");
static boolean blueSqr = false;

public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.fillRect(10, 10, 10, 10);

    if(blueSqr){
        g.setColor(Color.BLUE);
        g.fillRect(10, 10, 10, 10);
    }
}

public static void main(String[] args){
    createWindow();
    theWindow.getContentPane().add(new paintTest());
    while(true){
        blueSqr = false;

        System.out.println("off");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}

        blueSqr = true;
        // Needs something here
        System.out.println("on");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
}

public static void createWindow(){
    theWindow.setSize(500, 500);
    theWindow.setLocationRelativeTo(null);
    theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theWindow.setVisible(true);
}
}

Any help would be really good. 任何帮助都是非常好的。

Use a Swing Timer to call repaint() . 使用Swing Timer调用repaint() Also, override paintComponent() in a JPanel , rather than paint() . 另外,在JPanel重写paintComponent() ,而不要重写paint()

Something like this: 像这样:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintTest extends JPanel{

    boolean blueSqr = false;

    PaintTest() {
        setPreferredSize(new Dimension(100,25));
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                blueSqr = !blueSqr;
                repaint();
            }
        };
        Timer timer = new Timer(1000,al);
        timer.start();
    }

    public void paintComponent(Graphics g) {
        Color c = (blueSqr ? Color.BLUE : Color.RED);
        g.setColor(c);
        g.fillRect(10, 10, 10, 10);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame theWindow = new JFrame("Window");
                theWindow.getContentPane().add(new PaintTest());
                createWindow(theWindow);
            }
        });
    }

    public static void createWindow(JFrame theWindow){
        theWindow.pack();
        theWindow.setLocationByPlatform(true);
        theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theWindow.setVisible(true);
    }
}

There were other improvements I could not be bothered documenting (code speaks louder than words). 还有其他我无法打扰的改进(代码胜于雄辩)。 If you have any questions (check the docs first, then) ask. 如果您有任何疑问(请先检查文档,然后再问)。

your issues are 你的问题是

1) by calling Thread.sleep(int) in the Swing related code, never do that, for delaying in Swing (there are lots of topics about why not use sleep in programing languages ...) use Swing Timer 1)通过在Swing相关代码中调用Thread.sleep(int) ,永远不要这样做,因为延迟了Swing(有很多关于为什么不以编程语言使用sleep的话题……)使用Swing计时器

2) your JPanel doesn't returns any XxxSize 2)您的JPanel不返回任何XxxSize

3) for Swing use paintComponent() , only if you have got really important reasons then use method paint() more about repaint and animating Graphics in the 2D Graphics tutorial 3)对于Swing,请使用paintComponent() ,只有在您有非常重要的理由时,才应使用paint()方法更多有关2D图形教程中的重新绘制和对图形进行动画处理

4) Swing GUI should be built in the Event Dispatch Thread 4)Swing GUI应该内置在事件调度线程中

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

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