Skip to main content

fillPolygon performance versus drawImage

5 replies [Last post]
gmiecznik
Offline
Joined: 2007-11-07

Hi,

This is probably a naive question...

I need to display a frequently varying rectangles, that can have different size and color. A typical size of an individual rectangle is about 5 by 5 pixels, but there can be up to 100x110 rectangles per display (a typical bitmap display). I need to display it from with a JComponent.

My big question is: what is the fastest way to do it?
I can either loop over the number of rectangles and each time call Graphics.fillRect(), or I can create an image using MemoryImageSource followed by createImage and eventually Graphics.drawImage() function. Which one (or a different method) shall I use?

Also, where can I find information about how Graphics.fillRect() work? Does this
function fills in an array of pixels with one color, and then creates an object
equivalent to an image, so in essence it is the same as calling drawImage?
Or is the mechanism completely different?

Thank you
Gregory

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Ken Warner

Hi Gregory,

Do all your rectangle drawing to an off screen image. The image can be any
thing from a Toolkit image to a BufferedImage to a VolitileImage. Each
one has advantages. Then draw the off screen image to the screen.

Your mileage may vary...

java2d@JAVADESKTOP.ORG wrote:
> Hi,
>
> This is probably a naive question...
>
> I need to display a frequently varying rectangles, that can have different size and color. A typical size of an individual rectangle is about 5 by 5 pixels, but there can be up to 100x110 rectangles per display (a typical bitmap display). I need to display it from with a JComponent.
>
> My big question is: what is the fastest way to do it?
> I can either loop over the number of rectangles and each time call Graphics.fillRect(), or I can create an image using MemoryImageSource followed by createImage and eventually Graphics.drawImage() function. Which one (or a different method) shall I use?
>
> Also, where can I find information about how Graphics.fillRect() work? Does this
> function fills in an array of pixels with one color, and then creates an object
> equivalent to an image, so in essence it is the same as calling drawImage?
> Or is the mechanism completely different?
>
> Thank you
> Gregory
> [Message sent by forum member 'gmiecznik' (gmiecznik)]
>
> http://forums.java.net/jive/thread.jspa?messageID=248161
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".
>
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

gmiecznik
Offline
Joined: 2007-11-07

Thank you!

I still can't find answer to my other question - how the rectangles (or shapes, in general) are drawn.

Here is an example from "Building Imaging Application with Java Technology" by L.H. Rodriques on page 159:

---------------------- Listing 5.1 ------------------------------
protected BufferedImage offScrImage;
protected Graphics2D offScrGc;

public void createOffScreenImage(int wid, int ht){
offScrImage = new BufferedImage(wid, ht,BufferedImage.TYPE_INT_RGB);
offScrGc = offScrImage.createGraphics();
}

public void drawRectOffScreen(){
offScrGc.drawRect(30,40,100,100,this);
}

public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(offScrImage, 0,0, this);
}

---------------------------------------------------------------------

In the above example, the recangle is drawn off-screen to a buffered image (I guess
this happens indicetly by calling the graphic context).

A proper (?) way to draw a rectangle into a buffered image would be through a DataBuffer, a SampleModel and a WritableRaster.

Are these two methods identical? If not, what is the drawback of using the above listing versus a BufferedImage pupulated with a DataBuffer/SampleModel/WritableRaster?

Thanks again.
Gregory

flar
Offline
Joined: 2003-06-11

Hi Gregory,

Sorry to take so long to respond to this thread. Others had been responding, but now it looks like your last set of questions has gone unanswered.

One thing that confuses me is that you are asking about a wide range of APIs from the very high level to the very low level and unsure where to start. I'd say start with the simple technique of just calling fillRect() on each rectangle in the list and see where that gets you. That may work just fine for your needs.

If that doesn't achieve the performance you were looking for then look into enabling one of the acceleration pipelines we provide (other threads on the forum provide information on the new D3D pipelines on Windows as well as the OpenGL pipelines for Linux and Windows).

If you can't get the performance that you need from those APIs then I'm not sure you will do much better getting the DataBuffers and pixel arrays yourself using the other APIs you've cited. Those are more for doing rendering that isn't addressed by our Graphics APIs and they are intended only for experts (i.e. "proceed there at your own risk"). I'd almost go so far as to say that "if you have to ask if they are a 'proper' way to draw a rectangle then I would stay away from them".

...jim

Dmitri Trembovetski

Hi Jim,

could you take a look at this one if you have a sec:
http://www.javagaming.org/forums/index.php?topic=17925.0

It'll take me much longer than you to explain the
guy what's going on..

Thanks,
Dmitri

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Dmitri Trembovetski

And that is why "reply to sender only" w/o looking
at who you're replying to is not smart =)

Dmitri

Dmitri Trembovetski wrote:
> Hi Jim,
>
> could you take a look at this one if you have a sec:
> http://www.javagaming.org/forums/index.php?topic=17925.0
>
> It'll take me much longer than you to explain the
> guy what's going on..
>
> Thanks,
> Dmitri
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".