java GUI输出一个空心菱形
时间: 2023-04-11 09:01:43 浏览: 94
Java输出一个空心菱形
好的,以下是Java GUI输出一个空心菱形的代码:
```
import java.awt.*;
import javax.swing.*;
public class Diamond extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int[] xPoints = {150, 200, 150, 100};
int[] yPoints = {100, 150, 200, 150};
g.drawPolygon(xPoints, yPoints, 4);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Diamond");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Diamond diamond = new Diamond();
diamond.setBackground(Color.WHITE);
frame.add(diamond);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
```
希望这个代码可以帮到你!
阅读全文