📄 drawtest.java
字号:
package drawtest;
/**
* Java语言实验参考程序
* Company 北京师范大学计算机系
* @author 孙一林
* @version 1.0
*/
import java.awt.*;
import java.applet.*;
import java.util.Vector;
public class DrawTest extends Applet { // 构造Applet小程序
public DrawTest() { // 初始化窗口
setLayout(new BorderLayout());
DrawPanel dp = new DrawPanel();
add("Center", dp);
add("South",new DrawControls(dp));
}
public boolean handleEvent(Event e) { // 事件处理
switch (e.id) {
case Event.WINDOW_DESTROY:
System.exit(0);
return true;
default:
return false;
}
}
public static void main(String args[]) { // Application程序入口
Frame f = new Frame("画图程序"); // 创建Application程序框架
DrawTest drawTest = new DrawTest();
drawTest.init();
drawTest.start();
f.add("Center", drawTest);
f.resize(300, 300);
f.show();
}
}
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);
}
public void setDrawMode(int mode) { // 设置画点、画线模式
switch (mode) {
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}
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;
}
}
public void paint(Graphics g) { // 画线(LINES)
int np = lines.size();
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); // 画线
}
}
}
}
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;
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);
}
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -