java applet flickering and can't refresh ...

Posted on 2004-04-13
Medium Priority
Last Modified: 2013-11-23
I am learning about how to write a Java Applet. The following is my first applet example from a java book. I don't understand why -
1. no matter what I change and recompile the java class code, the applet runs exactly the same as the first time that I ran this applet.
2. How to make the applet runs without flickering?
Remark: The code is no longer working but it works before. I don't know what is going on. Anyone can help me?
HTML file
<applet code="Fun.class" width=300 height=300></applet>
import java.applet.*;
import java.awt.*;
public class Fun extends Applet implements Runnable {
      Thread animation;
      int locx,locy; // location of rectangle
      int width, height; // dimensions of rectangle
      static final byte UP = 0; // direction of motion
      static final byte DOWN = 1;
      static final byte LEFT = 2;
      static final byte RIGHT = 3;
      byte state; // state the rect is in
      Graphics backGround;
      Image offScreen;
// length of pausing interval
      static final int REFRESH_RATE = 100; // in ms

// applet methods:
      public void init() {
            locx = 80; // parameters of center rect
            locy = 100;
            width = 110;
            height = 90;
            state = UP;
            offScreen = createImage(width, height);
            backGround = offScreen.getGraphics();

      public void start() {
            animation = new Thread(this);
            if (animation != null) {

      public void paint(Graphics g) {
            backGround.fillRect(80,110,100,20); // hidden rectangle
      public void update(Graphics g){
// update the center rectangle
      void updateRectangle() {
            switch (state) {
                  case DOWN:
                        locy += 2;
                        if (locy >= 110) {
                              state = UP;
                  case UP:
                        locy -= 2;
                        if (locy <= 90) {
                              state = RIGHT;
                  case RIGHT:
                        locx += 2;
                        if (locx >= 90) {
                              state = LEFT;
                  case LEFT:
                        locx -= 2;
                        if (locx <= 70) {
                              state = DOWN;

      public void run() {
            while (true) {
                  try {
                        Thread.sleep (REFRESH_RATE);
                  } catch (Exception exc) { };

      public void stop() {
            if (animation != null) {
                  animation = null;


Question by:frey50
LVL 13

Accepted Solution

Webstorm earned 320 total points
ID: 10814304
Hi frey50,

1. clear the class cache :
      open the java console
      type 'x'
      reload your html page

LVL 13

Expert Comment

ID: 10814312
2. override the repaint method:

     public void repaint()
         Graphics g=getGraphics();
         if (g==null) super.repaint();
         else paint(g);

Author Comment

ID: 10815413
I added -
      public void repaint()
         Graphics g=getGraphics();
         if (g==null) super.repaint();
         else paint(g);
and cleared the class loader cache, but still not working :(

LVL 92

Assisted Solution

objects earned 320 total points
ID: 10817933
use double buffereing to remove the flicker.
ie. do you painting to an offscreen image, and then paint that image to the screen.

Expert Comment

ID: 10818074
I recently had the same problem.
Once I uploaded the applet to a server, the problem was no more :)

It might be something to do with memory on your computer but thats a guess so you might want to double check with someone else.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question