第七次实验课代码
添加C04~C07
This commit is contained in:
11
C05 Extends/C05 Extends.iml
Normal file
11
C05 Extends/C05 Extends.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
35
C05 Extends/src/shape/Circle.java
Normal file
35
C05 Extends/src/shape/Circle.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package shape;
|
||||
import java.awt.*;
|
||||
|
||||
public class Circle extends Shape {
|
||||
private int radius;
|
||||
|
||||
@Override
|
||||
public double getArea(){
|
||||
return Math.PI*radius*radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPerimeter(){
|
||||
return 2*Math.PI*radius;
|
||||
}
|
||||
@Override
|
||||
public void draw(Graphics g) {
|
||||
g.setColor(color);
|
||||
// 绘制圆形
|
||||
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
|
||||
|
||||
// 绘制信息
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawString(String.format("面积: %.2f", getArea()), x + radius + 5, y);
|
||||
g.drawString(String.format("周长: %.2f", getPerimeter()), x + radius + 5, y + 15);
|
||||
}
|
||||
|
||||
public Circle(){
|
||||
this(Color.RED,100,100,50);
|
||||
}
|
||||
public Circle(Color color,int x,int y,int radius){
|
||||
super(color,x,y);
|
||||
this.radius=radius;
|
||||
}
|
||||
}
|
||||
14
C05 Extends/src/shape/Rectangle.java
Normal file
14
C05 Extends/src/shape/Rectangle.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package shape;
|
||||
import java.awt.*;
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
private int width,height;
|
||||
public Rectangle() {
|
||||
super();
|
||||
}
|
||||
public Rectangle(Color color, int x, int y, int width, int height) {
|
||||
super(color, x, y);
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
52
C05 Extends/src/shape/Shape.java
Normal file
52
C05 Extends/src/shape/Shape.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package shape;
|
||||
import java.awt.*;
|
||||
|
||||
public class Shape {
|
||||
protected Color color;
|
||||
protected int x,y;
|
||||
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
public double getArea(){
|
||||
return x*y;
|
||||
}
|
||||
|
||||
public void setX(int x){
|
||||
this.x=x;
|
||||
}
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setY(int y){
|
||||
this.y=y;
|
||||
}
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
|
||||
public double getPerimeter(){
|
||||
return x*y;
|
||||
}
|
||||
|
||||
public void move(int dx,int dy){
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
public void draw(Graphics g){
|
||||
g.setColor(color);
|
||||
g.fillRect(x,y,1,1);
|
||||
}
|
||||
|
||||
Shape(){}
|
||||
Shape(Color color,int x,int y){
|
||||
this.color=color;
|
||||
this.x=x;
|
||||
this.y=y;
|
||||
}
|
||||
}
|
||||
55
C05 Extends/src/test/ShapeDrawerTest.java
Normal file
55
C05 Extends/src/test/ShapeDrawerTest.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package test;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import shape.Circle;
|
||||
|
||||
public class ShapeDrawerTest extends JFrame {
|
||||
/*******请在下面添加**************/
|
||||
// 【任务1】定义一个包含5个元素的圆形数组
|
||||
Circle[] circles = new Circle[5];
|
||||
/*******请在上面添加**************/
|
||||
|
||||
// 初始化图形
|
||||
private void initShapes() {
|
||||
/*******请在下面添加**************/
|
||||
// 【任务2】采用无参构造添加一个红色,坐标为(100,100),半径为50的圆形,并加入对应数组
|
||||
circles[0] = new Circle();
|
||||
// 【任务3】采用有参构造添加一个黄色,坐标为(150,300),半径为70的圆形,并加入对应数组
|
||||
circles[1] = new Circle(Color.YELLOW, 150, 300, 70);
|
||||
circles[2] = new Circle();
|
||||
circles[3] = new Circle();
|
||||
circles[4] = new Circle();
|
||||
/*******请在上面添加**************/
|
||||
}
|
||||
|
||||
// 重写绘制方法
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
/*******请在下面添加**************/
|
||||
// 绘制所有图形
|
||||
// 【任务4】采用增强型for循环绘制所有图形
|
||||
for (Circle circle : circles) {
|
||||
circle.draw(g);
|
||||
}
|
||||
/*******请在上面添加**************/
|
||||
// 绘制标题
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawString("Java继承图形绘制示例", 300, 30);
|
||||
}
|
||||
public ShapeDrawerTest() {
|
||||
// 初始化窗口
|
||||
setTitle("图形绘制程序");
|
||||
setSize(800, 600);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
// 创建各种图形对象
|
||||
initShapes();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
new ShapeDrawerTest().setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user