Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reloading a text file and flicker

 
Donella Mangia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I overwrite the update() it keeps redrawing over what is there but doesn't clear previous text before changing it. If I comment out the overwriting of the update() it does everything write except it flickers.
Here is the code......
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.String;
public class Quotes extends java.applet.Applet implements Runnable{
Font f = new Font("TimesRoman", Font.PLAIN, 18);
Thread go;
String quo = "";
URL url;
DataInputStream stream;
int x;
int y;
Vector vecQuotes = new Vector();
public void init(){
setBackground(Color.black);
x = 5;
y = 5;
quo = "";
}

public void start(){
if((go == null)| |(!go.isAlive()))
{
go = new Thread(this);
}
go.start();
}
public void paint(Graphics g){
x=5;
y=5;
g.setFont(f);
g.setColor(Color.green);
try{
url = new URL(getDocumentBase(), "esquote.txt");
stream = new DataInputStream(url.openStream());
String s = "";
boolean eof = false;
while(!eof){
x = 5;
y = y + 15;
quo = stream.readLine();
StringTokenizer strQuo = new StringTokenizer(quo,",");
if(quo == null)
{
eof = true;
}
else{
boolean z = false;
while(strQuo.hasMoreElements())
{
if(z == false)
{
s = strQuo.nextToken();
g.drawString(s, x, y);
x = 130;
z = true;
}
else
{
s = strQuo.nextToken();
g.drawString(s, x, y);
x = x + 90;
}
}
}
}
stream.close();
}
catch(IOException e)
{}
}
public void run(){
while(Thread.currentThread() == go)
{
try{
repaint();
Thread.sleep(3000);
}
catch(InterruptedException e)
{}
}
}
public void stop(){
if((go != null) && (go.isAlive()))
{
go.stop();
}
}
public void destroy()
{
go = null;
}
//public void update(Graphics g)
//{
// paint(g);
//}
}

 
Brian Mahn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Donella
Go to the Javaranch Code Barn to see how to get rid of flicker. I'm not sure how it works myself, but it looks god in the applet.
hope this helps
 
Grant Crofton
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason it flickers when you don't override update() is that the original update() clears the screen before calling paint(). When you override it, the screen no longer gets cleared, so the original image remains.
You need to use double buffering, which I'm guessing that link explains.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic