Java 图形程序设计
画图工具 htgj.java
// 画图工具 htgj.java 2003.8.28.
import java.awt.*;
import java.applet.*;
import java.util.Vector;//导入 java语言不同类型工具包矢量类
// 1.画图工具 htgj 类继承小程序框架 Applet
public class htgj extends Applet {
public htgj() {
setLayout(new BorderLayout()); // 设置边界布局
DrawPanel dp = new DrawPanel();// 创建画图面板实例 dp
add("Center",dp); // 对中加入画图面板 dp
add("South",new DrawControls(dp));//画图控件
}
// 2.关闭窗口
public boolean handleEvent(Event e) {
switch (e.id) {
case Event.WINDOW_DESTROY:
System.exit(0);
return true;
default:
return false;
}
}
// 3.应用程序入口 main
public static void main(String args[]) {
Frame f = new Frame("Java 语言图形程序设计 画图工具 htgj 2003.8.28.");
htgj htgj_sl = new htgj();
htgj_sl.init();
htgj_sl.start();
f.add("Center", htgj_sl);
f.resize(300, 300);
f.show();
}}
// 4.构造画板
class DrawPanel extends Panel {
public static final int LINES = 0;
public static final int POINTS = 1;
int mode = LINES;
Vector lines = new Vector();
Vector colors = new Vector();
int x1,y1;
int x2,y2;
int xl, yl;
public DrawPanel() {
setBackground(Color.white);// 画板背景白色
}
// 3.1 设置点,线模式
public void setDrawMode(int mode) {
switch (mode) {
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}
// 4.鼠标事件
public boolean handleEvent(Event e) {
switch (e.id) {
case Event.MOUSE_DOWN: // 按下
switch (mode) {
case LINES:
x1 = e.x;
y1 = e.y;
x2 = -1;
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(e.x, e.y, -1, -1));
x1 = e.x;
y1 = e.y;
repaint(); // 刷新屏幕
break;
}
return true;
case Event.MOUSE_UP: // 松开
switch (mode) {
case LINES:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.x, e.y));
x2 = xl = -1;
break;
case POINTS:
default:
break;
}
repaint(); // 刷新屏幕
return true;
case Event.MOUSE_DRAG: // 拖动
switch (mode) {
case LINES: // 画线
xl = x2;
yl = y2;
x2 = e.x;
y2 = e.y;
break;
case POINTS: // 画点
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.x, e.y));
x1 = e.x;
y1 = e.y;
break;
}
repaint(); // 刷新屏幕
return true;
case Event.WINDOW_DESTROY:
System.exit(0);
return true;
default:
return false;
}
}
// 5.画当前线,点
public void paint(Graphics g) {
int np = lines.size(); // n 个点
g.setColor(getForeground());
g.setPaintMode();
for (int i=0; i<np; i++) {
Rectangle p = (Rectangle)lines.elementAt(i);
g.setColor((Color)colors.elementAt(i));
if (p.width != -1) {
g.drawLine(p.x, p.y, p.width, p.height);// 画线
}
else {
g.drawLine(p.x, p.y, p.x, p.y);// 画点
}
}
if (mode == LINES) {
g.setXORMode(getBackground());//异或模式
if (xl != -1) {
g.drawLine(x1, y1, xl, yl);// 刷除最后一条线
}
g.setColor(getForeground());
g.setPaintMode();
if (x2 != -1) {
g.drawLine(x1, y1, x2, y2);
}
}
}
}
// 6.画图控件
class DrawControls extends Panel {
DrawPanel target;
public DrawControls(DrawPanel target) {
this.target = target; // 目标
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();// 单选框组
Checkbox b; // 单选框
// 6.1 选择颜色
add(b = new Checkbox(null, group, false));
b.setBackground(Color.red);// 红
add(b = new Checkbox(null, group, false));
b.setBackground(Color.green); //绿
add(b = new Checkbox(null, group, false));
b.setBackground(Color.blue); // 蓝
add(b = new Checkbox(null, group, false));
b.setBackground(Color.pink); // 粉红
add(b = new Checkbox(null, group, false));
b.setBackground(Color.orange); // 橙
add(b = new Checkbox(null, group, true));
b.setBackground(Color.black); // 黑
target.setForeground(b.getForeground());
Choice shapes = new Choice(); // 下拉菜单
shapes.addItem("LINES"); // 画线菜单项
shapes.addItem("POINTS"); // 画点菜单项
shapes.setBackground(Color.lightGray);
add(shapes);
}
public void paint(Graphics g) {
Rectangle r = bounds();
g.setColor(Color.lightGray);
g.draw3DRect(0, 0, r.width, r.height, false);
}
// 6.2 选择点线
public boolean action(Event e, Object arg) {
if (e.target instanceof Checkbox) { // 选择框
target.setForeground(((Component)e.target).getBackground());
}
else
if (e.target instanceof Choice) { // 下拉菜单
String choice = (String)arg;
if (choice.equals("LINES")) {
target.setDrawMode(DrawPanel.LINES);
}
else
if (choice.equals("POINTS")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
return true;
}
}