📄 drawgraphicsdemo.java
字号:
package chapter11;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawGraphicsDemo extends JFrame {
public DrawGraphicsDemo() {
setTitle("DrawGraphicsDemo ");
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;
public static void main(String[] args) {
DrawGraphicsDemo frame = new DrawGraphicsDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
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.drawRect(20, 20, 50, 50);
g.setColor(Color.RED);
// 画弧
g.drawArc(x1 - radius / 2, y1, radius, radius, startAngle, arcAngle);
int x[] = { 90, 150, 140, 90 };
int y[] = { 130, 140, 170, 170 };
g.drawPolygon(x, y, 4);
g.drawOval(160, 130, 60, 40);
g.fillOval(230, 130, 60, 40);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -