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 / April 2005

Tip: Looking for answers? Try searching our database.

Multiple BufferedImages and Graphics...

Thread view: 
opusprime - 21 Apr 2005 00:27 GMT
I'm writing a simple (at this point) drawing program that allows the
user to draw different shapes such as lines, rectangles etc.

The drawings need to be pretty precise so I want to display a grid on
my JPanel, and allow the user to draw objects over the grid.

I have no problems drawing the grid using the Graphics2D object I get
from my offscreen BufferedImage, i.e.,

public void buildGridComponent(Graphics2d g2) {
          Dimension d = getSize();
          Graphics2D g2 = (Graphics2D)gridBufImg.getGraphics();
          for(double x = 0.0; x < d.width; x++) {
           if(x % 10 == 0) {
               Line2D l = new Line2D.Double(new Point2D.Double(x, 0), new
Point2D.Double(x, d.getHeight()));
               g2.draw(l);
           }
       }
              for(double y = 0.0; y < d.width; y++) {
                  if(y % 10 == 0) {
               Line2D l2 = new Line2D.Double(new Point2D.Double(0, y), new
Point2D.Double(d.getWidth(), y));
               g2.draw(l2);
           }
       }
}

But this is incredibly inefficient when drawing.  Painting slows down
so much that my mouse will ghost from the ends of my lines.  Its slow
and choppy.

So, my idea was to just create another BufferedImage, get it's
Graphics2D object, draw to that object, and just draw the BufferedImage
whenever the grid button is toggled.  For some reason though, I'm
blowing up when trying to draw this image.

gridBufImg = new BufferedImage(d.width, d.height,
BufferedImage.TYPE_BYTE_GRAY );
Graphics2D g2 = (Graphics2D)gridBufImg.getGraphics();

When I try and paint this BufferedImage to my offscreen Image I get a
NullPointerException.

java.lang.NullPointerException
       at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
       at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
       at sun.java2d.pipe.ValidatePipe.copyImage(Unknown Source)
       at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
       at sun.java2d.SunGraphics2D.drawImage(Unknown Source)

Any hints?
Arnaud Berger - 21 Apr 2005 07:27 GMT
Hi,

Wouldn't :

Dimension d = getSize();

for (int x = 0; x < d.width; x+10) {
g.drawLine(x * , 0, x , d.height);
}
for (int y= 0; y< d.height; y+10) {
g.drawLine(0, y, d.width, y);
}

be more efficient ?

Indeed, your loop seems to check each "pixel", because it loops on any x/y
combinations.
e.g if the size is 100*100, you will loop 100*100=10000 times , and the code
above will loop 10+10= 20 times ....

Regards,

Arnaud

> I'm writing a simple (at this point) drawing program that allows the
> user to draw different shapes such as lines, rectangles etc.
[quoted text clipped - 48 lines]
>
> Any hints?
Arnaud Berger - 21 Apr 2005 07:29 GMT
Well sorry, I haven't checked enough.

You would actually loop 2*100=200 times.

However, it is still 10 times too much, and I think drawLine may be simpler
than Line2D stuff .

Regards,

Arnaud

> I'm writing a simple (at this point) drawing program that allows the
> user to draw different shapes such as lines, rectangles etc.
[quoted text clipped - 48 lines]
>
> Any hints?
opusprime - 22 Apr 2005 01:07 GMT
Hey Arnaud,

Thanks for the reply.   The reason I have the lines using the 2D model
is because I want to make them dashed lines and control the color to
make them barely visable.

But either way, I dont want to paint each line in the grid everytime
the paint() is called.  I would like to build the grid once, put it
into a BufferedImage, and then just display the already constructed
BufferedImage when I want.

OP
opusprime - 22 Apr 2005 01:08 GMT
Hey Arnaud,

Thanks for the reply.   The reason I have the lines using the 2D model
is because I want to make them dashed lines and control the color to
make there barely visable.

But either way, I dont want to paint each line in the grid everytime
the paint() is called.  I would like to build the grid once, put it
into a BufferedImage, and then just display the already constructed
BufferedImage when I want.

OP
opusprime - 22 Apr 2005 01:29 GMT
Ok, I got it.

You can take the Graphics object from a second BufferedImage and draw
to it.    My mistake appears to have been in drawing my second
BufferedImage, I was using the Graphics2D class to draw on my
BufferedImage, but I was adding the image to my background using the
regular Graphics class.  Once I switch this to  draw to the Graphics2D
object, it painted correctly.

g.drawImage(bimg, 0, 0, this);

switched to

g2.drawImage(bimg, 0, 0, this);
opusprime - 22 Apr 2005 01:09 GMT
Hey Arnaud,

Thanks for the reply.   The reason I have the lines using the 2D model
is because I want to make them dashed lines and control the color to
make them barely visable.

But either way, I dont want to paint each line in the grid everytime
the paint() is called.  I would like to build the grid once, put it
into a BufferedImage, and then just display the already constructed
BufferedImage when I want.

OP


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.