Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Need Help with Paint Proccess

Thread view: 
dcook@ccs-a.com - 27 Feb 2006 21:11 GMT
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
Knute Johnson - 28 Feb 2006 17:53 GMT
> Need help with a java project. My java application draws animated 2D
> objects in a canvas using BufferStrategy. Upon certain events I want to
[quoted text clipped - 56 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

dcook@ccs-a.com - 28 Feb 2006 19:53 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.