6章 アニメーション


アニメーションとマウス(EventListenerの併用)


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar

public class clock extends Applet implements Runnable, MouseListener, MouseMotionListener {
	int k=0;
	int dt=200;
	double da=3.1416/30.0;
	Thread th;

	int Mxx=(-10), Myy=(-10);

	public void init()
	{
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	public void start() {
		th=new Thread(this);
		th.start();
	}
	

	public void mousePressed(MouseEvent e)
	{	
		Mxx=e.getX();
		Myy=e.getY();
		repaint();
	}
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mouseMoved(MouseEvent e){}
	public void mouseDragged(MouseEvent e)
	{
		Mxx=e.getX();
		Myy=e.getY();
		repaint();
	}		
		
	public void paint(Graphics g){
		double h, m, ss, cc;
		double t, r=75.0, rr=20.0;
		double u, rrr=70.0;
		double v, rrrr=60.0;
		int x, y, xx, yy;
		g.setColor(Color.white);
		g.fillOval(20, 20, 160, 160);
		g.setColor(Color.black);
		g.drawOval(19, 19, 162, 162);
		g.drawOval(20, 20, 160, 160);
		g.drawOval(21, 21, 158, 158);
		g.drawOval(97, 97, 6, 6);
		Calendar jikoku=Calendar.getInstance();
		t=da*(double)jikoku.get(Calendar.SECOND);
		ss=Math.sin(t);
		cc=Math.cos(t);
		x=100+(int)(r*ss);
		y=100-(int)(r*cc);
		xx=100-(int)(rr*ss);
		yy=100+(int)(rr*cc);
		g.drawLine(xx, yy, x, y);
		g.setColor(Color.blue);
		m=(double)jikoku.get(Calendar.MINUTE);
		u=da*m;
		ss=Math.sin(u);
		cc=Math.cos(u);
		x=100+(int)(rrr*ss);
		y=100-(int)(rrr*cc);
		g.drawLine(100, 100, x, y);
		g.setColor(Color.red);
		h=(double)jikoku.get(Calendar.HOUR_OF_DAY);
		v=da*(5.0*(h+m/60.0));
		ss=Math.sin(v);
		cc=Math.cos(v);
		x=100+(int)(rrrr*ss);
		y=100-(int)(rrrr*cc);
		g.drawLine(100,100,x,y);

		g.drawOval(Mxx-5, Myy-5, 20, 20);
  }

	public void run()
	{
		for (int i=0; i<1000000; ++i)
		{
			repaint();
			++k;
			try {
				Thread.sleep(dt);
			}

			catch (InterruptedException e) {
			}
		}
	}
}

中割法によるアニメーション


/* http://www.h5.dion.ne.jp/~hearty1/JavaApp/ButterflyT.htmlをmodifyさせていただきました。
   ありがとうございました */
import java.awt.*;
import java.applet.*;

public class anime extends Applet implements Runnable{
   int w,h,px,py,flag;  
   double n=-2.0;
   double ts=48.0;   // 時間分割数
   int x [] = new int[121];
   int y [] = new int[121];
   double x1[] = new double[121];
   double y1[] = new double[121];
   double x2[] = new double[121];
   double y2[] = new double[121];
   double pai=Math.PI;
  
   Thread th;              // スレッド
   Image fb;               // 裏画面
   Graphics gg;            // 裏画面のグラフィックス

   public void init() {
       Dimension d=getSize();        // アプレットのサイズ	
       w=d.width;                    // 画面の横幅
       h=d.height;                   // 画面の縦幅
       flag=1;                       // Butterfly→Clover(Circle)
       setBackground(Color.gray);    // 背景色の設定
       th=null;                      // スレッド
       fb = createImage( w, h );     // 裏画面の作成  
       gg  = fb.getGraphics();       // 裏画面のグラフィックス
       Fig1();                   // Ellipse
       Fig2();                   // Circle
   }   
   
   public void start(){              // Start
       if(th==null){
          th=new Thread(this);
          th.start();
       }
   }
	
   public void stop(){                // Stop
       if(th!=null){
          th=null;
       }
   }
                
   
   public void run(){                  // Run
       while(true){ 
	 				try{Thread.sleep(200);}
	 				catch(InterruptedException e){ }
          repaint();
       }  
   }
   
   public void paint(Graphics g) {   // Paint
      gg.setColor(Color.gray);
      gg.fillRect(0,0,w,h);
      px=w/2;
      py=h/2; 
      if(flag==1)n=n+2;
      if(flag==2)n=n-2;
      if(n>ts)flag=2;n=n-2; 
      if(n<-2)flag=1;n=n+2;
      gg.setColor(Color.getHSBColor((float)(n/ts), 0.6f, 1.0f));  
                                 // HSB 色相・彩度・明度

                                 // Circle へ変形
         for ( int i=0;  i<=120;  i+=1){
          x[i]=(int)((x2[i]-x1[i])/ts*n+x1[i]);
          y[i]=(int)((y2[i]-y1[i])/ts*n+y1[i]);
          x[i]=x[i]+px; y[i]=y[i]+py;       
         }
         gg.fillPolygon(x, y, 121);
      g.drawImage(fb,0,0,this);              // 裏画面を描画
   }
   
   public void update(Graphics g){           // Update
      paint(g);
   }

   // Ellipse   
   public void Fig1() {
         double a, p=200.0, q=20.0;
         int i;

         for ( i=0, a=0.0;  a<2.0*pai;  a+=pai/60.0){ 
           x1[i]=p*Math.cos(a);
           y1[i]=q*Math.sin(a);
           i=i+1; 
         }
         x1[120]=x1[0]; y1[120]=y1[0];
         return;
    }

    // Circle
    public void Fig2() {
         double a, r=100;
         int    i;
         for ( i=0, a=0.0;  a<2.0*pai;  a+=pai/60.0){           
         x2[i]=r*Math.cos(a);
         y2[i]=r*Math.sin(a);
         i=i+1; 
         }
         x2[120]=x2[0]; y2[120]=y2[0];
         return;
    }
}  

その他