Solved

java applet flickering and can't refresh ...

Posted on 2004-04-13
7
314 Views
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
<html><body>
<applet code="Fun.class" width=300 height=300></applet>
</body></html>
---------------------------------------------------------------------------------------------------
Fun.java
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() {
            setBackground(Color.black);
            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) {
            animation.start();
            }
      }

      public void paint(Graphics g) {
            backGround.setColor(Color.black);
            backGround.fillRect(0,0,300,300);
            backGround.setColor(Color.yellow);
            backGround.fillRect(0,0,90,90);
            backGround.fillRect(250,0,40,190);
            backGround.fillRect(80,110,100,20); // hidden rectangle
            backGround.setColor(Color.blue);
            backGround.fillRect(80,200,220,90);
            backGround.fillRect(100,10,90,80);
            backGround.setColor(Color.lightGray);
            backGround.fillRect(locx,locy,width,height);
            backGround.setColor(Color.red);
            backGround.fillRect(200,0,45,45);
            backGround.fillRect(0,100,70,200);
            backGround.setColor(Color.magenta);
            backGround.fillRect(200,55,60,135);
            g.drawImage(offScreen,0,0,this);
      }
      public void update(Graphics g){
            g.clipRect(70,90,130,110);
            paint(g);
      }
// update the center rectangle
      void updateRectangle() {
            switch (state) {
                  case DOWN:
                        locy += 2;
                        if (locy >= 110) {
                              state = UP;
                        }
                        break;
                  case UP:
                        locy -= 2;
                        if (locy <= 90) {
                              state = RIGHT;
                        }
                        break;
                  case RIGHT:
                        locx += 2;
                        if (locx >= 90) {
                              state = LEFT;
                        }
                        break;
                  case LEFT:
                        locx -= 2;
                        if (locx <= 70) {
                              state = DOWN;
                        }
                        break;
            }
      }

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

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

}

0
Comment
Question by:frey50
7 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 80 total points
ID: 10814304
Hi frey50,

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

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10814312
2. override the repaint method:

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

Author Comment

by:frey50
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 :(

0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 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.
0
 
LVL 9

Expert Comment

by:OBCT
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 57
groupNoAdj 7 86
Java asynchronous logging 4 50
arguments to jar 5 14
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now