Results 1 to 5 of 5
  1. #1
    George1935 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default Two sleeps in animation

    The listing below is a modification to a program in Campesato's excellent book "Java Graphics Programming: Concepts to Source Code," page 95. It draws a series of multicolored rectangles along a deltoid curve. I added start(), run(), and stop() methods to contol the animation; my objective being to slow down the loop and sleep a tenth of a second or so between drawing each individual rectangle and then another sleep of a few seconds after the entire figure is drawn before it redraws itself. The long sleep after the whole figure is drawn was straight forward; works fine, but try as I may I can't slow down drawing the rectangles. Would someone please show me what I'm missing here? I commented out some of my failed attempts but left them in the listing so you can see the kinds I was trying.

    Thank you
    George1935


    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.*;
    
    public class MyOscillatingRectangles01 extends JApplet implements Runnable
    {
        Thread animator; 
    //    boolean blankScreen;
        private int loopCount = 0;
        private Image    offScreen;
        private Graphics offScreenG;
       
        private int x;
        private int xCoord;
        private int yCoord; 
        private int angle; 
        private int width      = 800;
        private int height     = 800;
        private int basePointX = 400;  // positions figure on screen
        private int basePointY = 300;  // positions figure on screen
        private int currentX   = basePointX;
        private int currentY   = basePointY;
        private double offsetX    = 0;
        private double offsetY    = 0;
        private double sineAngle1 = 0;
        private double sineAngle2 = 0;
        private double cosineAngle1 = 0;
        private double cosineAngle2 = 0;
        private int anglePartition = 12;  //affects colors
        private int branch = 0;
        private int radius = 90;   //determines overall size
        private int eHeight    = 70;  // rectangle height
        private int eWidth     = 70;  // rectangle width
        private int startTheta = 0;
        private int endTheta   = 360;
        private int angleSpan = endTheta-startTheta;
    
        private Color[] rectangleColors = 
        {
          Color.red, Color.green, Color.blue, Color.yellow,
          Color.white, Color.cyan, Color.orange
        };  // ends rectangleColors
    
       private int colorCount = rectangleColors.length;
     
       public void init()
       {
          offScreen  = createImage(getWidth(), getHeight());
          offScreenG = offScreen.getGraphics();
       } // ends init 
       
       public void start() 
       {
         animator = new Thread(this);
         animator.start();
       }  /*** Ends public void start ***/
       
     
       public void paint(Graphics gc)
       {
          offScreenG.setColor(Color.cyan);
          offScreenG.fillRect(0, 0, width, height);
          offScreenG.setColor(Color.blue);
          drawSpiral(offScreenG, gc);
       } // ends paint
    
       public void drawSpiral(Graphics gc, Graphics gcMain)
       {
           for(int angle=startTheta; angle<endTheta; angle++)
           {
             if( angle % anglePartition == 0 ) { ++branch; } // varies color
    
             sineAngle1 = Math.sin(angle*Math.PI/180);
             sineAngle2 = Math.sin(3*angle*Math.PI/180);
    
             cosineAngle1 = Math.cos(angle*Math.PI/180);
             cosineAngle2 = Math.cos(3*angle*Math.PI/180);
    
             offsetX = radius*(2*cosineAngle1+cosineAngle2);
             offsetY = radius*(2*sineAngle1-sineAngle2);
    
             currentX = basePointX+(int)offsetX;
             currentY = basePointY+(int)offsetY;
     
             drawRectangle(gc, angle, currentX, currentY);
             gcMain.drawImage(offScreen, 0, 0, this);
           }  // ends for loop
       } // ends drawSpiral
    
       public void drawRectangle(Graphics gc, int x, int xCoord, int yCoord)
       {
          gc.setColor(rectangleColors[((x%2)*2+branch)%colorCount]);
          gc.fillRect(xCoord, yCoord, eWidth, eHeight); 
       } // ends drawRectangle
       
       public void run()
       {
           while (Thread.currentThread() == animator)
           {
    //           for(int angle=startTheta; angle<endTheta; angle++)
    //           branch++;
    //           currentX++;
    //           if( angle % anglePartition == 0 ) { ++branch; } 
    
    
               repaint();
               {
               try
               {
                   Thread.sleep(2000);
               }
               catch (InterruptedException e)
               {
                   break;
               }
               
     //           for(int angle=startTheta; angle<endTheta; angle++)  
          try
               {
                   Thread.sleep(20);
               }
               catch (InterruptedException e)
               {
                   break;
               }     
               
               
               }
    
            }
        } // ends run
    
        public void stop()
        {
            animator = null;
        } // ends stop
    } // ends class MyOscillatingRectangles01

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

    Default

    Have you found out where the animations are being created?
    Somewhere in the applet there is code to draw an rectangle and then another and then another and so on. It must be in a loop. To add time between the drawing of one rectangle and the next one, where would be a good place to sleep? Add println statements to see where and what is happening.
    Why do You have 2 sleep statements one after the other? Why not one for 2020?

  3. #3
    George1935 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    The loop is set up in the drawSpiral method() which then calls drawRectangle() to draw the rectangles. The sleep(2000) statement makes it sleep 2 seconds after the whole figure is drawn (that works). The sleep(20) - which is the one I can't get to work - was intended to cause a slight delay between drawing each individual rectangle.

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

    Default Rewritten version

    It was a slow afternoon.
    Java Code:
    Following allows AppletViewer on this source
    <APPLET CODE=OsclRectApplet WIDTH=750 HEIGHT=550>
    <PARAM NAME=DELAY VALUE=20>
    </APPLET>
    */
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.*;
    
    public class OsclRectApplet extends JApplet implements Runnable  {
        Thread animator; 
        private Image    offScreen;        // Draw image here
        private Graphics offScreenG;
       
        // Define constants for the app
        final private int width      = 750;
        final private int height     = 550;
        final private int basePointX = 300; // positions figure on screen
        final private int basePointY = 255; // positions figure on screen
        final private int eHeight    = 60;  // rectangle height
        final private int eWidth     = 60;  // rectangle width
        final private int startTheta = 0;
        final private int endTheta   = 360;
        final private int anglePartition = 12;  //affects colors
        final private int radius = 90;   //determines overall size
    
        private int branch = 0;  // index into colors
    
        final private Color BackGround = Color.lightGray;
        final private Color[] rectangleColors = 
        {
          Color.red, Color.green, Color.blue, Color.yellow,
          Color.white, Color.cyan, Color.orange, Color.black
        };  // ends rectangleColors
    
       final private int colorCount = rectangleColors.length;
    
       // Controls for timing of drawings
       private int delay = 2;              // time to delay between displays of squares
       private int timeToNext = 2000;      // time between spirals
     
       //-----------------------------------------------------------------
       public void init()    {
          offScreen  = createImage(getWidth(), getHeight());
          System.out.println(getWidth() + " " + getHeight());
          offScreenG = offScreen.getGraphics();
          String delayS = getParameter("DELAY");
          if(delayS != null) {
             delay = Integer.parseInt(delayS);
             System.out.println("delay=" + delay);
          }
       } // ends init 
       
       public void start()     {
         animator = new Thread(this);
         animator.start();
       }  /*** Ends public void start ***/
       
       public void run()   {
           // Continuous loop to show spiral
           while (Thread.currentThread() == animator)        {
             if(animator == null)
                break;                     // We've been stopped
    
             // Blank the screen
             offScreenG.setColor(BackGround);
             offScreenG.fillRect(0, 0, width, height);
             offScreenG.setColor(Color.blue);  // first color?
    
             for(int angle = startTheta; angle < endTheta; angle++)        {
               if( angle % anglePartition == 0 ) { ++branch; } // varies color
               drawSpiral(angle, offScreenG);      // go draw a square       
               repaint();
               try {Thread.sleep(delay);} catch (InterruptedException e){}  
             } //end for
             // What before drawing the next one
             try           {
                Thread.sleep(timeToNext);
             } catch (InterruptedException e) {
                break;
             }
           } // end while()
           System.out.println("exiting run");
        } // ends run
    
     
       public void paint(Graphics gc)    {
          gc.drawImage(offScreen, 0, 0, this);
       } // ends paint
    
       private void drawSpiral(int angle, Graphics gc)    {
             double sineAngle1 = Math.sin(angle*Math.PI/180);
             double sineAngle2 = Math.sin(3*angle*Math.PI/180);
    
             double cosineAngle1 = Math.cos(angle*Math.PI/180);
             double cosineAngle2 = Math.cos(3*angle*Math.PI/180);
    
             double offsetX = radius*(2*cosineAngle1+cosineAngle2);
             double offsetY = radius*(2*sineAngle1-sineAngle2);
    
             int currentX = basePointX+(int)offsetX;
             int currentY = basePointY+(int)offsetY;
             if(currentY > (height-eHeight)) {
    //            System.out.println("currentY=" + currentY);    //NB - GOES OFF BOTTOM OF SCREEN 
             }
             drawRectangle(gc, angle, currentX, currentY);
       } // ends drawSpiral
    
       private void drawRectangle(Graphics gc, int x, int xCoord, int yCoord)    {
          gc.setColor(rectangleColors[((x%2)*2+branch)%colorCount]);
          gc.fillRect(xCoord, yCoord, eWidth, eHeight); 
       } // ends drawRectangle
       
        public void stop()     {
            animator = null;
        } // ends stop
    } // ends class

  5. #5
    George1935 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks a lot, Norm. I really appreciate your help. Not only did you answer my question, your solution included a couple of neat tricks I never would've thought of.

    George

Similar Threads

  1. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 04:26 PM
  2. Animation Synchronization
    By dreadrocksean in forum Advanced Java
    Replies: 5
    Last Post: 08-08-2008, 02:56 AM
  3. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM
  4. Text Animation
    By rossomandop@acm.org in forum AWT / Swing
    Replies: 4
    Last Post: 05-30-2008, 03:34 AM
  5. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 10:16 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
  •