【问题标题】:JApplet Animation Not RunningJApplet 动画未运行
【发布时间】:2013-03-14 18:56:25
【问题描述】:

所以我试图进入简单的动画和虚拟物理等等。我正在尝试为一个球设置动画,以便它随着时间的推移慢慢增长。我这里的代码与我的 Java For Dummies 书中的代码几乎完全相同,除了以下几点:去掉小程序大小的常量 (this.setSize(500, 500) vs this.setSize(WIDTH, HEIGHT) 并在前面声明 WIDTH 和 HEIGHT)。更改很简单,不会影响程序。 (我会知道,因为我在学校上过 Java 课程)。无论如何,我从 Applets 开始,我无法让程序运行两次迭代。在绘制函数中,我有一个 System.out.println(d) 来检查椭圆的直径增长了多少倍。但是我看到的唯一输出是“21”然后是“22”。小程序继续通过小程序查看器运行,但是即使它应该继续增长,也不会打印任何其他内容。有谁知道怎么了? 作为旁注,我应该提到我正在使用 NetBeans 7.2 并选择“运行文件”来运行它。

package GraphicsTesting;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
import java.util.concurrent.*;

public class Main extends JApplet
{
    private PaintSurface canvas;

    @Override
    public void init()
    {
        this.setSize(500,500);
        canvas = new PaintSurface();
        this.add(canvas, BorderLayout.CENTER);
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
        executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS);
    }
}

class AnimationThread implements Runnable
{
    JApplet c;

    public AnimationThread(JApplet C)
    {
        this.c = c;
    }

    public void run()
    {
        c.repaint();
    }
}

class PaintSurface extends JComponent
{
    int d = 20;
    @Override
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint
                (RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        d+=1;
        System.out.println(d);//This is to test
        Shape ball = new Ellipse2D.Float(200, 200, d, d);
        g2.setColor(Color.RED);
        g2.fill(ball);
    }
}

【问题讨论】:

    标签: java japplet


    【解决方案1】:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication3;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import javax.swing.Timer;
    import javax.swing.JApplet;
    import javax.swing.JComponent;
    
    public class Main extends JApplet {
    
      private PaintSurface canvas;
      private Timer timer;
    
      @Override
      public void init() {
        this.setSize(500, 500);
        canvas = new PaintSurface();
        this.add(canvas, BorderLayout.CENTER);
    //    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    //    executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS);
        timer = new Timer(20, new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            canvas.repaint();
          }
        });
        timer.start();
      }
    }
    
    class PaintSurface extends JComponent {
    
      int d = 20;
    
      @Override
      public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        d += 1;
        System.out.println(d);//This is to test
        Shape ball = new Ellipse2D.Float(0, 0, d, d);
        g2.setColor(Color.RED);
        g2.fill(ball);
      }
    }
    

    您在不是Event Dispatch Thread 的线程上调用 repaint() 所以 UI 没有更新。还有其他方法可以做到这一点,但在内部 javax.swing.Timer 会调用 Event Dispatch Thread 中的 actionPerformed 方法,以便更新 UI。

    更新:您可以使用 java webstart 看到正在运行的小程序:https://tetris-battle-bot.googlecode.com/files/launch.jnlp

    【讨论】:

    • 我还要补充一点,在paint 方法中更新游戏状态是一个非常糟糕的主意,因为paint 方法可以独立更新。另外,建议调用super.paint,事实上,应该鼓励OP不要从顶级容器扩展并使用JPanel....或类似的东西;)
    【解决方案2】:

    上面的答案确实有效。但是,查看您的原始代码时,您会发现一个小小的误解,似乎你们都没有发现。在动画线程的构造函数中,您将JApplet C 作为参数而不是JApplet c。澄清一下,您不小心将c 大写。 C 的大写导致您设置 this.c = c 基本上将其分配给自己。根本不需要重写整个代码。

    【讨论】:

    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    相关资源
    最近更新 更多