Paint and copy problem

Paint and copy problem

Post by Heph » Sat, 17 Jun 2006 16:16:55


Hey people,

I have a small problem with some paint and replaceing.

What I try to do:

I draw 1 line at the most right of a panel
next I have to make a image out of this line, move it 1 pixel to the
left.
then I draw a new line, the previous line should still be visible to
the left of the new line.
I then have to make a image out of these 2 lines, move it 1 pixel to
the left
etc.

Why I want to do it this way:

repaint of 100's of lines takes too much instead of just an image + new
line.

What I have tried so far:

public void paintComponent(Graphics g) {

super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;


bimage = (BufferedImage)this.createImage(seqNr,getHeight()); //where
seqNr is the amount of lines currently painted so far
g2d = (Graphics2D)bimage.getGraphics();
g2d.copyArea(x1, 0, 1, 0, -1, 0);
g2d.drawImage(bimage, 0, 0, null);


g2d.paintstuff //this actually paints the lines in the correct color,
size etc

}


the g2d.paintstuff is creating the lines out of a bunch of seperately
colored pixels.


I have tried replacing the image making etc to other parts but that
didnt work either.

My questions:

How can i make an image out of the selection of lines currently on
screen?
How do i move that 1 pixel and NOT lose it on repaint();?
Did i overlook a much simpler solution to what I am trying to do?



So it should look like this:

You have a increasing number of lines spawning from the rightside of
the panel and pusing older lines to the left.
most left = oldest.

greetings, D
 
 
 

Paint and copy problem

Post by Oliver Won » Sat, 17 Jun 2006 23:39:25


Why didn't the above code work?

- Oliver

 
 
 

Paint and copy problem

Post by Richard Wi » Sun, 18 Jun 2006 07:42:15


What you need to do is only create the BufferedImage the first time, in
the full width. Then do the copyArea on the BufferedImage to scroll the
pixels.

eg.

public void paintComponent(Graphics g) {
Graphics2D g2d;
if (bimage == null) {
bimage = getGraphicsConfiguration()
.createCompatibleImage(MAX_WIDTH,getHeight());
g2d = (Graphics2D)bimage.getGraphics();
g2d.setColor(getBackground());
g2d.fillRect(0,0,MAX_WIDTH,getHeight()); // First time, clear image
}
else {
g2d = (Graphics2D)bimage.getGraphics();
g2d.copyArea(x1, 0, 1, 0, -1, 0);
}
// Draw the new line (on the BufferedImage);
// eg. g2d.drawLine(0,0,0,getHeight());

// Dispose of the Graphics used on the image
g2d.dispose();

// Now copy the BufferedImage to the Component Graphics...
g.drawImage(bimage, 0, 0, null);
}

You'll also notice the second problem with your original code is that
you were drawing the BufferedImage back into it's own Graphics (g2d), so
you wouldn't get any on-screen drawing.

Regards,
Richard