Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    The endless loop traps the GUI thread in paint() and it never exits. Nothing else will get executed until that loop exits.
    You need to use a Timer class object and/or threads for what you are trying to do.
    At this point, I suggest you do some research on Threads and Timers before trying to code any more.

  2. #22
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default AWT buffer image implementation

    This is the traditional AWT coding format
    for creating a buffered image output.
    Notice the update(Graphics g) method
    calling only the paint(Graphics g) method
    seems counter-intuitive.


    Java Code:
      public void update(Graphics g) { 
        paint(g);
      }
    
      public void paint( Graphics g ) {
        // 1. Create the offscreen buffer.
        // 2. Fill offscreen buffer with desired graphics.
        // 3. Draw the offscreen buffer onto a viewable component.
      }
    You can do a study as to why this
    must be, as I have done, then you
    can forget those details as I have
    done, and just use this format for
    what it is.

    Here is your applet implementing this format..
    PLUS,
    I added a red non-buffered red line segment
    to your while loop so you can see the
    difference in the display.


    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet {
      int sec = 45;
      int x = 1;
      int w = 120;
      int h = 150;
    
      public void init() {
        x = 1;
        setBackground( Color.blue );
      }
    
      public void start() {
        if (sec == 45) {
          w = 120;
          h = 150;
          try{
            Thread.sleep(1000);
          } 
          catch(InterruptedException e){
            e.printStackTrace();
          }
          sec++;
        }
        if (sec == 44) {
          w = 119;
          h = 147;
          try {
            Thread.sleep(1000);
          } 
          catch(InterruptedException e) {
            e.printStackTrace();
          }
          sec++;
        }
      }
    
      public void update(Graphics g) { 
        paint(g);
      }
    
      public void paint( Graphics g ) {
        // create the offscreen buffer and associated Graphics
        Graphics offgc;
        Image offscreen = null;
        Dimension d = size();
        offscreen = createImage(d.width, d.height);
        offgc = offscreen.getGraphics();
        // clear the exposed area
        offgc.setColor(getBackground());
        offgc.fillRect(0, 0, d.width, d.height);
        offgc.setColor(getForeground());
        offgc.setColor(Color.black);
        offgc.fillRect(0, 0, 300, 300);
        offgc.setColor(new Color(48, 203, 255));
        while(true){                           // 
          offgc.drawLine(150, 150, w, h);
          System.out.println("in update");
    
          // do normal redraw
          // transfer offscreen to window
          g.drawImage(offscreen, 0, 0, this);
          g.setColor(Color.red);               // Draw a non-buffered
          g.drawLine(150, 50, w, h-100);       // red line segment.
        }
      }
    }
    Swing does not require this coding format.
    When you move from AWT to Swing, you will
    relearn this topic in a different context.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    You've left in the problem with the endless loop in paint. Once execution gets into that loop it will stay there forever. Do you see lots of "in update" messages?

Page 2 of 2 FirstFirst 12

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  2. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  3. Need Help: Double Buffer
    By rukawa527 in forum AWT / Swing
    Replies: 1
    Last Post: 02-09-2009, 12:39 AM
  4. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  5. Can't DOUBLE BUFFER images
    By hunterbdb in forum Java 2D
    Replies: 7
    Last Post: 11-14-2008, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •