模拟实现电风扇,可以调3档速度(慢速、中速、快速);开关按钮;定时吹风;描述风扇的扇叶大小、颜色等。 设计Fan类,属性包括:3个常量SLOW(1)、MEDIUM(2)、FAST(3)代表风扇的速度;1个int属性speed指定速度,默认值为SLOW;1个boolean属性on指定开关机,默认值false;1个double属性radius指定风扇扇叶大小;1个String属性color指定扇叶颜色,默认值为blue。方法包括这些属性的访问器、构造函数、重写Object类的toString()和equals()方法等。修改版本1中Fan类,让其继承Pane类,并且把color属性设置为Color类型,默认属性为red。随机产生radius,取值范围为1-5;随机产生颜色,取值范围为red、blue、yellow、green、orange;根据color、radius属性值绘制风扇。。用javafx写出版本二代码
时间: 2024-03-27 14:38:43 浏览: 203
版本一代码:
```java
public class Fan {
private final int SLOW = 1;
private final int MEDIUM = 2;
private final int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "blue";
public Fan() {}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Fan{" +
"speed=" + speed +
", on=" + on +
", radius=" + radius +
", color='" + color + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Fan fan = (Fan) o;
return speed == fan.speed &&
on == fan.on &&
Double.compare(fan.radius, radius) == 0 &&
Objects.equals(color, fan.color);
}
@Override
public int hashCode() {
return Objects.hash(speed, on, radius, color);
}
}
```
版本二代码:
```java
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import java.util.Random;
public class Fan extends Pane {
private final int SLOW = 1;
private final int MEDIUM = 2;
private final int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius;
private Color color;
public Fan() {
this.radius = new Random().nextInt(5) + 1;
this.color = generateRandomColor();
drawFan();
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
private void drawFan() {
double centerX = getWidth() / 2;
double centerY = getHeight() / 2;
double fanRadius = radius * 20;
Circle circle = new Circle(centerX, centerY, fanRadius, Color.TRANSPARENT);
circle.setStroke(color);
getChildren().add(circle);
for (int i = 0; i < 4; i++) {
Arc arc = new Arc(centerX, centerY, fanRadius - 10, fanRadius - 10, i * 90 + 20, 40);
arc.setFill(Color.TRANSPARENT);
arc.setStroke(color);
arc.setType(ArcType.ROUND);
getChildren().add(arc);
}
}
private Color generateRandomColor() {
Random random = new Random();
int colorIndex = random.nextInt(5);
switch (colorIndex) {
case 0:
return Color.RED;
case 1:
return Color.BLUE;
case 2:
return Color.YELLOW;
case 3:
return Color.GREEN;
case 4:
return Color.ORANGE;
default:
return Color.BLACK;
}
}
@Override
public String toString() {
return "Fan{" +
"speed=" + speed +
", on=" + on +
", radius=" + radius +
", color='" + color + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Fan fan = (Fan) o;
return speed == fan.speed &&
on == fan.on &&
Double.compare(fan.radius, radius) == 0 &&
Objects.equals(color, fan.color);
}
@Override
public int hashCode() {
return Objects.hash(speed, on, radius, color);
}
}
```
阅读全文