import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventObject;
import java.util.Vector;
public class PanitTest extends Applet{
/**
 * 
 */
private static final long serialVersionUID = 9205202288949886580L;
DrawPanel panel;
DrawControls controls;
public void init(){
setLayout(new BorderLayout());
panel=new DrawPanel();
controls=new DrawControls();
add("Center",panel);
add("North",controls);
}
private void add(String string, DrawControls controls2) {
// TODO 自动生成方法存根

}
public static void main(String[] args){
Frame f=new Frame("PaintTest");
PanitTest drawTest=new PanitTest();
drawTest.init();
drawTest.start();
f.add("Center",drawTest);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
f.setSize(300,300);
f.show();
}
public String getAppletInfo(){
return"A simple drawing program";
}
}
class DrawPanel extends Panel implements MouseListener,MouseMotionListener{
/**
 * 
 */
private static final long serialVersionUID = -8848658383208771709L;
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)lines.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,x2,y2);
      }
      break;
      }
     } 
   }
   class DrawContpols extends Panel
   implements ItemListener,MouseListener{
    /**
 * 
 */
private static final long serialVersionUID = -1907583388214022642L;
DrawPanel target;
    public void 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){}{
EventObject e = null;
if(e.getSource()instanceof Label){
target.setForeground(((Component)e.getSource()).getBackground());
}
}
public void mouseReleased(MouseEvent e){}
}

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.util.EventObject;
    import java.util.Vector;
    public class PaintTest extends Applet{//public class PanitTest extends Applet{
    /**
    *
    */
    private static final long serialVersionUID = 9205202288949886580L;
    DrawPanel panel;
    DrawControls controls;
    public void init(){
    setLayout(new BorderLayout());
    panel=new DrawPanel();
    controls=new DrawControls();
    add("Center",panel);
    add("North",controls);
    }
    private void add(String string, DrawControls controls2) {
    // TODO 自动生成方法存根}
    public static void main(String[] args){
    Frame f=new Frame("PaintTest");
    PaintTest drawTest=new PaintTest();//PanitTest drawTest=new PanitTest();
    drawTest.init();
    drawTest.start();
    f.add("Center",drawTest);
    f.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(1);
    }
    });
    f.setSize(300,300);
    f.show();
    }
    public String getAppletInfo(){
    return"A simple drawing program";
    }
    }
    class DrawPanel extends Panel implements MouseListener,MouseMotionListener{
    /**
    *
    */
    private static final long serialVersionUID = -8848658383208771709L;
    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)lines.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,x2,y2);
      }
      break;
      }
      }
      }
      class DrawControls extends Panel//class DrawContpols extends Panel
      implements ItemListener,MouseListener{
      /**
    *
    */
    private static final long serialVersionUID = -1907583388214022642L;
    DrawPanel target;
      public void 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){}{
    EventObject e = null;
    if(e!=null&&e.getSource()instanceof Label){//if(e.getSource()instanceof Label){***原先存在null引用问题
        System.out.println(e.getSource());
    target.setForeground(((Component)e.getSource()).getBackground());
    }
    }
    public void mouseReleased(MouseEvent e){}
    }
      

  2.   

    代码太长了,又没注释,我看得也不是非常仔细。我改的地方,只是你输入时输错了,还有就是:
    if(e!=null&&e.getSource()instanceof Label){//if(e.getSource()instanceof Label){***原先存在null引用问题
    你这代码是哪本书上的实例代码吗?