Re: [JAVA2D] Drawing to an off screen buffer

2008-09-25 Thread java2d
You don't need a BufferStrategy or VolatileImage unless you are doing live 
animation.  It was hard to tell from your message if that was the case.

I usually use a BufferedImage that you get from createCompatibleImage and draw 
using Java2D to the graphics context you get from it.  When the plot is ready 
hand it over to the event dispatch thread and tell some component to paint it 
with invokeLater().

Otherwise look for examples that use BufferStrategy...
[Message sent by forum member 'swpalmer' (swpalmer)]

http://forums.java.net/jive/thread.jspa?messageID=301594

===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message help.


Re: [JAVA2D] Drawing to an off screen buffer

2008-09-25 Thread java2d
Use the Canvas if you need hardware double-buffering badly enough that you're 
willing to deal with the Z-ordering issues that you get from mixing Swing and 
AWT widgets. Otherwise, use a JPanel. You cannot use a Canvas inside of a 
JScrollPane, for one thing.

Rendering outside of the event thread can be iffy. I wouldn't try using a 
VolatileImage outside the event thread, but a BufferedImage should work. One 
thing that we should make clear is that getting hardware acceleration (for 
things like blitting the image) and rendering outside the event thread will be 
pretty much mutually exclusive. (Unless you're doing some really deep AWT 
hacking.)

Are we talking about rendering an animation, or rendering a graphic 
infrequently (say, in response to button clicks)? What you want to do really 
depends on the answer to that.
[Message sent by forum member 'afishionado' (afishionado)]

http://forums.java.net/jive/thread.jspa?messageID=301626

===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message help.


Re: [JAVA2D] Drawing to an off screen buffer

2008-09-25 Thread Ken Warner

Do you get reasonable animation with this method?
How many frames a second do you get using invokeLater()?

[EMAIL PROTECTED] wrote:

You don't need a BufferStrategy or VolatileImage unless you are doing live 
animation.  It was hard to tell from your message if that was the case.

I usually use a BufferedImage that you get from createCompatibleImage and draw 
using Java2D to the graphics context you get from it.  When the plot is ready 
hand it over to the event dispatch thread and tell some component to paint it 
with invokeLater().

Otherwise look for examples that use BufferStrategy...
[Message sent by forum member 'swpalmer' (swpalmer)]

http://forums.java.net/jive/thread.jspa?messageID=301594

===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message help.



===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message help.


Re: [JAVA2D] Drawing to an off screen buffer

2008-09-25 Thread java2d
Looking at some BufferStrategy examples, it seems that the pattern is to do 
rendering in a timed loop, something like this:

[code]
// Render loop
while (!done) {
   Graphics g = strategy.getDrawGraphics();
   render(g);
   g.dispose();
   strategy.show();

   try {
  Thread.sleep(100);
   }
   catch (InterruptedException e) {
  done=true;
   }
}
[/code]

I'm drawing in response to UI events (scrolling, zooming, mouse clicks for 
selecting objects). Does that mean BufferStrategy is not for my situation? 
Pointers to examples would be great.

Thanks,

-chris
[Message sent by forum member 'cbare' (cbare)]

http://forums.java.net/jive/thread.jspa?messageID=301637

===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message help.


Re: [JAVA2D] Drawing to an off screen buffer

2008-09-25 Thread java2d
I recommend using something similar to the Foxtrot API. It is not recommended 
to perform time intensive processes in the event queue.
[Message sent by forum member 'bcorbett8769' (bcorbett8769)]

http://forums.java.net/jive/thread.jspa?messageID=301665

===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message help.


Re: [JAVA2D] Drawing to an off screen buffer

2008-09-25 Thread java2d
Well, lets clarify/assume a few things.

You have some data source, constantly producing data, right? While that data 
source happily produces data the user is also looking at a representation of 
that data, right? And the user can interactively select  (scrolling, zooming)  
which portion of the data he/she gets a representation of?

Ok, first, I am not a big fan of rendering and rasterizing (drawing to an 
image) the data in the backgrond in such a case. For at least two reasons: 
Zooming on already rasterized data doesn't work well, so for zooming it is 
required to freshly rasterize that data. Second, if the plot is indeed very 
large you can not fully predict to which area the user might scroll next. So 
you can't reasonably well guess which area should be preferentially rendered in 
the background. Because of this two issues, a lot of CPU power used for 
background rendering is just wasted, since you render something you can't use.

So, instead of rendering in the background, organize/store the incomming data 
in a way that it is easy to render in the background (in a thread). This, for 
example, might include to already decompose the data into primitive graphic 
elements like lines. You don't actually draw such lines, you just store easy to 
process information that you would draw a line. And provide a locking mechanism 
on the data, so that the maintenance of the data and the rendering (see below) 
are properly synchronized.

Further, if the plot is really so large, spatially organize that graphic 
element data in the background in a way that you can quickly find all data 
within a particular rectangular 2D region (quadtree). If you have many 
overlapping graphic elements you might even want to organize the data in three 
dimensions (octree), so that you can easily cull details when the user has 
zoomed out very fare (because in this case many details would anyhow be lost 
when rendering the data).

Now to the user interaction and actual rendering. You create an own Swing 
component based on a JComponent or JPanel. Creating an own Swing component has 
often been document, I won't go into this. Just make sure you get the basics 
and the nastier details right :-) Also implement the Scrollable interface. I 
assume you put that component in a JScrollPane.

The paintComponent() method of your component will be called when Swing thinks 
your component or a part of your component needs to be redrawn. The key here is 
part of your component. Swing indicates what part needs to be redrawn with 
the clipping region (don't forget to take the Graphics2D origin into account, 
it might not always be 0,0). So in your paintComponent() you use that 
information (and the current zoom factor) to identify the part of your graphic 
element data that needs rendering. That part should be much less then your 
whole data. Then, right in paintComponent() you render it to the provided 
Graphics2D.

I assume you have some extra buttons for zooming in or out (or some mouse 
events). Anyhow, whenever the zoom factor changes you remember the new zoom 
factor, then call repaint(). At some point in time repaint() will result in a 
call to paintComponent(). And since your paintComponent() takes the zoom factor 
into account (well, it should), it will render the plot with the new zoom 
factor.

Also, whenever the incomming data has changed significantly, you call repaint, 
too. Don't do it to often, and don't do it for every tiny change. Accumulate a 
bunch of changes, then call repaint(). However, if you really think you need 
15fps and every tiny change in the data needs to be immediately visible, than 
forget everything I wrote and change to active rendering.

That's the basic architecture. If that is still not responsive enough, consider 
caching the renderings you did in paintComponent() in BufferedImages. And 
before you render some area you check your cache if you have already all or 
parts of that area in your cache. If you have, you just copy that part from the 
cache to the screen. You need proper cache management, like remembering which 
zoom factor was used for a particular rendering, or if the incoming data would 
invalidate a cached rendering.

Careful planning, a well designed data structure, knowing Swing's paint 
mechanism (with all its barely documented idiosyncrasies), a clear 
understanding of world, component and screen coordinate systems, and probably a 
thousand odds and ends are needed to make this fly. Or you drop that Java2D 
junk, where you are never sure if you get hardware acceleration or not, and use 
some commercial data visualization system or write directly for a particular OS 
and native graphics system, so you are not at the mercy of a VM with rubbish 
documentation.
[Message sent by forum member 'ewin' (ewin)]

http://forums.java.net/jive/thread.jspa?messageID=301687

===
To unsubscribe, send email to [EMAIL PROTECTED] and 

[JAVA2D] Drawing to an off screen buffer

2008-09-24 Thread java2d
Hi,

I'm trying to build a little program that draws complicated time-consuming 
plots. I'd like to avoid having my UI freeze up while I'm rendering, so I don't 
want to do the rendering on the event dispatch thread. So, what's the modern 
(well, Java 5 level of modern) way of doing this?

First, should I be drawing on a Canvas or a JPanel? I'm using Swing components 
in the rest of my app and I've read that you're not supposed to mix Swing and 
AWT, but I can switch if there's good reason.

Next, do I want to use VolatileImage, BufferStrategy, or something else? Can 
you even get a BufferStrategy for a JPanel? I'd like to get hardware double 
buffering, if possible.

Last, I assume it's OK to draw on the offscreen image on another thread and 
then put a runnable on the event dispatch thread to do the blt? Or is that the 
wrong way to go about it?

Any pointers to the current (JDK1.5) wisdom would be appreciated! Thanks,

-chris
[Message sent by forum member 'cbare' (cbare)]

http://forums.java.net/jive/thread.jspa?messageID=301468

===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message signoff JAVA2D-INTEREST.  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message help.