Need Help with Paint Proccess

D

dcook

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
 
K

Knute Johnson

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.
 
D

dcook

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top