Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

reloading a text file and flicker

 
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);
//}
}

 
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
 
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.
 
Attractive, successful people love this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic