Need Help with Paint Proccess

Discussion in 'Java' started by dcook@ccs-a.com, Feb 27, 2006.

  1. Guest

    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
     
    , Feb 27, 2006
    #1
    1. Advertising

  2. 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/
     
    Knute Johnson, Feb 28, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Feb 28, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    472
    Bruce Barker
    Nov 23, 2005
  2. patrick

    javaw.exe proccess

    patrick, Jun 28, 2004, in forum: Java
    Replies:
    5
    Views:
    4,494
    Stefan Poehn
    Jun 29, 2004
  3. Replies:
    4
    Views:
    4,634
    Brendan Guild
    Oct 23, 2005
  4. John Camp
    Replies:
    1
    Views:
    366
    John Camp
    Jul 11, 2003
  5. alexrait1

    Proccess termination

    alexrait1, Jan 28, 2005, in forum: Python
    Replies:
    2
    Views:
    309
    George Yoshida
    Jan 28, 2005
Loading...

Share This Page