Results 1 to 2 of 2
  1. #1
    pcman is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Unhappy the code dosent work

    i try to do a animation....
    all 10 minutes i need to repaint and drew the oval again after some of time
    i think the problam here
    Java Code:
    t.sleep( 1000 );
    the code:
    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class ok extends Applet implements Runnable {
    int y;
    Thread t = null;
    public void paint(Graphics g)
    {
    	while(y > 500)
    	{
    	g.fillOval(100, y, 50, 50);
    	y = y+10;
    	t.sleep( 1000 );
    	repaint();
    	}
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    //  <applet code="OkAnimation" width="300" height="300"></applet>
    //  use: >appletviewer OkAnimation.java
    import java.applet.*;
    import java.awt.*;
    
    public class OkAnimation extends Applet implements Runnable
    {
        int y;
        int dy = 10;
        Thread t = null;
        boolean isRunning = false;
    
        /** Applet method override */
        public void paint(Graphics g)
        {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g.fillOval(100, y, 50, 50);
        }
    
        /** Applet method override */
        public void start()
        {
            if(!isRunning)
            {
                isRunning = true;
                t = new Thread(this);
                t.start();
            }
        }
    
        /** Applet method override */
        public void stop()
        {
            isRunning = false;
            if(t != null)
                t.interrupt();  // wake up
            t = null;
        }
    
        /** Runnable interface implementation */
        public void run()
        {
            while(isRunning)
            {
                try
                {
                    t.sleep( 1000 );
                }
                catch(InterruptedException e)
                {
                    stop();
                }
                if(y + dy < 0 || y + 50 + dy > getHeight()) {
                    // if the next move puts us out of bounds
                    // reverse direction.
                    dy *= -1;
                }
                y = y+dy;
                repaint();
            }
        }
    }

Similar Threads

  1. Pass by ref. A work around?
    By diRisig in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 07:25 PM
  2. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  3. what do I have to install to work with JSP
    By boy22 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 04:08 AM
  4. My own ClassLoader didn't work.
    By snooze-g in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2007, 11:12 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •