Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiple BufferedImages and Graphics...

1 view
Skip to first unread message

opusprime

unread,
Apr 20, 2005, 7:27:20 PM4/20/05
to
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

unread,
Apr 21, 2005, 2:27:05 AM4/21/05
to
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

"opusprime" <opus...@yahoo.com> a écrit dans le message news:
1114039640....@g14g2000cwa.googlegroups.com...

Arnaud Berger

unread,
Apr 21, 2005, 2:29:33 AM4/21/05
to
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

"opusprime" <opus...@yahoo.com> a écrit dans le message news:
1114039640....@g14g2000cwa.googlegroups.com...

opusprime

unread,
Apr 21, 2005, 8:08:01 PM4/21/05
to
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

unread,
Apr 21, 2005, 8:07:56 PM4/21/05
to
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.

opusprime

unread,
Apr 21, 2005, 8:09:37 PM4/21/05
to

opusprime

unread,
Apr 21, 2005, 8:29:47 PM4/21/05
to
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);

0 new messages