📄 threadballtest.java
字号:
/*
* ThreadBallTest.java
*
* Created on 2007年12月23日, 上午8:29
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.xifei;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
/**
*
* @author Administrator
*/
public class ThreadBallTest extends Frame{
/** Creates a new instance of ThreadBallTest */
public ThreadBallTest() {
super();
this.setSize(800,600);
this.setVisible(true);
this.setLayout(null);
this.setFocusableWindowState(true);
ballPanel b1=new ballPanel();
b1.setBounds(0,0,400,600);
ballPanel b2=new ballPanel();
this.add(b1);
b2.setBounds(400,0,400,600);
this.add(b2);
Thread t1=new Thread(b1);
Thread t2=new Thread(b2);
t1.start();
t2.start();
}
public static void main(String args[]){
new ThreadBallTest();
}
}
class ballPanel extends Panel implements Runnable{
private int x=(int)(Math.random()*10);
private int y=(int)(Math.random()*100);
private boolean bx=false;
private boolean by=false;
public void paint(Graphics g){
g.fillOval(x,y,15,15);
}
public void run(){
while(true){
if(x>400)
bx=true;
if(x<0)
bx=false;
if(y>600)
by=true;
if(y<0)
by=false;
if(bx)
x--;
else
x++;
if(by)
y--;
else
y++;
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
this.repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -