2013-11-14 49 views
0

如何在Java中創建基本動畫?創建基本動畫

目前我正在嘗試使用Java中的swing實現基本的動畫程序。 但我不明白我的程序邏輯是否正確。我的程序實現Runnable,ActionListener接口,並且還擴展JApplet。 我想知道,是否有必要區分JApplet和Runnable的啓動方法?

如果是,那麼爲什麼..?

我的程序是基本的氣球程序,當我點擊開始按鈕氣球開始向上移動並再次到達地面。這將繼續,直到我按停止按鈕。

這是我的代碼。

public class Balls extends JApplet implements Runnable{ 

    private static final long serialVersionUID = 1L; 
    JPanel btnPanel=new JPanel(); 
    static boolean flag1=true; 
    static boolean flag2=true; 
    static boolean flag3=false; 
    static int h; 
    static int temp=10; 
    Thread t=new Thread(this); 

    JButton start; 
    JButton stop; 


    public void init() 
    { 
     try 
     { 
      SwingUtilities.invokeAndWait(

       new Runnable() 
       { 
        public void run() 
        { 
         makeGUI(); 
        } 


       });       

     } 
     catch(Exception e) 
     { 
      JOptionPane.showMessageDialog(null,"Can't create GUI because of exception"); 
      System.exit(0); 
     } 

    } 


    private void makeGUI() { 

     start=new JButton("start"); 
     stop=new JButton("stop"); 
     btnPanel.add(start); 
     btnPanel.add(stop); 
     add(btnPanel,BorderLayout.NORTH); 
    } 

    public void run() 
    {   
     while(true) 
     { 
      if(flag1) 
      { 
       repaint(); 
       flag1=false; 
      } 
      else 
      { 
       try 
       { 
        wait(); 
        flag3=true; 
       } 
       catch(InterruptedException e) 
       { 
        JOptionPane.showMessageDialog(null,"Error ocuured !!\n Exiting.."); 
        System.exit(0); 
       } 
      } 
     } 

    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     int h=Integer.parseInt(getParameter("height")); 
     if (flag1) 
     { 
      g.setColor(Color.RED); 
      g.fillOval(10,h,50,50); 
      g.setColor(Color.YELLOW); 
      g.fillOval(50,h,20,20); 
      g.setColor(Color.CYAN); 
      g.fillOval(70,h,80,30); 
      g.setColor(Color.BLUE); 
      g.fillOval(120,h,50,60); 
      g.setColor(Color.GRAY); 
      g.fillOval(160,h,70,50); 
      g.setColor(Color.GREEN); 
      g.fillOval(200,h,80,80); 
      g.setColor(Color.MAGENTA); 
      g.fillOval(260,h,80,30); 
      g.setColor(Color.DARK_GRAY); 
      g.fillOval(320,h,60,40); 
      g.setColor(Color.pink); 
      g.fillOval(370,h,65,45); 
      flag1=false; 
     } 
     else 
     { 
      g.setColor(Color.RED); 
      g.fillOval(10,h-temp,50,50); 
      g.setColor(Color.YELLOW); 
      g.fillOval(50,h-temp,20,20); 
      g.setColor(Color.CYAN); 
      g.fillOval(70,h-temp,80,30); 
      g.setColor(Color.BLUE); 
      g.fillOval(120,355,50,60); 
      g.setColor(Color.GRAY); 
      g.fillOval(160,h-temp,70,50); 
      g.setColor(Color.GREEN); 
      g.fillOval(200,h-temp,80,80); 
      g.setColor(Color.MAGENTA); 
      g.fillOval(260,h-temp,80,30); 
      g.setColor(Color.DARK_GRAY); 
      g.fillOval(320,h-temp,60,40); 
      g.setColor(Color.pink); 
      g.fillOval(370,h-temp,65,45); 
      if(flag2 && temp<=400) 
      { 
       temp+=10; 
       if(temp==400) 
       { 
        flag2=false; 
       } 
      } 
      else if(!flag2) 
      { 
       temp-=10; 
       if(temp==10) 
       { 
        flag2=true; 
       } 
      } 
      else 
      { 

      } 
     } 
    } 

    public void start() 
    { 
     start.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       t.start(); 
       if(flag3) 
       { 
        notify(); 
       } 

      } 
     }); 
     stop.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       flag1=false; 
       try 
       { 
        Thread.sleep(5000); 
       } 
       catch(InterruptedException e) 
       { 
        repaint(); 
        t=null; 
       } 
      } 
     }); 

    }  
} 
+0

