gpt4 book ai didi

java - JApplet 动画未运行

转载 作者:行者123 更新时间:2023-12-02 07:12:33 28 4
gpt4 key购买 nike

所以我正在尝试研究简单的动画和虚拟物理等等。我正在尝试制作一个球的动画,使其随着时间的推移慢慢变大。我这里的代码与我在《Java For Dummies》书中的代码几乎完全相同,除了以下几件事:去掉小程序大小的常量(this.setSize(500, 500) 与this.setSize(WIDTH, HEIGHT) 并先前声明 WIDTH 和 HEIGHT)。更改很简单,不会影响程序。 (我知道,因为我在学校学过 Java 类(class))。不管怎样,我从 Applet 开始,但我无法让程序运行过去的两次迭代。在绘制函数中,我有一个 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);
}
}

最佳答案

/*
* 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 在内部调用事件调度线程内的 actionPerformed 方法,以便更新 UI。

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

关于java - JApplet 动画未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15330914/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com