第七次实验课代码

添加C04~C07
This commit is contained in:
2025-10-29 00:52:52 +08:00
parent 1986bbbe3e
commit 1c869f816f
24 changed files with 761 additions and 3 deletions

View 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>

View File

@@ -0,0 +1,49 @@
package shape;
import java.awt.*;
public class Circle extends Shape {
private int radius;
public void setRadius(int r) {
radius = r;
}
public int getRadius() {
return 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 void drawCenterMark(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(x-5,y,x+5,y);
g.drawLine(x,y-5,x,y+5);
g.drawString("O", x+5, y+5);
}
public Circle(){
super();
}
public Circle(Color color,int x,int y,int radius){
super(color,x,y);
this.radius=radius;
}
}

View File

@@ -0,0 +1,56 @@
package shape;
import java.awt.*;
public class Rectangle extends Shape {
private int width,height;
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
@Override
public double getArea() {
return width*height;
}
@Override
public double getPerimeter() {
return 2*(width+height);
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x, y, width, height);
// 绘制信息
g.setColor(Color.BLACK);
g.drawString(String.format("面积: %.2f", getArea()), x + width + 5, y);
g.drawString(String.format("周长: %.2f", getPerimeter()), x + width + 5, y + 15);
}
public void drawDiagonals(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(x, y, x+width, y+height);
g.drawLine(x, y+height, x+width, y);
}
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;
}
}

View 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;
}
}

View File

@@ -0,0 +1,77 @@
package test;
import shape.Circle;
import shape.Rectangle;
import javax.swing.*;
import java.awt.*;
public class ShapeDrawerTest extends JFrame {
/*******请在下面添加**************/
// 【任务1】定义一个包含5个元素的圆形数组
Circle[] circles = new Circle[5];
Rectangle[] rectangles = new Rectangle[2];
private shape.Shape[] shapes = new shape.Shape[4];
/*******请在上面添加**************/
// 初始化图形
private void initShapes() {
/*******请在下面添加**************/
// 【任务2】采用无参构造添加一个红色坐标为(100,100)半径为50的圆形并加入对应数组
shapes[0] = new Circle();
((Circle)shapes[0]).setX(100);
((Circle)shapes[0]).setY(100);
((Circle)shapes[0]).setRadius(50);
((Circle)shapes[0]).setColor(Color.RED);
// 【任务3】采用有参构造添加一个黄色坐标为(150,300)半径为70的圆形并加入对应数组
shapes[1] = new Circle(Color.YELLOW, 150, 300, 70);
// 【任务4】采用无参构造添加一个绿色坐标为(250,80)宽为100高为80的矩形并加入对应数组
shapes[2] = new Rectangle();
((Rectangle)shapes[2]).setX(250);
((Rectangle)shapes[2]).setY(80);
((Rectangle)shapes[2]).setWidth(100);
((Rectangle)shapes[2]).setHeight(80);
((Rectangle)shapes[2]).setColor(Color.GREEN);
// 【任务5】采用有参构造添加一个粉色坐标为(300,300)宽为120高为60的矩形
shapes[3] = new Rectangle(Color.PINK, 300, 300, 120, 60);
// 【任务6】下移动第二个矩形50个单位
((Rectangle)shapes[3]).setY(this.getY()-50);
/*******请在上面添加**************/
}
// 重写绘制方法
@Override
public void paint(Graphics g) {
super.paint(g);
/*******请在下面添加**************/
// 绘制所有图形
// 【任务4】采用增强型for循环绘制所有图形
for (shape.Shape shape : shapes) {
shape.draw(g);
if (shape instanceof Circle) {
((Circle)shape).drawCenterMark(g);
}
if (shape instanceof Rectangle) {
((Rectangle)shape).drawDiagonals(g);
}
}
/*******请在上面添加**************/
// 绘制标题
g.setColor(Color.BLACK);
g.drawString("Java继承图形绘制示例", 300, 50);
}
public ShapeDrawerTest() {
// 初始化窗口
setTitle("图形绘制程序");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建各种图形对象
initShapes();
}
public static void main(String[] args) {
new ShapeDrawerTest().setVisible(true);
}
}