Java的:安全动画与Swing [英] Java: Safe Animations with Swing

查看:187
本文介绍了Java的:安全动画与Swing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个使用的JFrame,JPanel中,JLabel中其他各种Swing组件的程序。

我想要做的是专用于这个动画单独的JPanel创建2D动画。因此,我将重写的paintComponent(图形G)的方法。

我有经验,使动画与循环+线程,但我听到的线程不与摆动安全的。

由于这个原因,它是安全的,我做的动画与使用Runnable接口的?如果不是我应该使用(例如定时器),并请就如何最好地利用它(或一个网页的链接)的一个小例子。

编辑:

感谢杰夫,我将使用定时器来创建动画。对于这个问题,未来的观众,这里是在大约5分钟的快速程序I codeD,对不起脏code。

我也加了一些快速的意见。

 进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。
类JFrameAnimation扩展JFrame中实现的ActionListener
{    JPanel的面板;
    定时器定时器;
    INT X,Y;    公共JFrameAnimation()
    {
        超();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        定时器=新定时器(15本); // @第一个参数是延迟(以毫秒为单位),因此该动画每15毫秒更新一次。该延迟更短,更快的动画。
        //该类iplements的ActionListener,而且那里的动画变量被更新。计时器每经过15毫秒之后,一个ActionEvent。    }
    公共无效的run()
    {        面板=新JPanel()
        {
            公共无效的paintComponent(图形G)//将JPanel的paint方法我们覆盖。
            {
                g.setColor(Color.white);
                g.fillRect(0,0,500,500); //设置面板的背景(在这种情况下,白);
                //g.fillRect(-1 + X,-1 + Y,50,50); //这是抹去的黑色遗体的动画。 (未使用,因为背景总是重绘。
                g.setColor(Color.black);
                g.fillRect(0 + X,0 + Y,50,50); //这是动画。            }        }
        ;
        panel.set preferredSize(新尺寸(500,500)); //设置面板尺寸        的getContentPane()。新增(面板); //添加面板框架。
        包();
        调用setVisible(真);
        timer.start(); //这将启动动画。
    }
    公共无效的actionPerformed(ActionEvent的五)
    {
        X ++;
        ÿ++;
        如果(X == 250)
            timer.stop(); //这将停止动画一旦达到一定的距离。
        panel.repaint(); //这是再画动画(注意:没有这将无法工作)。
        panel.revalidate(); //这是没有必要的,我喜欢有它,以防万一。    }
    公共静态无效的主要(字串[] args)
    {
        新JFrameAnimation().RUN(); //开始我们的新的应用程序。
    }
}


解决方案

吉米,我想你是误会线程在Swing中是如何工作的。您必须使用所谓的事件指派线程做在Swing组件更新任何特定的线程(少数特殊情况例外,我不会在这里讨论)。您可以使用摆动计时器设置重复任务到事件调度线程上运行。看到如何使用摇摆定时器这个例子。 <一href=\"http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html\">http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html

您也应该阅读了在事件指派线程,以便您了解它在Swing <地方href=\"http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html\">http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

Java也提供了多种方法来与在的SwingUtilities 类摇摆,特别是的invokeLater 和工作 invokeAndWait 将事件调度线程上运行code。

I am creating a program that uses JFrame, JPanel, JLabel and all other sorts of swing components.

What I want to do is create a 2D animation on a separate JPanel that is dedicated to this animation. So I will be overriding the paintComponent (Graphics g) method.

I have experience making animations with for loops + Threads, but I am hearing that threads are not safe with swing.

Due to this, is it safe for me to make an animation with the use of the Runnable interface? If not what shall I use (e.g. Timer) and please give a small example on how to best use it (or a link to a web page).

EDIT:

Thanks to Jeff, I will be using Timer to create the animation. For future viewers of this question, here is a quick program I coded in about 5 minutes, excuse the dirty code.

I have also added some quick comments.

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


class JFrameAnimation extends JFrame implements ActionListener
{

    JPanel panel;
    Timer timer;
    int x, y;

    public JFrameAnimation ()
    {
        super ();
        setDefaultCloseOperation (EXIT_ON_CLOSE);
        timer = new Timer (15, this); //@ First param is the delay (in milliseconds) therefore this animation is updated every 15 ms. The shorter the delay, the faster the animation.
        //This class iplements ActionListener, and that is where the animation variables are updated. Timer passes an ActionEvent after each 15 ms.

    }


    public void run ()
    {

        panel = new JPanel ()
        {
            public void paintComponent (Graphics g)  //The JPanel paint method we are overriding.
            {
                g.setColor (Color.white);
                g.fillRect (0, 0, 500, 500); //Setting panel background (white in this case);
                //g.fillRect (-1 + x, -1 + y, 50, 50);  //This is to erase the black remains of the animation. (not used because the background is always redrawn.
                g.setColor (Color.black);
                g.fillRect (0 + x, 0 + y, 50, 50); //This is the animation.

            }

        }
        ;
        panel.setPreferredSize (new Dimension (500, 500)); //Setting the panel size

        getContentPane ().add (panel); //Adding panel to frame.
        pack ();
        setVisible (true);
        timer.start (); //This starts the animation.
    }


    public void actionPerformed (ActionEvent e)
    {
        x++;
        y++;
        if (x == 250)
            timer.stop (); //This stops the animation once it reaches a certain distance.
        panel.repaint ();  //This is what paints the animation again (IMPORTANT: won't work without this).
        panel.revalidate (); //This isn't necessary, I like having it just in case.

    }


    public static void main (String[] args)
    {
        new JFrameAnimation ().run (); //Start our new application.
    }
}

解决方案

Jimmy, I think you are misunderstanding how threads work in Swing. You must use a specific thread called the Event Dispatch Thread to do any updating on swing components (with a few specific exceptions I won't discuss here). You can use a swing timer to set a recurring task to run on the event dispatch thread. See this example of how to use Swing timers. http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html

You should also read up on the Event Dispatch Thread so you understand its place in Swing http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

Java also provides a variety of methods for working with Swing in the SwingUtilities class, notably invokeLater and invokeAndWait which will run code on the event dispatch thread.

这篇关于Java的:安全动画与Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