红蓝按钮交替移动
来源:互联网 发布:机器人保姆知乎 编辑:程序博客网 时间:2024/04/30 15:15
编写一个应用程序,除了主线程外,还有两个线程:first和second。first负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60)。另外还有一个start按钮,当点击start按钮后,红色按钮平行移动从左边移动到右边,当红色按钮移动到绿色按钮位置后,红色按钮停止在绿色按钮起始位置,然后绿色按钮接着移动。当绿色按钮移动到指定位置后,所有按钮位置重置,然后循环执行上述过程。
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.JFrame;
public classMoveButton extends JFrame implements Runnable, ActionListener {
private int distance=10;
Thread first,second;
Button redButton,greenButton,startButton;
/*
* 构造方法进行初始化
*/
public MoveButton(){
/*
* 创建线程
*/
first=new Thread(this);
second=new Thread(this);
setLayout(null);//清除默认布局
/*
* 设置红色按钮的颜色
* 设置起始位置与大小
* 加入窗体
*/
redButton=new Button();
redButton.setBackground(Color.red);
redButton.setBounds(10, 60, 15, 15);
add(redButton);
/*
* 设置红色按钮的颜色
* 设置起始位置与大小
* 加入窗体
*/
greenButton=new Button();
greenButton.setBackground(Color.green);
greenButton.setBounds(100,60,15,15);
add(greenButton);
/*
* 设置开始按钮的起始位置与大小
* 添加监听器
* 加入窗体
*/
startButton=newButton("start");
startButton.addActionListener(this);
startButton.setBounds(10, 100, 30, 30);
add(startButton);
setBounds(0, 0, 300, 200);//设置窗体的起始位置与长宽
setVisible(true);//设置窗体可见
/*
* 关闭窗时释放内存资源
*/
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvente){
System.exit(0);
}
});
}
/*
* 实现按钮的移动
*synchronized 方法控制对类成员变量的访问
*/
private synchronized void moveComponent(Button button) {
if(Thread.currentThread() == first){
while(distance>100 &&distance<=200){
try {
wait();
System.out.println("你好");//线程等待,直到其他线程调用notify或notifyAll方法通知该线程醒来
} catch (InterruptedException e) {
e.printStackTrace();
}
}
distance+=1;
button.setLocation(distance, 60);
if(distance>=100){
if(distance<=200){
button.setLocation(100, 60);//在蓝色按钮运动期间红色按钮始终位于蓝色按钮最初位置
}else{
button.setLocation(10, 60);//当距离移动距离大于200时,蓝色按钮归位
}
notify();//唤醒单个等待的线程(由于约束条件的存在,此程序中势必只有一个等待的线程,故可用此方法替换)
//notifyAll();//唤起全部等地的线程
}
}
if(Thread.currentThread() == second){
while(distance>=10 &&distance<100){
try {
wait();//线程等待,直到其他线程调用notify或notifyAll方法通知该线程醒来
} catch (InterruptedException e) {
e.printStackTrace();
}
}
distance+=1;
button.setLocation(distance, 60);
if(distance>200){
button.setLocation(100, 60);//当距离移动距离大于200时,蓝色按钮归位
distance=10;//distance置初值
notify();//
}
}
}
/*
*赋写Run()方法
*/
public void run() {
while(true){
/*
* 判断当前执行的线程是否为first
* 如果是,调用moveComponent()方法,移动redButton
* 线程睡眠20ms
*/
if(Thread.currentThread()==first){
moveComponent(redButton);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*
* 判断当前执行的线程是否为second
* 如果是,调用moveComponent()方法,移动greenButton
* 线程睡眠10ms
*/
if(Thread.currentThread()==second){
moveComponent(greenButton);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
* 事件监听,启动线程(由于只对startButton按钮绑定监听器,所以默认监听该按钮)
*/
public void actionPerformed(ActionEvent e) {
try {
first.start();//启动线程
second.start();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
new MoveButton();
}
}
- 红蓝按钮交替移动
- 北京理工大学虚拟现实作业之红蓝交替
- 我国电信运营商格局趋向:中移动的消失,蓝色系和红色系的抗衡
- 12[红+绿+蓝+Alpha]
- 蓝桥 移动距离
- 蓝魔rm600+ 红屏解决方案
- 红R绿G蓝B
- 红蓝3D的原理
- 随机生成双色球彩票红,蓝数字
- HTML之变色(蓝、绿、红)
- 移动按钮
- JQuery 实现鼠标移动的动态交替
- 红点移动的原理
- 红点移动的原理
- Game Programming with DirectX -- 12[红+绿+蓝+Alpha]
- 红蓝眼镜支持的stero渲染(立体眼镜)
- Game Programming with DirectX -- 12[红+绿+蓝+Alpha]
- direct 3d 渲染红蓝立体效果
- 知乎的本质
- 124象棋(14)
- jsp超链接调用action时的传参数
- ~/.bashrc什么意思
- Deep Learning(深度学习)
- 红蓝按钮交替移动
- 字符串基本操作
- CODEVS 1380 没有上司的舞会
- sqoop系列(一)
- 第十一周项目1——二叉树算法验证(4) 哈夫曼编码的算法验证
- 如何开发AR增强现实应用与产品
- Android 实现图片缩放和拖动
- 聊天室项目(一)框架搭建
- html5 设备管理信息 device