资源简介

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.applet

     文件        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个文件信息

评论

共有 条评论