Java的code为简单的动画只能在Windows上运行 [英] Java Code for Simple Animation only runs on Windows

查看:124
本文介绍了Java的code为简单的动画只能在Windows上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个反弹球的动画这种简单的code例如:

In this simple code example for an animation of a bouncing ball:

import javax.swing.JApplet;
import javax.swing.JFrame;
import java.awt.*;

public class GraphicsMovement extends JApplet
{
public static void pause()
{
    try {
        Thread.sleep(10);
        } catch(InterruptedException e) {
          }
}

public static void main(String args[])
{
    JApplet example = new GraphicsMovement();
    JFrame frame = new JFrame("Movement");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(example);
    frame.setSize(new Dimension(500,300));       //Sets the dimensions of panel to appear when run
    frame.setVisible(true);
}

  public void paint (Graphics page)
  {
 int width = getWidth();    // width = the width of the panel which appears when run
 int height = getHeight();  // height = the height of the panel which appears when run.

//Changes background color to a blueish color
page.setColor(new Color (140,214,225));
page.fillRect(0,0,width,height);
for(int i = 0; i <= 5; i++)
{
    for (int j = 0; j <= 100; j++)
    {
        page.setColor(Color.YELLOW);
        page.fillOval(100,55 + j,100,100);  //draws a yellow oval
        pause();
        page.setColor(new Color (140,214,225));
        page.fillOval(100,55 + j,100,100);  //draws a blueish oval over the yellow oval
    }
    for (int k = 100; k >= 0; k--)
    {
        page.setColor(Color.YELLOW);
        page.fillOval(100,55 + k,100,100);  //draws a yellow oval
        pause();
        if (k != 0)
        {
            page.setColor(new Color (140,214,225));  //draws a blueish oval over the yellow oval
            page.fillOval(100,55 + k,100,100);
        }
    }
}
 }
 }

动画绘制精美,并运行(使用JCreator中)在Windows机器上,但无论是的IntelliJ或者Eclipse编译在Mac OS X将不会运行。尝试了两种不同的OS X的机器,都将以此为球和背景(经过漫长的等待),但不会与动画进行。

The animation is drawn fine and runs on a Windows machine (using JCreator), but will not run on Mac OS X compiled with either IntelliJ or Eclipse. Tried on two different OS X machines, and both will draw the ball and background (after a long wait) but will not proceed with the animation.

是否有某种特定平台的code在这里,我很想念?
谢谢!

Is there some sort of platform-specific code in here that I am missing? Thanks!

推荐答案

永远不要做任何油漆任何可能阻止或可能引发重画请求方法。

您应该总是叫 super.paintXxx ,这些方法都在后台做了大量的工作,通常是好了很多,那么你就可以了。

You should always call super.paintXxx, these methods do a lot of work in the background, usually a lot better then you can.

您不应该需要(极少数情况下),从顶层容器延伸,比如 JApplet的的JFrame 。要创建一个自定义的容器(如的JP​​anel )的更好,添加组件,它(或执行您的风俗画)。除了双缓冲支持,您还可以获得灵活的部署选择。

You shouldn't need to (very rare cases) extend from top level containers, like JApplet or JFrame. You are better of creating a custom container (such as JPanel) and add your components to it (or perform your custom painting). Apart from the double buffering support, you also gain flexibility in deployment choices.

不要欺骗自己,你不用管了烤漆工艺,它是由重绘管理员做出这些决定,但是,您可以鼓励来更新。这将导致最痛的时候人们开始动画播放。

Don't kid yourself, you don't control the paint process, it's up to the repaint manager to make those decisions, you can however, "encourage" it to update. This causes the most pain when people start playing with animation.

您应该修改动画的状态事件指派线程的(EDT)的影响之外,或在你的情况,出边油漆上下文。

You should modify the "state" of the animation outside of the influence of the Event Dispatching Thread (EDT) or in your case, out side the paint context.

您的问题是很简单的,一个简单的 javax.swing.Timer中的将解决这个问题。更复杂的动画,可能需要一个动漫主题。

You problem is simple enough that a simple javax.swing.Timer will solve it. More complex animations might require a "animation" thread.

public class GraphicsMovement extends JApplet {

    @Override
    public void init() {
        setLayout(new BorderLayout());
        add(new AnimatedPane());
    }

    @Override
    public void start() {
    }

    public class AnimatedPane extends JPanel {

        private Timer timer;
        private boolean colorSwitch = false;

        private int yOffset = 0;
        private int direction = 1;

        public AnimatedPane() {
            timer = new Timer(10, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
//                    colorSwitch = !colorSwitch;
                    yOffset += direction;
                    if (yOffset > 100) {
                        direction = -1;
                        yOffset = 100;
                    } else if (yOffset < 0){
                        direction = 1;
                        yOffset = 0;
                    }
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();

            setBackground(new Color(140, 214, 225));
        }

        @Override
        protected void paintComponent(Graphics page) {
            super.paintComponent(page);
            int width = getWidth();    // width = the width of the panel which appears when run
            int height = getHeight();  // height = the height of the panel which appears when run.

            if (colorSwitch) {
                page.setColor(new Color(140, 214, 225));
            } else {
                page.setColor(Color.YELLOW);
            }
            page.fillOval(100, 55 + yOffset, 100, 100);  //draws a yellow oval
        }
    }
}

我关心的10毫秒延迟,这足以要我去面对一个合适的:P

I'm concerned with the "delay" of 10 milliseconds, it's enough to want me to face a fit :P

您可能会发现通过阅读...

You might find reading through...

  • Concurrency
  • Concurrency in Swing
  • How to Use Swing Timers
  • Performing Custom Painting
  • Painting in AWT and Swing

有些兴趣

这篇关于Java的code为简单的动画只能在Windows上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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