通常java中的動畫需要使用[**'SwingTimer' **](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html),也有庫。但我真的沒有嘗試'JApplet',我不知道它是如何工作的。 –

+0

http://cs.wellesley.edu/~cs111/fall05/labs/lab12/animationDemo.html這是一個鏈接,我從這個鏈接中得到了想法。他們使用了Applet。 –

+0

非常好的網站,各種各樣的例子 http://www.java2s.com/Code/Java/Advanced-Graphics/Animation.htm –

回答

1

1)使用SwingTimer建議您Runnable實現的,而不是。

2)閱讀custom paintingand here

3)在JPanel,而不是上繪製JFrame

我已經改變了你的代碼,研究它。我認爲,它是做你想做的。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class Balls extends JApplet { 

    private static final long serialVersionUID = 1L; 
    JPanel btnPanel = new JPanel(); 
    static boolean flag1 = true; 
    static boolean flag2 = true; 
    static boolean flag3 = false; 
    static int h; 
    static int temp = 10; 
    JButton start; 
    JButton stop; 
    private Timer timer; 

    public void init() { 
      SwingUtilities.invokeLater(

      new Runnable() { 
       public void run() { 
        makeGUI(); 
       } 

      }); 
    } 

    private void makeGUI() { 
     timer = new Timer(10, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       repaint(); 
      } 
     }); 
     start = new JButton("start"); 
     stop = new JButton("stop"); 
     start.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       timer.start(); 
      } 
     }); 
     stop.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       timer.stop(); 
      } 
     }); 
     btnPanel.add(start); 
     btnPanel.add(stop); 
     add(new MyPanel(), BorderLayout.CENTER); 
     add(btnPanel, BorderLayout.NORTH); 
    } 

    class MyPanel extends JPanel{ 

     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      int h = Integer.parseInt(getParameter("height")); 
      if (flag1) { 
       g.setColor(Color.RED); 
       g.fillOval(10, h, 50, 50); 
       g.setColor(Color.YELLOW); 
       g.fillOval(50, h, 20, 20); 
       g.setColor(Color.CYAN); 
       g.fillOval(70, h, 80, 30); 
       g.setColor(Color.BLUE); 
       g.fillOval(120, h, 50, 60); 
       g.setColor(Color.GRAY); 
       g.fillOval(160, h, 70, 50); 
       g.setColor(Color.GREEN); 
       g.fillOval(200, h, 80, 80); 
       g.setColor(Color.MAGENTA); 
       g.fillOval(260, h, 80, 30); 
       g.setColor(Color.DARK_GRAY); 
       g.fillOval(320, h, 60, 40); 
       g.setColor(Color.pink); 
       g.fillOval(370, h, 65, 45); 
       flag1 = false; 
      } else { 
       g.setColor(Color.RED); 
       g.fillOval(10, h - temp, 50, 50); 
       g.setColor(Color.YELLOW); 
       g.fillOval(50, h - temp, 20, 20); 
       g.setColor(Color.CYAN); 
       g.fillOval(70, h - temp, 80, 30); 
       g.setColor(Color.BLUE); 
       g.fillOval(120, 355, 50, 60); 
       g.setColor(Color.GRAY); 
       g.fillOval(160, h - temp, 70, 50); 
       g.setColor(Color.GREEN); 
       g.fillOval(200, h - temp, 80, 80); 
       g.setColor(Color.MAGENTA); 
       g.fillOval(260, h - temp, 80, 30); 
       g.setColor(Color.DARK_GRAY); 
       g.fillOval(320, h - temp, 60, 40); 
       g.setColor(Color.pink); 
       g.fillOval(370, h - temp, 65, 45); 
       if (flag2 && temp <= 400) { 
        temp += 10; 
        if (temp == 400) { 
         flag2 = false; 
        } 
       } else if (!flag2) { 
        temp -= 10; 
        if (temp == 10) { 
         flag2 = true; 
        } 
       } else { 

       } 
      } 
     } 
    } 
} 
+0

您的修改代碼有幫助我提高了我的概念,但仍然給錯誤,謝謝答覆。謝謝 –

+0

錯誤?哪裏?哪一個?我運行我的例子它工作正常 – alex2410

+0

W7,你添加進口到你的班級? – alex2410