高手帮忙!!!解释一段JAVA的程序,要尽量详细!!!100分!!!

import java.awt.event.*;
import java.awt.*;
import java.applet.*;

import java.util.Vector;

public class DrawTest extends Applet{
DrawPanel panel;
DrawControls controls;

public void init() {
setLayout(new BorderLayout());
panel = new DrawPanel();
controls = new DrawControls(panel);
add("Center", panel);
add("South",controls);
}

public void destroy() {
remove(panel);
remove(controls);
}

public static void main(String args[]) {
Frame f = new Frame("DrawTest");
DrawTest drawTest = new DrawTest();
drawTest.init();
drawTest.start();

f.add("Center", drawTest);
f.setSize(300, 300);
f.show();
}
public String getAppletInfo() {
return "A simple drawing program.";
}
}

class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
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;

import java.awt.event.*; //导入包,分别是AWT组件,APPLET组件和事件处理组件,还有向量组件
import java.awt.*;
import java.applet.*;

import java.util.Vector;

public class DrawTest extends Applet{ //声明类,继承了APPLET
DrawPanel panel; //声明DRAWPANEL类的实例
DrawControls controls; //声明DRAWCONTROLS类的实例

public void init() { //本类初始化方法,将一些参数初始化
setLayout(new BorderLayout());
panel = new DrawPanel();
controls = new DrawControls(panel); //将控制类和面板类关联
add("Center", panel); //将画图面板放在显示终端的中间
add("South",controls); //将控制面板放在显示终端的下面
}

public void destroy() { //增加删除面板方法,这里删除了画图面板和控制面板
remove(panel);
remove(controls);
}

public static void main(String args[]) { //程序入口
Frame f = new Frame("DrawTest");
DrawTest drawTest = new DrawTest(); //实例化测试类
drawTest.init(); //初始化类实例
drawTest.start(); //运行类实例

f.add("Center", drawTest); //将整个画图界面放在显示终端的中间
f.setSize(300, 300); //设置界面大小
f.show(); //显示界面
}
public String getAppletInfo() { //定义获取当前APPLET应用的解释
return "A simple drawing program.";
}
}

class DrawPanel extends Panel implements MouseListener, MouseMotionListener { //定义画图面板类
public static final int LINES = 0; //定义常量0表示直线
public static final int POINTS = 1; //1表示点
int mode = LINES; //初始化模式为直线
Vector lines = new Vector(); //直线向量
Vector colors = new Vector(); //颜色向量
int x1,y1; //这四个都是点坐标,因为画图时要用
int x2,y2;
public DrawPanel() { //无参构造画图面板方法
setBackground(Color.white); //设置背景为白色
addMouseMotionListener(this); //这两个都是增加鼠标点击侦听方法
addMouseListener(this);
}

public void setDrawMode(int mode) { //初始化模式的方法
switch (mode) {
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}

public void mouseDragged(MouseEvent e) { //鼠标拖动侦听处理
e.consume();
switch (mode) { //如果是直线,则记录鼠标结束的坐标,并在向量中记录
case LINES:
x2 = e.getX();
y2 = e.getY();
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x1 = e.getX();
y1 = e.getY();
break;
}
repaint(); //刷新面板(因为鼠标拖动过一次)
}

public void mouseMoved(MouseEvent e) {
}

public void mousePressed(MouseEvent e) { //鼠标点击侦听处理
e.consume();
switch (mode) { //类似上一个方法,我就不说了
case LINES:
x1 = e.getX();
y1 = e.getY();
x2 = -1;
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
x1 = e.getX();
y1 = e.getY();
repaint();
break;
}
}

public void mouseReleased(MouseEvent e) { //鼠标释放侦听处理
e.consume();
switch (mode) {
case LINES:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 = -1;
break;
case POINTS:
default:
break;
}
repaint();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void paint(Graphics g) { //根据记录开始画图的方法
int np = lines.size();

/* draw the current lines */
g.setColor(getForeground());
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.setColor(getForeground());
if (x2 != -1) {
g.drawLine(x1, y1, x2, y2);
}
}
}
}
大致就这个意思。总的来说就是一个APPLET程序,有控制面板和绘图面板。不懂可以继续补充提问。呵呵。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-06-13
public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}

public void setDrawMode(int mode) {
switch (mode) {
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}

public void mouseDragged(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
x2 = e.getX();
y2 = e.getY();
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x1 = e.getX();
y1 = e.getY();
break;
}
repaint();
}

public void mouseMoved(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
x1 = e.getX();
y1 = e.getY();
x2 = -1;
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
x1 = e.getX();
y1 = e.getY();
repaint();
break;
}
}

public void mouseReleased(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 = -1;
break;
case POINTS:
default:
break;
}
repaint();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void paint(Graphics g) {
int np = lines.size();

/* draw the current lines */
g.setColor(getForeground());
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.setColor(getForeground());
if (x2 != -1) {
g.drawLine(x1, y1, x2, y2);
}
}
}
}

这是装JAVA的时候自带的一个程序,希望高手能够帮我解释清楚都是干什么用的,越详细越好!!!在线等!!!
第2个回答  2008-06-14
我晕倒,楼上的还真仔细啊,佩服!其实如果楼主会英文的话,大致就可以知道程序的意思,所以与其这样靠别人,不如自己努力一下英文,这样对编程大有好处!
第3个回答  2008-06-13
就是绘制屏幕用的吧