File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes how to repaint but still keep the graphic that painted previously? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to repaint but still keep the graphic that painted previously?" Watch "how to repaint but still keep the graphic that painted previously?" New topic
Author

how to repaint but still keep the graphic that painted previously?

Linda Xu
Ranch Hand

Joined: Feb 14, 2000
Posts: 51
Hi,
I am doing program to draw a graphic that composed by several lines. I want to draw it step by step, that means each time the button is clicked, I add one more line to the picture and keep the lines drawed previously.
But each time when I draw, the lines drawed previously is erased, so I have to redraw all the lines and add the new line I really want to add. How to solve this problem and keep all the old lines ? (I already tried repaint() and update(g), doesn't work.)
Thanks!
zoe goddard
Ranch Hand

Joined: Dec 12, 2000
Posts: 74
I haven't actually tried it in Java, but I assume there is a way to do it.
Create a image buffer that you draw your lines into one at a time and then dump the image buffer to your drawing area each time.
Anh Nguyen
Greenhorn

Joined: Jan 24, 2001
Posts: 18
U should use the JComponent instead of Component . Just import javax.swing.* and make your class extends JFrame
EX code :

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;

public class LineSaver extends JFrame
{
private Timer timer;

public LineSaver()
{
super("Screen saver with 100s 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);
}
});
}
}
------------------

... No one can escape .......<BR><B>Heavenly Evil Knight</B>
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi Linda,
in case u want to use AWT only then u should override the update() method of your Frame with the following method.i.e. u add the following method to your LineSaver class as it is.


Now for the reasoning part ......
Let's see what goes behind the scene When we call a repaint() method .
first of all repaint calls the update(Graphics g) method. default implementation of upadate is that it first clears the background and then calls the paint method which finally does the drawing .
and hence due to the default implementation of the update method previous drawing gets erased.
if we override the update method as given above then it calls paint(g) directly and hence does not clear the earlier drawing.
In swing the update method does not clears the background and hence the solution as given by Anh works.
hope this helps
regards
deekasha

[This message has been edited by deekasha gunwant (edited January 29, 2001).]
[This message has been edited by deekasha gunwant (edited January 29, 2001).]
Anh Nguyen
Greenhorn

Joined: Jan 24, 2001
Posts: 18
Deekasha Gunwant is right . The reason I told you to use Swing just because the update method in JComponent is already overrided so that it only calls the paint method again w/o clear the background .
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to repaint but still keep the graphic that painted previously?