资源简介
6. 模拟风扇(满分50分)
版本1:满分 15 分
模拟实现电风扇,可以调 3 档速度(慢速、中速、快速);开关按钮;定时吹风;描述
风扇的扇叶大小、颜色等。
设计Fan 类,属性包括:3 个常量 SLOW (1)、MEDIUM(2)、FA ST(3)代表风扇的
速度;1 个int 属性speed 指定速度,默认值为 SLOW ;1 个boolean属性 on 指定开关机,默
认值false ;1 个double 属性 radius 指定风扇扇叶大小;1 个String 属性 color指定扇叶颜色,
默认值为 blue 。方法包括这些属性的访问器、构造函数、重写 Object 类的 toString() 和equals()
方法等。运行测试代码:
public static void main(String[] args) {
Fan1 fan1 = new Fan1();
fan1.setSpeed(Fan1.FAST);
fan1.setRadius(10);
fan1.setColor("yellow" );
fan1.setOn( true);
System.out .println(fan1.toString());
}
版本2:满分 15 分
修改版本 1 中Fan 类,让其继承 JPanel 类,并且把 color属性设置为 Color类型,默认
属性为 red。随机产生 radius ,取值范围为 1-5;随机产生颜色,取值范围为 red、blue 、yellow 、
green、orange ;根据 color、radius 属性值绘制风扇。运行如下图:
版本3:满分 20 分
让版本 2 中的风扇转起来。创建一个 FanControl 类包含以下内容:Start 、Stop 、Reverse
按钮,用于开启、关闭、反转控制;一个滚动条控制速度。运行示例如下:
代码片段和文件信息
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
*/
/**
* @Project name AnalogFan
* @class name Fan
* @author X-Hay
* @Date 2013-8-24
* @CQUT
* @UP.
*/
public class Fan extends JPanel implements Runnable{
public static final int SLOW = 10;
public static final int MEDIUM = 5;
public static final int FAST = 1;
private int speed = MEDIUM;
private boolean off = true;//是否关闭
private int radius;
private Color color = Color.green;
private int dushu = 1;
private boolean reverse = false;
public Fan() {
//创建并开始线程
new Thread(this).start();
}
public Fan(int speed boolean off int radius Color color) {
this.speed = speed;
this.off = off;
this.radius = radius;
this.color = color;
}
/**
* @return the speed
*/
public int getSpeed() {
return speed;
}
/**
* @param speed the speed to set
*/
public void setSpeed(int speed) {
this.speed = speed;
}
/**
* @return the on
*/
public boolean isOff() {
return off;
}
/**
* @param on the on to set
*/
public void setOff(boolean off) {
this.off = off;
}
/**
* @return the color
*/
public Color getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(Color color) {
this.color = color;
}
/**
* @return the radius
*/
public int getRadius() {
return radius;
}
/**
* @param radius the radius to set
*/
public void setRadius(int radius) {
this.radius = radius;
}
/**
* @return the reverse
*/
public boolean isReverse() {
return reverse;
}
/**
* @param reverse the reverse to set
*/
public void setReverse(boolean reverse) {
this.reverse = reverse;
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
radius = (int)(Math.min(getSize().width getSize().height)*0.4);
int x=getWidth()/2 - radius;
int y=getHeight()/2-radius;
g.setColor(Color.black);
g.drawOval(x-20 y-20 2*radius+40 2*radius+40);
g.setColor(color);
g.fillArc(x y 2*radius 2*radius 0+dushu 30);
g.fillArc(x y 2*radius 2*radius 90+dushu 30);
g.fillArc(x y 2*radius 2*radius 180+dushu 30);
g.fillArc(x y 2*radius 2*radius 270+dushu 30);
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
if(reverse){
dushu+=1;
}else {
dushu-=1;
}
repaint();
Thread.sleep(speed);
//每次检查是否需要暂停
waiting();
}
} catch (Exception e) {
// TODO: handle exception
}
}
//继续转动风扇(线程恢复)
public synchronized void resume() {
if (off) {
off = false;
notify(); //恢复所有线程
}
}
//停止转动风扇(线程暂停)
public synchronized void suspend(){
off = true;
}
//让线程等待达到停止转动的效果
private synchronized void waiting() throws InterruptedException {
while
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 301 2013-08-24 11:02 AnalogFan\.classpath
文件 385 2013-08-24 11:02 AnalogFan\.project
文件 598 2013-08-24 11:02 AnalogFan\.settings\org.eclipse.jdt.core.prefs
文件 5272 2013-08-24 11:08 AnalogFan\.settings\org.eclipse.jdt.ui.prefs
文件 3216 2013-08-27 09:49 AnalogFan\bin\Fan.class
文件 2037 2013-08-27 09:49 AnalogFan\bin\Fan1.class
文件 2562 2013-08-27 09:49 AnalogFan\bin\Fan2.class
文件 710 2013-08-27 09:49 AnalogFan\bin\FanControl$1.class
文件 711 2013-08-27 09:49 AnalogFan\bin\FanControl$2.class
文件 844 2013-08-27 09:49 AnalogFan\bin\FanControl$3.class
文件 837 2013-08-27 09:49 AnalogFan\bin\FanControl$4.class
文件 2061 2013-08-27 09:49 AnalogFan\bin\FanControl.class
文件 141 2013-08-24 12:02 AnalogFan\bin\java.policy.ap
文件 982 2013-08-27 09:49 AnalogFan\bin\Test1.class
文件 849 2013-08-27 09:49 AnalogFan\bin\Test2.class
文件 1132 2013-08-24 20:59 AnalogFan\bin\uml.umr
文件 3165 2013-08-24 15:07 AnalogFan\src\Fan.java
文件 1678 2013-08-24 11:20 AnalogFan\src\Fan1.java
文件 2780 2013-08-24 20:55 AnalogFan\src\Fan2.java
文件 2334 2013-08-24 15:06 AnalogFan\src\FanControl.java
文件 459 2013-08-24 11:44 AnalogFan\src\Test1.java
文件 580 2013-08-24 20:49 AnalogFan\src\Test2.java
文件 1132 2013-08-24 20:59 AnalogFan\src\uml.umr
目录 0 2013-08-24 11:08 AnalogFan\.settings
目录 0 2013-08-27 09:49 AnalogFan\bin
目录 0 2013-08-24 20:59 AnalogFan\src
目录 0 2013-08-24 11:02 AnalogFan
----------- --------- ---------- ----- ----
34766 27
............此处省略0个文件信息
相关资源
- 校园共享单车管理系统
- 软件工程课程设计完美的版本有源码
- 考勤管理系统 WEBjsp)课程设计 acces
- Java+固定资产管理系统课程设计源代码
- JAVA课程设计-画图板含文档、源码源码
- java程序里实现ssh scp sftp
- 银行排队模拟程序 Java课程设计 源代
- 实用JAVA软件工程课程设计
- Java课程设计报告-酒店客房管理系统
- 安卓个人通讯录课程设计报告
- Java程序开发-井字棋控制台界面
- java课程设计报告(小学数学教辅软件
- Java web课程设计宿舍管理系统数据库
- Java web 课程设计宿舍管理系统
- Java程序启动器 Java program launcher.exe
- Android账户管理课程设计源码
- (JAVA+MySQL)课程设计 学生选课管理系
- 基于SQLsever2012的Java swing停车管理系统
- Java课程设计医院门诊管理系统解压密
- 数据库课程设计,JAVA+数据库,图书管
- 航空客运订票系统JAVA课程设计
- javaweb学生选课系统课程设计
- 基于jsp的留言板课程设计
- 银行账户管理系统java程序编写
- java程序 停车场管理系统
- 吉林大学Java程序设计实验报告
- JAVA做的学生成绩管理系统带源码
- 史上最强大的java版的银行家算法
- java记事本课程设计论文
- java课程设计-计算器-功能完整的科学
评论
共有 条评论