所以我试图进入简单的动画和虚拟物理等等。我试图让球变形,以便随着时间的推移它慢慢增长。我在这里的代码与Java For Dummies书中的代码非常完全相同,除了一些例外情况之外,它包括:删除applet大小的常量(this.setSize(500,500)vs this.setSize(WIDTH,HEIGHT)并在之前声明WIDTH和HEIGHT)。这些变化很简单,不会影响该计划。 (我知道,因为我在学校学习了Java课程)。无论如何,我从这里开始使用Applet,我无法让程序运行两次迭代。在paint函数中,我有一个System.out.println(d)来检查椭圆直径增长的次数。然而,我看到的唯一输出是“21”然后是“22”。小程序继续通过applet查看器运行,但即使它应该继续增长,也不会打印任何其他内容。谁知道什么是错的? 作为旁注,我应该提到我正在使用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);
}
}
答案 0 :(得分:2)
/*
* 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
答案 1 :(得分:0)
上述答案确实有效。然而,看看你的原始代码有一点点误解,看来,你们两个都没有抓到。在动画线程的构造函数中,您将JApplet C
作为参数而不是JApplet c
。为了澄清,您意外地将c
大写。 C
的capitlization使您设置this.c = c
,基本上将其分配给自己。根本不需要重写整个代码。