?
Solved

creating an applet with animation and static text.

Posted on 2005-04-01
5
Medium Priority
?
244 Views
Last Modified: 2010-07-27
I got this code off the web, (for animation) and all is well, but i'm trying to include a piece of static text now, but the text keeps flashing. I've read that this is a common problem but I don't know what to do.

I'm a java newbie so please help.

thanks.

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


public class NellieJREDetect extends Applet implements Runnable {
    int frameNumber = -1;
    int delay;
    Thread animatorThread;
    boolean frozen = false;
      Dimension offDimension;
    Image offImage;
    Graphics offGraphics;

    Image[] images;
    MediaTracker tracker;

    public void init() {
        String str;
        int fps = 10;
        //How many milliseconds between frames?
        str = getParameter("fps");
        try {
            if (str != null) {
                fps = Integer.parseInt(str);
            }
        } catch (Exception e) {}
        delay = (fps > 0) ? (1000 / fps) : 100;

        //Load all the images.
        images = new Image[4];
        tracker = new MediaTracker(this);
        for (int i = 1; i <= 4; i++) {
            images[i-1] = getImage(getCodeBase(),"img"+i+".gif");
            tracker.addImage(images[i-1], 0);
        }
    }

    public void start() {
        if (frozen) {
            //there's nothing happening here
            //don't do anything.
        } else {
            //Start animating!
            if (animatorThread == null) {
                animatorThread = new Thread(this);
            }
            animatorThread.start();
        }
    }

    public void stop() {
        //Stop the animating thread.
        animatorThread = null;

        //Get rid of the objects necessary for double buffering.
        offGraphics = null;
        offImage = null;
    }

    public boolean mouseDown(Event e, int x, int y) {
        if (frozen) {
            frozen = false;
            start();
        } else {
            frozen = true;

            //Instead of calling stop(), which destroys the
            //backbuffer, just stop the animating thread.
            animatorThread = null;
        }
        return true;
    }

    public void run() {
        try {
            //Start downloading the images. Wait until they're loaded
          //before requesting repaints.
            tracker.waitForAll();
        } catch (InterruptedException e) {}

        //Just to be nice, lower this thread's priority
        //so it can't interfere with other processing going on.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
 
        //Remember the starting time.
        long startTime = System.currentTimeMillis();

        //This is the animation loop.
        while (Thread.currentThread() == animatorThread) {
            //Advance the animation frame.
            frameNumber++;

            //Display it.
            repaint();

            //Delay depending on how far we are behind.
            try {
                startTime += delay;
                Thread.sleep(Math.max(0, startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    public void paint(Graphics g) {
            update(g);
    }

    public void update(Graphics g) {
        Dimension d = size();
             //If not all the images are loaded, just clear the background
        //and display a status string.
        if (!tracker.checkAll()) {
            g.clearRect(0, 0, d.width, d.height);
            g.drawString("Please wait...", 0, d.height/2);
        }

        //If all images are loaded, draw.
        else {
                    
            //Create the offscreen graphics context, if no good one exists.
            if ( (offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height) ) {
                offDimension = d;
                offImage = createImage(d.width, d.height);
                offGraphics = offImage.getGraphics();
                        
            }
            //Erase the previous image.
            offGraphics.setColor(getBackground());
            offGraphics.fillRect(0, 0, d.width, d.height);
            offGraphics.setColor(Color.black);
                  
            //Paint the frame into the image.
          offGraphics.drawImage(images[frameNumber % 4], 0, 0, this);

            //Paint the image onto the screen.
            g.drawImage(offImage, 0, 0, this);
            g.drawString("Hello there!", 60, 120);
                  
        }
            
    }
}
0
Comment
Question by:blue-genie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 13681192
Change:

            //Paint the frame into the image.
          offGraphics.drawImage(images[frameNumber % 4], 0, 0, this);

            //Paint the image onto the screen.
            g.drawImage(offImage, 0, 0, this);
            g.drawString("Hello there!", 60, 120);


to:

            //Paint the frame into the image.
            offGraphics.drawImage(images[frameNumber % 4], 0, 0, this);
            offGraphics.drawString("Hello there!", 60, 120);

            //Paint the image onto the screen.
            g.drawImage(offImage, 0, 0, this);
0
 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 13681207
What happens, is you build up the "offScreen" image, then paint it to the screen in one go with the line:

            g.drawImage(offImage, 0, 0, this);

as soon as you start doing anything else to g like

            g.drawString("Hello there!", 60, 120);

you will get flickering...

Always draw to the offscreen image, hen paint the image to the applet in one step :-)

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13681254
Did that work ok?

Hope so :-)

Good luck with it all!!

Tim
0
 
LVL 39

Author Comment

by:blue-genie
ID: 13681256
that works brilliantly thanks .

and especially thanks for the explanation.

:)

blu.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13681302
Glad I could help :-)

Good luck again!! :-)

Tim
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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