Files
2025ClassOfJava/C05 Extends/src/test/ShapeDrawerTest.java
2025-10-29 00:52:52 +08:00

56 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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