java 画一个空心圆
时间: 2023-06-06 07:05:08 浏览: 135
好的,下面是Java语言画一个空心圆的代码:
```
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HollowCircle extends JPanel {
public void paint(Graphics g) {
g.drawOval(50,50,100,100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new HollowCircle());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
```
这段代码可以在Java环境下直接运行。
相关问题
绘画空心圆,java
在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实例添加到窗口中显示。
java实现绘制空心半圆,空心圆形,空心矩形 ,空心三角形,空心梯形
空心半圆:
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HalfCircle extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
// 画空心半圆
g.drawArc(50, 50, 100, 100, 0, -180);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new HalfCircle());
frame.setSize(200, 200);
frame.setVisible(true);
}
}
```
空心圆形:
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Circle extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
// 画空心圆形
g.drawOval(50, 50, 100, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Circle());
frame.setSize(200, 200);
frame.setVisible(true);
}
}
```
空心矩形:
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Rectangle extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
// 画空心矩形
g.drawRect(50, 50, 100, 50);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Rectangle());
frame.setSize(200, 200);
frame.setVisible(true);
}
}
```
空心三角形:
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Triangle extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
// 画空心三角形
g.drawLine(50, 100, 150, 100);
g.drawLine(50, 100, 100, 50);
g.drawLine(100, 50, 150, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Triangle());
frame.setSize(200, 200);
frame.setVisible(true);
}
}
```
空心梯形:
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Trapezoid extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
// 画空心梯形
g.drawLine(50, 100, 100, 50);
g.drawLine(100, 50, 150, 50);
g.drawLine(150, 50, 200, 100);
g.drawLine(50, 100, 200, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Trapezoid());
frame.setSize(250, 150);
frame.setVisible(true);
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)