Paint and copy problem

H

Heph

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
 
O

Oliver Wong

Heph said:
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.

Why didn't the above code work?

- Oliver
 
R

Richard Wilson

Heph said:
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

}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top