?? animator.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Animator extends JDialog
{
private AnimatorPanel animatorPanel; //繪制動畫的面板
//private JButton startButton, stopButton;
//private JPanel buttonPanel; //存放開始按鈕和結束按鈕的面板
public Animator()
{
//super("給點獎勵吧!!!!!!!!!!");
super(new Frame(), "給點獎勵吧!!!!!!!!!!", false);
// setSize(400, 400);
this.setBounds(180,150,400,400);
//獲取內容面板
Container container = getContentPane();
//創建用于繪制圖像的面板
animatorPanel = new AnimatorPanel();
container.add(animatorPanel, BorderLayout.CENTER);
//設置字體
Font font = new Font("Serif", Font.PLAIN, 14);
//container.add(buttonPanel, BorderLayout.SOUTH);
setVisible(false);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//處理按鈕事件
/* public void actionPerformed(ActionEvent event)
{
//if(event.getSource() == startButton)
animatorPanel.start();
// else if(event.getSource() == stopButton)
// animatorPanel.stop();
}*/
/* public static void main(String[] args)
{
AnimatorDemo application = new AnimatorDemo();
}*/
class AnimatorPanel extends JPanel implements ActionListener
{
private ImageIcon images[];
private int currentImage = 0; //當前幀
private Timer timer; //定時器
private int x = -100, y; //圖像的坐標
public AnimatorPanel()
{
super();
setBackground(Color.WHITE);
images = new ImageIcon[4]; //創建圖像對象數組
for(int i = 0; i < images.length; i++)
{ //載入圖像
images[i] = new ImageIcon("boy0" + (i+1) + ".png");
}
//this.setVisible(false);
}
public void paintComponent(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
//繪制圖像,坐標為屏幕中心
y = (this.getHeight() - images[currentImage].getIconHeight())/2;
images[currentImage].paintIcon(this, g, x, y);
//定時器正在運行
currentImage = (++currentImage)%4;
animatorPanel.start();
}
//開始動畫
public void start()
{
if(timer == null)
{
currentImage = 0;
timer = new Timer(100, this); //創建定時器
timer.start();
}
else if(!timer.isRunning())
timer.restart();
//timer.stop();
//startButton.setEnabled(false);
// stopButton.setEnabled(true);
}
//動畫暫停
public void stop()
{
timer.stop();
//startButton.setEnabled(true);
//stopButton.setEnabled(false);
}
//處理定時器事件
public void actionPerformed(ActionEvent event)
{
x += 10;
if(x >= 450)
x=-100;
repaint();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -