📄 simpledraw.java
字号:
//SimpleDraw
import java.awt.*;
import javax.swing.*;
public class SimpleDraw
{
public static void main(String[] args)
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class DrawFrame extends JFrame
{
public DrawFrame()
{
setTitle("简单图形绘制");
setSize(WIDTH, HEIGHT);
// 将panel加入到frame
DrawPanel panel = new DrawPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x1 = 50;
int y1 = 50;
int x2 = 50;
int y2 = 150;
int radius = 100; //半径
int startAngle = - 90; //起始角度
int arcAngle = 180; //弧的角度
//画线
g.drawLine(x1, y1, x2, y2);
//画弧
g.drawArc(x1 - radius / 2 , y1 ,
radius, radius, startAngle, arcAngle);
Polygon p = new Polygon();
x1 += 150;
y1 += 50;
radius /= 2;
int i;
for (i = 0; i < 6; i++)
p.addPoint(
(int)(x1 + radius * Math.cos(i * 2 * Math.PI / 6)),
(int)(y1 + radius * Math.sin(i * 2 * Math.PI / 6)));
//画六边形
g.drawPolygon(p);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -