[java]代码库
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ArcTest extends Applet implements WindowListener {
ArcControls controls;
public void init() { // Applet的入口方法
setLayout(new BorderLayout());
ArcCanvas c = new ArcCanvas();
add("Center", c);
add("South", controls = new ArcControls(c));
}
public void start() {
controls.setEnabled(true); // 激活controls
}
public void stop() {
controls.setEnabled(false);
}
public void windowActivated(WindowEvent e) {
}
// 重写WindowListener的方法
public void windowClosed(WindowEvent e) {
}
// 重写WindowListener的方法
public void windowClosing(WindowEvent e) {
// 重写WindowListener的方法
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
// 重写WindowListener的方法
public void windowDeiconified(WindowEvent e) {
}
// 重写WindowListener的方法
public void windowIconified(WindowEvent e) {
}
// 重写WindowListener的方法
public void windowOpend(WindowEvent e) {
}
// 重写WindowListener的方法
public static void main(String args[]) {
Frame f = new Frame("ArcTest"); // 构造Frame
ArcTest arcTest = new ArcTest(); // 构造arcTest
arcTest.init();
arcTest.start();
f.add("Center", arcTest);
f.setSize(300, 300);
f.show();
f.addWindowListener(arcTest);
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}
class ArcCanvas extends Canvas { // 类ArcCanvas
int startAngle = 0;
int endAngle = 45;
boolean filled = false;
Font font;
public void paint(Graphics g) {
// paint方法,该方法的作用是在Canvas上画图
Rectangle r = getBounds();
int hlines = r.height / 10;
int vlines = r.width / 10;
g.setColor(Color.pink);
for (int i = 1; i <= hlines; i++) {
g.drawLine(0, i * 10, r.width, i * 10);
}
for (int i = 1; i <= vlines; i++) {
g.drawLine(i * 10, 0, i * 10, r.height);
}
g.setColor(Color.red);
if (filled) {
g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
} else {
g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, endAngle);
}
g.setColor(Color.black);
g.setFont(font);
g.drawLine(0, r.height / 2, r.width, r.height / 2);
g.drawLine(r.width / 2, 0, r.width / 2, r.height);
g.drawLine(0, 0, r.width, r.height);
g.drawLine(r.width, 0, 0, r.height);
int sx = 10;
int sy = r.height - 28;
g.drawString("S=" + startAngle, sx, sy);
g.drawString("E=" + endAngle, sx, sy + 14);
}
public void redraw(boolean filled, int start, int end) { // 重画方法
this.filled = filled;
this.startAngle = start;
this.endAngle = end;
repaint();
// 通过调用repaint()方法,从而最终调用paint方法完成重画
}
}
class ArcControls extends Panel implements ActionListener { // ArcControls类
TextField s;
TextField e;
ArcCanvas canvas;
public ArcControls(ArcCanvas canvas) {
Button b = null;
this.canvas = canvas;
add(s = new TextField("0", 4));
add(e = new TextField("45", 4));
b = new Button("Fill");
b.addActionListener(this);
add(b);
b = new Button("Draw");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent ev) {
// 实现接口ActionListener的方法
String label = ev.getActionCommand();
canvas.redraw(label.equals("Fill"),
Integer.parseInt(s.getText().trim()),
Integer.parseInt(e.getText().trim()));
}
}