利用Applet绘图
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Vector;public class PaintTest extends Applet{
DrawPanel panel;
drawControls controls;
public void init(){
setLayout(new BorderLayout());
panel=new DrawPanel();
controls=new drawControls(panel);
add("Center",panel);
add("North",controls);
}
    public static void main(String args[]){
     Frame f=new Frame("PaintTest");
     PaintTest drawTest=new PaintTest();
     drawTest.init();
     drawTest.start();
     f.addWindowListener(new WindowAdapter(){
     public void windowClosing(WindowEvent e){
     System.exit(1);
     }
     });
     f.setSize(600,600);
     f.show();
    }
    public String getAppletInfo(){
     return"A simple drawing peogram.";
    }
    
}
class DrawPanel extends Panel implements MouseListener,MouseMotionListener{
public static final int LINES=0;
public static final int POINTS=1;
public static final int RECTANGLES=2;
public static final int OVALS=3;
int mode=LINES;
int x1,y1;
int x2,y2;
Vector lines=new Vector();
Vector rectangles=new Vector();
Vector ovals=new Vector();
Vector Lcolors=new Vector();
Vector Rcolors=new Vector();
Vector Ocolors=new Vector(); 
public DrawPanel(){
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void setDrawMode(int mode){
switch(mode){
case LINES:
case POINTS:
case RECTANGLES:
case OVALS:
this.mode=mode;
break;
default:
throw new IllegalArgumentException();

}
}
public void mouseDragged(MouseEvent e){
e.consume();
switch(mode){
case LINES:
case RECTANGLES:
case OVALS:
x2=e.getX();
y2=e.getY();
break;
case POINTS:
default:
Lcolors.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:
case RECTANGLES:
case OVALS:
x1=e.getX();
y1=e.getY();
x2=-1;
break;
case POINTS:
default:
Lcolors.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:
Lcolors.addElement(getForeground());
lines.addElement(new Rectangle(x1,y1,e.getX(),e.getY()));
x2=-1;
break;
case RECTANGLES:
Rcolors.addElement(getForeground());
rectangles.addElement(new Rectangle(x1,y1,e.getX(),e.getY()));
x2=-1;
break;
case OVALS:
Ocolors.addElement(getForeground());
ovals.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();
for(int i=0;i<np;i++){
Rectangle p=(Rectangle)Lcolors.elementAt(i);
g.setColor((Color)Lcolors.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);
}
}
np=rectangles.size();
for(int i=0;i<np;i++){
Rectangle p=(Rectangle)rectangles.elementAt(i);
g.setColor((Color)Rcolors.elementAt(i));
if(p.width !=-1){
g.drawRect(p.x,p.y,p.width,p.height);
}
}
np=ovals.size();
for(int i=0;i<np;i++){
Rectangle p=(Rectangle)ovals.elementAt(i);
g.setColor((Color)Ocolors.elementAt(i));
if(p.width !=-1){
g.drawOval(p.x,p.y,p.width,p.height);
}
}
g.setColor(getForeground());
switch(mode){
case LINES:
if(x2 !=-1){
g.drawLine(x1,y1,x2,y2);
}
break;
case RECTANGLES:
if(x2 !=-1){
g.drawRect(x1,y1,x2,y2);
}
break;
case OVALS:
if(x2 !=-1){
g.drawOval(x1,y1,x1,y2);
}
break;
}
}
}
class drawControls extends Panel implements ItemListener,MouseListener{
DrawPanel target;
public drawControls(DrawPanel target){
this.target=target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
Panel pcolor=new Panel();
pcolor.setLayout(new GridLayout(1,8));
Label l;
pcolor.add(l=new Label());
l.setBackground(Color.red);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.green);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.blue);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.yellow);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.pink);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.orange);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.cyan);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.magenta);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.gray);
l.addMouseListener(this);
pcolor.add(l=new Label());
l.setBackground(Color.black);
l.addMouseListener(this);
add(pcolor);
Choice shapes=new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.addItem("Rectangles");
shapes.addItem("Ovals");
shapes.setBackground(Color.lightGray);
add(shapes);
}
public void itemStateChanged(ItemEvent e){
if(e.getSource() instanceof Choice){
String choice=(String)e.getItem();
if(choice.equals("Lines")){
target.setDrawMode(DrawPanel.LINES);
}else if(choice.equals("Points")){
target.setDrawMode(DrawPanel.POINTS);
}else if(choice.equals("Rectangles")){
target.setDrawMode(DrawPanel.RECTANGLES);
}else if(choice.equals("Ovals")){
target.setDrawMode(DrawPanel.OVALS);
}
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(e.getSource() instanceof Label){
target.setForeground(((Component)e.getSource()).getBackground());
}
}
public void mouseReleased(MouseEvent e){}
}
最后运行结果如下
--------------------Configuration: <Default>--------------------
注意:C:\Documents and Settings\Administrator.WWW-BE7638F586A\桌面\PaintTeset.java 使用或覆盖了已过时的 API。
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
注意:C:\Documents and Settings\Administrator.WWW-BE7638F586A\桌面\PaintTeset.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。Process completed