• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

repaint() problem

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try to make a screen saver which will draw 100 lines, refresh, then draw another 100 in every second.
I don't know why it does not refresh the screen when I call repaint() in this program . Please help me .
<h6>public class LineSaver extends JFrame
{
private Timer timer;

public LineSaver()
{
super("Screen saver with 100 of random lines");

setSize(400,400);
show();

timer = new Timer(1000, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
repaint();
}
});
timer.start();
}

public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;

for (int i=0; i<100; i++)
{
g2d.setColor( new Color ((int)(Math.random()*256),
(int)(Math.random()*256),
(int)(Math.random()*256)));
g2d.setStroke( new BasicStroke((float)(Math.random()*10)));
g2d.draw( new Line2D.Double((int)(Math.random()*400),
(int)(Math.random()*400),
(int)(Math.random()*400),
(int)(Math.random()*400)));
}

}

public static void main (String [] agrs)
{
LineSaver app = new LineSaver();

app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
});
}
}</h6>
 
Anh Nguyen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't know how to align the code lines in order . Look like it just skip all the tabs and spaces .
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try calling invalidate() first and then repaint(). invalidate() will the container and all parents above it as needing to be laid out and, therefore, repainted.
Hope this helps,
Stella
 
Anh Nguyen
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not work :-( . But thanks anyway :-)
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Anh,
your program is running perfectly on my m/c . i.e. with jdk 1.3 on win NT 4.0


regards
deeksha
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you call repaint(), it doesn't necessarily repaint the screen streight away, it just puts in a request for it to be done at the next available opportunity.
Sometimes it helps if you pass a number, eg repaint( 50 ) - this is asking for a repaint within 50ms.
Sometimes it just doesn't seem to work at all - I guess this could be because the machine is too slow. Drawing graphics tends to be fairly slow in Java anyway.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anh,
Did you ever get this to work?
Specifically, where the screen clears. I am trying the same assignment and am unable to get the screen to clear before drawing the next 100 lines.
Either I get one pass through the 'for' loop getting me a screen with 100 lines and the program ends, or when I add a call to
repaint()as the last line in method paint I get an endless loop.
But how to clear the screen escapes me.
Any luck?
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just use super.paint(g); as the first statement in your paint method. It SHOULD work
 
reply
    Bookmark Topic Watch Topic
  • New Topic