Skip to main content

[JAVA2D] Must all drawing be performed on the EventQueue thread?

1 reply [Last post]
Anonymous

I know that when I draw to the graphics context of a component, it's
important to draw everything on the EventQueue. But does that rule also
apply to an internally created image? For example, if I code:

BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

Graphics2D gc = (Graphics2D) image.getGraphics();
gc.drawRectangle(100, 100, 300, 400);
gc.dispose();

Since the image is not displayed yet, I would think it is kosher to call
this code from a non-EventQueue thread. Adding the image to a component,
of course, would have to occur on the EventQueue thread.

Am I correct in this assumption, or off-base?

Thanks,

Dan

===========================================================================
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".

Reply viewing options

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

Hi Dan,

This should work fine. In fact, some server apps take advantage of this
by using Java2D to render images on separate threads that take
advantage of multiple processors.

The only trick here is making sure your different threads
(rendering thread(s) and EventQueue thread) are doing the
right things at the right time. For example, make sure your rendering
thread is finished drawing by the time you add the image to your
component on the EventQueue thread.

Chet.

Dan Blanks wrote:
> I know that when I draw to the graphics context of a component, it's
> important to draw everything on the EventQueue. But does that rule also
> apply to an internally created image? For example, if I code:
>
> BufferedImage image = new BufferedImage(width, height,
> BufferedImage.TYPE_INT_RGB);
>
> Graphics2D gc = (Graphics2D) image.getGraphics();
> gc.drawRectangle(100, 100, 300, 400);
> gc.dispose();
>
> Since the image is not displayed yet, I would think it is kosher to call
> this code from a non-EventQueue thread. Adding the image to a component,
> of course, would have to occur on the EventQueue thread.
>
> Am I correct in this assumption, or off-base?
>
> Thanks,
>
> Dan
>
> ===========================================================================
> 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".