简体   繁体   English

Java Swing计时器不工作

[英]Java Swing Timer Not Working

I am trying to repaint my applet every second in order to show a race between a tortoise and a hare, but the swing timer in my applet doesn't seem to be working properly. 我试图每秒重新绘制我的小程序,以显示乌龟和野兔之间的竞赛,但我的小程序中的摇摆计时器似乎没有正常工作。 Any help or advice would be much appreciated! 任何帮助或建议将不胜感激!

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
import java.awt.Color;

public class Project2 extends Applet
{
int squaret = 1;
int squareh = 1; //initial location of tortoise and hare
int move;
String tmessage;
String hmessage;


Timer timer;

public void init()
{

    timer = new Timer(100, getNewValues); 
    timer.addActionListener(getNewValues);
    timer.start();



}

ActionListener getNewValues = new ActionListener() {

    public void actionPerformed(ActionEvent e) 
    {

        repaint();
    }
};

public void paint (Graphics g) 
{
    move = (int)(Math.random() * 10) + 1;

    if (move > 8)
    {   
        squaret -= 6;
        tmessage = "Tortoise slips!";

        if (squaret < 1)
            squaret = 1;

    }       
    else if (move > 6)
    {
        squaret += 1;
        tmessage = "Tortoise plods slowly along.";
        if (squaret > 49)
            squaret = 50;

        squareh -=2;
        hmessage = "Hare slips slightly.";
        if (squareh < 1)
            squareh = 1;

    }

    else if (move > 5)
    {
        squaret += 1;
        tmessage = "Tortoise plods slowly along.";
        if (squaret > 49)
            squaret = 50;

        squareh -=12;
        hmessage = "Hare makes a big slip.";
        if (squareh < 1)
            squareh = 1;

    }

    else if (move > 2)
    {
        squaret += 3;
        tmessage = "Tortoise plods along quickly.";
        if (squaret > 49)
            squaret = 50;

        squareh += 1;
        hmessage = "Hare makes a small hop.";
        if (squareh > 49)
            squareh = 50;

    }
    else 
    {
        squaret += 3;
        tmessage = "Tortoise plods along quickly.";
        if (squaret > 49)
            squaret = 50;

        squareh += 9;
        hmessage = "Hare makes a big hop.";
        if (squareh > 49)
            squareh = 50;
    }

    g.drawString("Start (Square 1)", 0, 70);
    g.drawString("Finish (Square 50)", 900, 70);


    //determine positions for each area
    //each box is ten wide and 150 tall

    final int WIDTH_OF_OVAL = 4;
    final int HEIGHT_OF_OVAL = 4;
    final int WIDTH_OF_SQUARE = 20;
    final int HEIGHT_OF_SQUARE = 20;
    g.setColor(Color.GREEN);
    g.fillOval(((WIDTH_OF_SQUARE - WIDTH_OF_OVAL) / 2) + WIDTH_OF_SQUARE * (squaret - 1), ((HEIGHT_OF_SQUARE - HEIGHT_OF_OVAL) / 2), WIDTH_OF_OVAL, HEIGHT_OF_OVAL);

    g.setColor(Color.YELLOW);
    g.fillOval(((WIDTH_OF_SQUARE - WIDTH_OF_OVAL) / 2) + WIDTH_OF_SQUARE * (squaret - 1), ((HEIGHT_OF_SQUARE - HEIGHT_OF_OVAL) / 2) + HEIGHT_OF_SQUARE, WIDTH_OF_OVAL, HEIGHT_OF_OVAL);

    //show messages
    g.setColor(Color.BLACK);
    g.drawString(tmessage, 10, 100);
    g.drawString(hmessage, 10, 120);

    g.drawLine(0, HEIGHT_OF_SQUARE, WIDTH_OF_SQUARE * 50, HEIGHT_OF_SQUARE); //draw horizontal middle line

    for (int i = 0; i < 50; i++) //draw vertical lines
    {
        int width = (i + 1) * WIDTH_OF_SQUARE;

        g.drawLine(width, 0, width, HEIGHT_OF_SQUARE * 2);
    }

    if (squaret > 49 && squareh > 49)
    {
        g.drawString("Tie!", 500, 60);
        timer.stop();
    }   
    else if (squaret > 49)
    {
        g.drawString("Turtle wins!", 500, 60);
        timer.stop();
    }
    else if (squareh > 49)
    {
        g.drawString("Hare wins!", 500, 60);
        timer.stop();
    }   
    else
    {

    }   

    update(g);


}

public static void main(String[] args) 
{

    Project2 panel = new Project2();
    JFrame application = new JFrame();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    application.add(panel);
    application.setSize(2600, 300);
    application.setVisible(true);

    }
}    

Don't call update() from paint() . 不要从paint()调用update() paint() update() will eventually trigger paint() and thus you are creating an endless loop of repaints which results in StackOverflowError . update()最终将触发paint() ,因此您将创建一个无限循环的重绘,从而导致StackOverflowError

Some other concerns: 其他一些问题:

Assuming that you want to create an applet I'm not sure what you wanted to achieve with main() method that executes it as a normal application. 假设你想要创建一个applet,我不确定你想用main()方法实现它作为普通应用程序执行它。 Also not clear why choose Applet and not JApplet . 也不清楚为什么选择Applet而不是JApplet

See Java Applets for more details and examples. 有关更多详细信息和示例,请参阅Java Applet In particular, see Methods for Milestones regarding the main . 特别是,请参阅有关main 方法的里程碑方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM