绘画空心圆,java
时间: 2024-03-20 08:36:42 浏览: 40
在Java中绘制空心圆可以使用Graphics2D类的draw方法来实现。下面是一个简单的示例代码:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HollowCircleExample extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int radius = 100;
g2d.setColor(Color.BLACK);
g2d.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Hollow Circle Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.add(new HollowCircleExample());
frame.setVisible(true);
}
}
```
这段代码创建了一个继承自JPanel的类HollowCircleExample,重写了paintComponent方法,在该方法中使用Graphics2D类的drawOval方法绘制了一个空心圆。然后在main方法中创建了一个JFrame窗口,并将HollowCircleExample实例添加到窗口中显示。
阅读全文