본문 바로가기

OM

애니메이션



Animation.java=======================================================================================

import java.awt.*;
import javax.swing.*;

public class Animation extends JApplet implements Runnable
{
    int imageTotal;
    int frame;
    int x, y;

    Image scard[];
    Thread clock;
    MediaTracker myTracker;

    Image off;
    Graphics offG;

    public void init()
    {
        off = createImage(400, 400);
        offG = off.getGraphics();

        imageTotal = 16;
        scard = new Image[imageTotal];

        myTracker = new MediaTracker(this);

        for(int i=0; i<imageTotal; i++)
        {
            scard[i] = Toolkit.getDefaultToolkit().getImage(getClass().getResource("s_"+i+".gif"));
            myTracker.addImage(scard[i], 0);
        }

        try
        {
            myTracker.waitForAll();
        }
        catch(InterruptedException ie){}

        while((myTracker.statusAll(true) & MediaTracker.COMPLETE) == 0){}

        x = y = 0;
    }

    public void start()
    {
        if(clock==null)
        {
            clock = new Thread(this);
            clock.start();
        }
    }

    public void paint(Graphics g)
    {
        g.drawImage(off, 0, 0, 400, 400, this);
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void run()
    {
        while(true)
        {
            try
            {
                clock.sleep(50);
            }
            catch(InterruptedException ie){}

            if(x<400 && y<400)
            {
                x++;
                y++;
            }
            else{
                x = y = 0;
            }

            if(frame<imageTotal-1)
            {
                frame++;
            }
            else{
                frame = 0;
            }

            Color bgColor = getBackground();
            offG.setColor(bgColor);
            offG.fillRect(x-1, y-1, 59, 90);
            offG.drawImage(scard[frame], x, y, 59, 90, this);

            repaint();
        }
    }

    public void stop()
    {
        if(clock!=null && clock.isAlive())
        {
            clock = null;
        }
    }
}

=====================================================================================================

'OM' 카테고리의 다른 글

캐릭터 이동  (2) 2008.05.11
Yahoo WebAPI를 이용한 날씨 위젯 2  (0) 2008.05.11
Yahoo WebAPI를 이용한 날씨 위젯 1  (1) 2008.05.10
Flash Javascript 연동 (ExternalInterface)  (0) 2008.05.07
생명게임  (0) 2008.05.05
Card Match 2  (0) 2008.05.04
원 그리기 (MouseListener)  (0) 2008.05.02
디지털 시계  (0) 2008.05.02
숫자 야구 게임  (0) 2008.05.02