Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2006

Tip: Looking for answers? Try searching our database.

Paint and copy problem

Thread view: 
Heph - 16 Jun 2006 08:16 GMT
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
Oliver Wong - 16 Jun 2006 15:39 GMT
> Hey people,
>
[quoted text clipped - 52 lines]
> the panel and pusing older lines to the left.
> most left = oldest.

   Why didn't the above code work?

   - Oliver
Richard Wilson - 16 Jun 2006 23:42 GMT
> What I have tried so far:
>
[quoted text clipped - 13 lines]
>
> }

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.