java实现绘制空心半圆,空心圆形,空心矩形 ,空心三角形,空心梯形
时间: 2023-07-31 14:11:23 浏览: 163
空心半圆:
```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);
}
}
```
阅读全文