Java Applet Thread Animation

假如想象 提交于 2020-01-11 06:45:08

问题


I am gone through some of code java applet and animation, i write the following code :

import java.applet.*;
import java.awt.*;

/*<applet code="AppletDemo" width = 200 height = 100></applet>
*/

public class AppletDemo extends Applet implements Runnable
{
    String msg = "Text Animating from right to left...";
    Thread t = null;
    int state;
    boolean stopFlag;
    int msgX = 200;
    String s;
    boolean diff;

    public void init()
    {
        setBackground(Color.cyan);
        setForeground(Color.black);
    }
    public void start()
    {
        t = new Thread(this);
        stopFlag = false;
        t.start();
        s = "abc";
         diff = s.equalsIgnoreCase("abc");
    }

    public void run()
    {
        while (true)
        {
            try{
            if(msgX>=-150)
                msgX--;
            else
                msgX =200;

            Thread.sleep(10);
            repaint();
               }
               catch(Exception e)
               {}
        }
    }
    public void paint(Graphics g)
    {
        g.drawString(msg,msgX,20);
        showStatus(diff+"Text at "+msgX+",20");
    }

}

What is happening is that when i put Thread.sleep(100), it works fine but when i try to animate faster that is Thread.sleep(10) it starts flickering , i couldn't understand what is happening can anyone help.


回答1:


  • Don't directly paint over top level container. Use JPanel to paint on it.
  • Don't use Thread.sleep(). It's better to use Swing Timer for animation.
  • Override paintComponent() method of JPanel for custom painting.
  • Don't forget to call super.paintComponent() inside overridden paintComponent method.

Instead of infinite loop try with Swing Timer.

Please have a look at How to Use Swing Timers

sample code:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

/*
 * <applet code="AppletDemo" width = 200 height = 100></applet>
 */

public class AppletDemo extends Applet {
    String msg = "Text Animating from right to left...";
    int state;
    boolean stopFlag;
    int msgX = 200;
    String s;
    boolean diff;
    JPanel panel;

    public void init() {
        setBackground(Color.cyan);
        setForeground(Color.black);
        panel = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponents(g);
                g.drawString(msg, msgX, 20);
                showStatus(diff + "Text at " + msgX + ",20");
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 40);
            }
        };
        add(panel);

        int delay = 10; // milliseconds
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (msgX >= -150)
                    msgX--;
                else
                    msgX = 200;
                repaint();
            }
        };
        Timer timer = new Timer(delay, taskPerformer);
        timer.setRepeats(true);
        timer.start();
    }

}

Find a Sample code here




回答2:


Yes it will flicker. You will have to solve this problem using the concept of DoubleBuffering. It means that the image to be drawn is already buffered before its drawn on screen. It will remove the flickering effect.



来源:https://stackoverflow.com/questions/25092903/java-applet-thread-animation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!