Results 1 to 4 of 4

Thread: Fish animation

  1. #1
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default Fish animation

    So i drew a fish with awt an i made him move kinda and thats the problem the fish justs get thicker how do i make it erase the old line

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    
    public class Template
    {
    	public static void main (String args[])
    	{
          Windows win = new Windows();
          win.setSize(800,600);
          win.addWindowListener(new WindowAdapter() {public void
          windowClosing(WindowEvent e) {System.exit(0);}});
          win.show();
           }
    }
    
    class Windows extends Frame
    {
           public void paint(Graphics screen)
           {
        
             Polygon fish= new Polygon();
            
             for(int x1=1;x1<=60;x1++)
             {
             fish.addPoint((130+x1	),150);
             fish.addPoint((125+x1),350);
             fish.addPoint((175+x1),250);
             screen.drawPolygon(fish);
             screen.drawLine((175+x1),250,(250+x1),400);
             screen.drawLine((250+x1),400,(375+x1),275);
             screen.drawLine((375+x1),275,(260+x1),125);
             screen.drawLine((175+x1),250,(260+x1),125);
             screen.drawOval((290+x1),225,50,50);
             screen.setColor(Color.green);
             screen.fillOval((308+x1),242,20,20);
             delay(5000);
             } 
           }
     public static void delay(double d)
         {
         double n;
         for(n=0;(n<=d);n+=.0001);
         } 
    /* public static move(int x1)          Comment out Start 
      {
    	int move=x1+=1;
    	return move;
      }
       public static move2(int x2)
      {
    	int move2=x2+=1;
    	return move2;                        END(Commment)
      }*/
    }
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I have no idea why you are using AWT. These days everybody uses Swing. So I would start by reading the Custom Painting section from the Swing tutorial for the proper way to do this in Swing.

    Also, check out the Swing Timer for a better way to do animation.

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Heres how I did it:

    Java Code:
       import java.io.*;
       import java.util.Random;
       import java.text.DecimalFormat;
       import javax.swing.*;
       import java.awt.*;
       import java.awt.event.*;
       import java.awt.Color;
    
        public class Template
          {
           public static void main (String args[])throws IOException
             {
             Windows win = new Windows();
             win.setSize(800,600);
             win.addWindowListener(
                                   new WindowAdapter() {
                                      public void
                                      windowClosing(WindowEvent e) {
                                        System.exit(0);}});
             win.show();
             }
          }
    
        class Windows extends Frame
          {
           public void paint(Graphics screen)
             {
             for(int x1=1;x1<=60;x1++)
                {
                Polygon fish= new Polygon();
                fish.addPoint((125+x1),150);
                fish.addPoint((125+x1),350);
                fish.addPoint((175+x1),250);
                screen.clearRect(0,0,800,600);
                screen.drawPolygon(fish);
                screen.drawLine((175+x1),250,(250+x1),400);
                screen.drawLine((250+x1),400,(375+x1),275);
                screen.drawLine((375+x1),275,(260+x1),125);
                screen.drawLine((175+x1),250,(260+x1),125);
                screen.drawOval((290+x1),225,50,50);
                screen.setColor(Color.green);
                screen.fillOval((308+x1),242,20,20);
                delay(5000);
                }
             
             } 
           public static void delay(double d)
             {
             double n;
             for(n=0;(n<=d);n+=.0001)
                ;
             } 
       /* public static move(int x1)           //comment out start
       {
       int move=x1+=1;
       return move;                           
       }
       public static move2(int x2)
       {
       int move2=x2+=1;
       return move2;
       }*/                                         // comment out end
          }
    This way is very bad in my opinion because you aren't clearing the Graphics, you just keep adding to it.

  4. #4
    macason is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    IMO,
    1. Clear the screen after each render
    2. Don't loop to cause a delay create a timer thread and sleep it for the time needed to delay the rendering or use another lib that can give the appropriate functionality. (how do you stop the application ? by crashing it)

Similar Threads

  1. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 05:26 PM
  2. Fish animation
    By SwEeTAcTioN in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 07:23 AM
  3. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 12:41 PM
  4. Animation Synchronization
    By dreadrocksean in forum Advanced Java
    Replies: 5
    Last Post: 08-08-2008, 03:56 AM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 04:37 AM

Posting Permissions

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