Double Buffering Doesn't Help My Flickering

B

Bryan R. Meyer

Hello Everyone,

I am practicing some Java animation, and I thought I'd start with an
Applet that displays scrolling headlines. Everything works, except, I
have the dreaded flickering problem. I've overridden the update()
method, and I have implemented double buffering, but my text still
flickers as it moves. Ideally, the user is able to change the scroll
rate. If the scroll rate is high, flickering is minimized. But if
the user decides on a slower rate, there is more flickering. You can
see my applet at: http://www.bryanrmeyer.net/Java/jHeadliner.html
I've included the source code below. Can anyone offer suggestions?

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


public class jHeadliner extends Applet {

int num_headlines, scroll_interval, xPos, yPos;
Color bg_color, text_color;
String[] headlineStorage;
String currentHeadline;
ScrollHeadlines scrollingThread;
Image doublebufImg;
Graphics doublebufGraphics;


public void init() {
getParameters();
currentHeadline = "";
this.setBackground(bg_color);
doublebufImg = createImage(this.getWidth(), this.getHeight());
doublebufGraphics = doublebufImg.getGraphics();
} //End of init method


public void start() {
scrollingThread = new
ScrollHeadlines(this,num_headlines,headlineStorage);
scrollingThread.start();
} //End of start method


public void stop() {

} //End of stop method


public void getParameters() {
String tempString;
int tempInt;

tempString = getParameter("num_headlines");
num_headlines = Integer.parseInt(tempString);
headlineStorage = new String[num_headlines];

tempString = getParameter("scroll_interval");
scroll_interval = Integer.parseInt(tempString);
//The scroll interval should be converted from seconds to
milliseconds.
scroll_interval = scroll_interval*1000;

tempString = getParameter("bg_color");
tempInt = Integer.valueOf(tempString, 16).intValue();
bg_color = new Color(tempInt);

tempString = getParameter("text_color");
tempInt = Integer.valueOf(tempString, 16).intValue();
text_color = new Color(tempInt);

for(int i=0;i<num_headlines;i++) {
headlineStorage = getParameter("headline" + (i+1));
}
} //End of getParameters method


public void paint(Graphics g) {
g.drawImage(doublebufImg, 0, 0, null);
} //End of paint method


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

public void drawHeadline(String hl, int x, int y) {
currentHeadline = hl;
xPos = x;
yPos = y;
doublebufGraphics.setColor(Color.black);
doublebufGraphics.fillRect(0,0,this.getWidth(),this.getHeight());
doublebufGraphics.setColor(text_color);
doublebufGraphics.drawString(currentHeadline, xPos, yPos);
repaint();
} //End of drawHeadline method

} //End of jHeadliner class



class ScrollHeadlines extends Thread {
boolean firstScroll;
int lastHeadline, nextHeadline, num_headlines;
String[] headlineStorage;
jHeadliner mainProgram;

public ScrollHeadlines(jHeadliner mp, int nh, String[] hs) {
super();
mainProgram = mp;
num_headlines = nh;
headlineStorage = hs;
lastHeadline = 0;
nextHeadline = 1;
firstScroll = true;
} //End of ScrollHeadlines constructor

public void run() {
String tempString;
FontMetrics fontTraits;
int xFinalPos, yFinalPos, yCurrentPos;

while(this!=null) {
if(firstScroll) {
tempString = headlineStorage[nextHeadline-1];
fontTraits = mainProgram.getFontMetrics(mainProgram.getFont());
xFinalPos = (mainProgram.getWidth()-fontTraits.stringWidth(tempString))/2;
yFinalPos = (mainProgram.getHeight()-fontTraits.getHeight())/2 +
fontTraits.getAscent();
yCurrentPos = mainProgram.getHeight() + fontTraits.getAscent();
while(yCurrentPos!=yFinalPos) {
yCurrentPos = yCurrentPos-1;
mainProgram.drawHeadline(tempString, xFinalPos, yCurrentPos);
try {
this.sleep(50);
}
catch(InterruptedException ie) {
System.out.println(ie);
}
}
firstScroll = false;
}

try {
this.sleep(5000);
}
catch(InterruptedException ie) {
System.out.println(ie);
}
}
} //End of run method

} //End of ScrollHeadlines class


Thanks,
Bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top