Java绘制一个平面直角坐标系,以坐标(100,100)为圆心,画一个半径为100的圆和它的内切矩阵,并填充该矩阵
时间: 2024-05-09 11:15:43 浏览: 114
画直角坐标系
4星 · 用户满意度95%
以下是Java代码,用于绘制平面直角坐标系、圆和内切矩阵,并填充矩阵:
```
import java.awt.*;
import javax.swing.*;
public class CoordinateSystem extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
// 绘制平面直角坐标系
g.setColor(Color.BLACK);
g.drawLine(0, height/2, width, height/2);
g.drawLine(width/2, 0, width/2, height);
for (int i = 0; i <= width; i += 50) {
g.drawLine(i, height/2-5, i, height/2+5);
g.drawString("" + (i-width/2), i-10, height/2+20);
}
for (int i = 0; i <= height; i += 50) {
g.drawLine(width/2-5, i, width/2+5, i);
g.drawString("" + (height/2-i), width/2+10, i+5);
}
// 绘制圆和内切矩阵
g.setColor(Color.BLUE);
int x = 100;
int y = 100;
int r = 100;
int rectWidth = (int)(r * Math.sqrt(2));
int rectX = x - rectWidth/2;
int rectY = y - rectWidth/2;
g.drawOval(x-r, y-r, 2*r, 2*r);
g.drawRect(rectX, rectY, rectWidth, rectWidth);
g.fillRect(rectX, rectY, rectWidth, rectWidth);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("平面直角坐标系");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
CoordinateSystem panel = new CoordinateSystem();
frame.add(panel);
frame.setVisible(true);
}
}
```
运行该程序,即可看到绘制好的平面直角坐标系、圆和内切矩阵。
阅读全文