?? testanimator.java
字號:
package com.test;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
public class TestAnimator extends JPanel{
private boolean stop = false;
Point lastP = new Point(100,100);
int ballW = 50,ballH =50;
int slope = 1;
int x , y ;
public TestAnimator(){
super();
x = lastP.x;
y = lastP.y;
moveBall();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setPreferredSize(new Dimension(800, 600));
Toolkit tk=Toolkit.getDefaultToolkit();
Image img=tk.getImage("c:/車.jpg");
g.drawOval(x,y,ballW,ballH);
}
private void moveBall(){
Thread t = new Thread(){
public void run(){
while (stop == false){
try{
Thread.sleep(100);
x += 10;
y=x*slope;
repaint();
System.out.println("----------x = " + x + " y = " + y);
if (x + ballW > 800 || y + ballH > 600)
stop = true;
}catch (Exception e){
e.printStackTrace();
}
}
}
};
t.start();
}
public static void main(String[] arg){
JFrame f = new JFrame();
TestAnimator tb = new TestAnimator();
f.getContentPane().add(tb);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
//http://www.7880.com/Info/Article-861dcd20.html
//http://hi.baidu.com/lizhao_yiqing/blog/item/4161511378c803075aaf530c.html
//http://topic.csdn.net/t/20060109/11/4505546.html
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -