📄 ecurve.java
字号:
package ch06.section03;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class ECurve
extends JPanel {
final static Color bg = Color.white;
final static Color fg = Color.black;
final static Color red = Color.red;
final static Color white = Color.white;
final static BasicStroke stroke = new BasicStroke(1.0f);
final static float dash1[] = {
10.0f};
final static BasicStroke dashed = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
public ECurve() {
setBackground(bg);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawShapes(g2);
}
static void drawShapes(Graphics2D g2) {
int gridWidth = 600 / 6;
int gridHeight = 250 / 2;
int rowspacing = 5;
int columnspacing = 7;
int rectWidth = gridWidth - columnspacing;
int rectHeight = gridHeight - rowspacing;
Color fg3D = Color.lightGray;
g2.setPaint(fg3D);
g2.drawRect(50, 50, 505 - 1, 130);
g2.setPaint(fg);
int x = 55;
int y = 57;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(stroke);
// 画一个二次曲线
g2.draw(new QuadCurve2D.Double(x, y, x + (int) (rectWidth / 2),
y + rectHeight, x + rectWidth, y));
x += gridWidth;
//画一个三次曲线
g2.draw(new CubicCurve2D.Double(x, y, x + (int) (rectWidth / 2),
y + rectHeight, x + (int) (rectWidth / 2),
y + rectHeight, x + rectWidth, y));
x += gridWidth;
// 画一段圆弧
g2.draw(new Arc2D.Double(x, y, rectWidth, rectWidth, 90,
180, Arc2D.OPEN));
x += gridWidth;
// 画一个圆
g2.draw(new Ellipse2D.Double(x, y, rectWidth, rectWidth));
x += gridWidth;
// 画一个椭圆
g2.draw(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
x += gridWidth;
}
public static void main(String[] args) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowClosed(WindowEvent e) {
System.exit(0);
}
};
JFrame f = new JFrame();
f.addWindowListener(l);
JPanel panel = new JPanel();
f.getContentPane().add(BorderLayout.SOUTH, panel);
f.getContentPane().add(BorderLayout.CENTER, new ECurve());
f.setSize(610, 350);
f.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -