Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need Help with Paint Proccess

Reply
Thread Tools

Need Help with Paint Proccess

 
 
dcook@ccs-a.com
Guest
Posts: n/a
 
      02-27-2006
Need help with a java project. My java application draws animated 2D
objects in a canvas using BufferStrategy. Upon certain events I want to
open another Frame; Frame A opens Frame B and draw objects into Frame B
from Frame A's paint process. Not sure what I am doing wrong but it
will not display what was drawn in Frame B unless I set focus to Frame
B. I need to paint to Frame B and have it shown without changing focus
to it.

Example;


// MyCanvasClass - Used for Frame A and Frame B
public void paint(Graphics g) {
if (this.frame == FrameB)
this.requestFocus();
if ( bimg == null) {
createBufferStrategy(2);
bimg = getBufferStrategy();
g2 = (Graphics2D)bimg.getDrawGraphics();
clearcanvas = true;
} else
g2 = (Graphics2D)bimg.getDrawGraphics();
render(g2);
bimg.show();
if ( clearcanvas) {
clearcanvas = false;
}
toolkit.sync();
// If Primary Frame and Frame B has been drawn too, repaint it.
if (this.frame != FrameB && FrameB.dirty == true) {
FrameB.canvas.repaint();
FrameB.dirty = false;
}
}

public void update(Graphics g) {
if (this.frame == FrameB)
this.requestFocus();
if (clearcanvas) {
super.update(g);
} else {
if ( bimg == null) {
createBufferStrategy(2);
bimg = getBufferStrategy();
g2 = (Graphics2D)bimg.getDrawGraphics();
clearcanvas = true;
} else
g2 = (Graphics2D)bimg.getDrawGraphics();
Myupdate(g2);
toolkit.sync();
// If Primary Frame and Frame B has been drawn too, repaint it.
if (this.frame != FrameB && FrameB.dirty == true) {
FrameB.canvas.repaint();
FrameB.dirty = false;
}
}
}



Thanks for any help you can provide!

Dave

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      02-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Need help with a java project. My java application draws animated 2D
> objects in a canvas using BufferStrategy. Upon certain events I want to
> open another Frame; Frame A opens Frame B and draw objects into Frame B
> from Frame A's paint process. Not sure what I am doing wrong but it
> will not display what was drawn in Frame B unless I set focus to Frame
> B. I need to paint to Frame B and have it shown without changing focus
> to it.
>
> Example;
>
>
> // MyCanvasClass - Used for Frame A and Frame B
> public void paint(Graphics g) {
> if (this.frame == FrameB)
> this.requestFocus();
> if ( bimg == null) {
> createBufferStrategy(2);
> bimg = getBufferStrategy();
> g2 = (Graphics2D)bimg.getDrawGraphics();
> clearcanvas = true;
> } else
> g2 = (Graphics2D)bimg.getDrawGraphics();
> render(g2);
> bimg.show();
> if ( clearcanvas) {
> clearcanvas = false;
> }
> toolkit.sync();
> // If Primary Frame and Frame B has been drawn too, repaint it.
> if (this.frame != FrameB && FrameB.dirty == true) {
> FrameB.canvas.repaint();
> FrameB.dirty = false;
> }
> }
>
> public void update(Graphics g) {
> if (this.frame == FrameB)
> this.requestFocus();
> if (clearcanvas) {
> super.update(g);
> } else {
> if ( bimg == null) {
> createBufferStrategy(2);
> bimg = getBufferStrategy();
> g2 = (Graphics2D)bimg.getDrawGraphics();
> clearcanvas = true;
> } else
> g2 = (Graphics2D)bimg.getDrawGraphics();
> Myupdate(g2);
> toolkit.sync();
> // If Primary Frame and Frame B has been drawn too, repaint it.
> if (this.frame != FrameB && FrameB.dirty == true) {
> FrameB.canvas.repaint();
> FrameB.dirty = false;
> }
> }
> }
>
>
>
> Thanks for any help you can provide!
>
> Dave
>


Dave:

You've got major architecture problems. First if you are going to do
active rendering you don't want to do it in the paint() method. Take a
look at BufferStrategy in the docs. Second, if you are going to open
another Frame or Window, do the rendering in that class. It will be
much easier to code and might actually work.

Also you can get about the same performance by using a VolatileImage as
your drawing surface and then drawing that on your component. With this
technique you use the component's paint() or paintComponent() method to
draw the VolatileImage. The real advantage to this method is that the
window can be automatically redrawn when you resize or cover it. With
typical active rendering that isn't possible as it bypasses the normal
painting process.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
dcook@ccs-a.com
Guest
Posts: n/a
 
      02-28-2006
Knute,

Thanks for your reply. My concern was with speed and flashing since
nothing is using any kind of Java object / component. Everything is
created using primitives such as drawText, drawArc, drawPolygon, etc...
and I only want to draw only the area that change. I started off using
BufferedImage array, manually doing what BufferStrategy does, but found
BufferStrategy would handle most of the work for me. When I draw into
another window, I have to draw from the primary window because I can
have overlapping objects partly in one window and the rest in another
window as well as mirrored objects . As a test, I made the second
window use a single BufferedImage that paint would just draw on the
canvas but found it also needed me to set focus to the window before I
could draw the buffered image or it would not show it. If I minimized
the window and restore it, it would then show the buffered image.

I am not sure if I am explaining this correctly so here is the concept.
This is more like a world map from 0,0 to width, height of whatever
size with the primary window (Frame) being at the bottom right side of
the coordinate plane and can show the entire world coordinates. The
windows are views of an area within that world coordinate system and
are dynamically opened based on animation events that can happen in
this world coordinate system. These windows can overlap the same
coordinates as well as overlap the primary window's coordinates. So
as you can see I have one process going through and drawing in the
desired coordinates but could be draw them to multiple windows at the
same time. This all works but I must set focus to the window being
updated before it will show it.

The one draw back I have found with BufferStrategy is since it uses
volatile images, it can lose the image,
"BufferStrategy.contentsLost()", which requires me to re-create the
BufferStratigy, redrawing all the objects which causes flashing.

Thanks!

Dave

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perfmon analysis of aspnet_wp proccess Mark ASP .Net 1 11-23-2005 05:39 PM
Unable to paint (using paint() in JPanel) inside mouse listeners eshedz@gmail.com Java 4 10-23-2005 09:13 PM
Proccess steve Computer Support 3 11-23-2004 06:36 PM
javaw.exe proccess patrick Java 5 06-29-2004 09:43 AM
Connecting to a proccess through a port using C++ John Camp C++ 1 07-11-2003 07:40 PM



Advertisments