Graphic g = getGraphics();
g2 = (Graphic2D) g;
g2.drawLine(x1, y1, x2, y2); //(x1, y1)起始位置, (x2, y2)终点位置

解决方案 »

  1.   

    import java.awt.*;
    import java.applet.*;
    import java.math.*;
    import java.util.Vector;
    import java.awt.Image;public class DrawTest extends Applet {
    Image picture1;

        public void init() {
    picture1 = getImage(getDocumentBase(),"Map.gif");
    setLayout(new BorderLayout());
    DrawPanel dp = new DrawPanel(picture1);
    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[]) {
    Frame f = new Frame("DrawTest");
    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;
    public static final int RECTANGLES  =3;
    public static final int CIRCLES = 4; Image Picture;
        int    mode = LINES;
    int    shape = LINES;
        int x1,y1;
        int x2,y2;
    int x3,y3; int distance; public DrawPanel(Image Picture) {
    setBackground(Color.white);
    this.Picture = Picture;
        }    public void setDrawMode(int mode,int shape) {
    this.shape = shape;
    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 (shape) {
          case LINES:
    x1 = e.x;
    y1 = e.y;
    x2 = -1;
    break;
      case RECTANGLES:
    x1 = e.x;
    y1 = e.y;
    x2 = -1;
    break;
      case CIRCLES:
    x1 = e.x;
    y1 = e.y;
    x2 = -1;
    break;
      case POINTS:
          default:
    x1 = e.x;
    y1 = e.y;
    repaint();
    break;
        }
        return true;
      case Event.MOUSE_UP:
        switch (shape) {
          case LINES:
    x2 = x3 = -1;
    break;
      case RECTANGLES:
    x2 = x3 = -1;
    break;
      case CIRCLES:
    x2 = x3 = -1;
    break;
          case POINTS:
          default:
    break;
        }
        repaint();
        return true;
      case Event.MOUSE_DRAG:
        switch (shape) {
          case LINES:
    x3 = x2;
    y3 = y2;
    x2 = e.x;
    y2 = e.y;
    break;
      case RECTANGLES:
    x3 = x2;
    y3 = y2;
    x2 = e.x;
    y2 = e.y;
    break;
      case CIRCLES:
    x3 = x2;
    y3 = y2;
    x2 = e.x;
    y2 = e.y;
    break;
      case POINTS:
          default:
    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) {
    /* draw the current lines */
    g.setColor(getForeground());
    g.setPaintMode(); g.drawImage(Picture,0,0,this); g.setColor(getForeground());
    //当前的线条
        g.setXORMode(getBackground());
    if (x3 != -1) {
    /* erase the last line. */
    if (shape == RECTANGLES)
    {
    g.drawRect(x1>x3 ? x3 : x1,y1> y3 ? y3: y1 ,Math.abs(x1 - x3),Math.abs(y1 - y3));
    }
    else if(shape == CIRCLES){
    distance = (int) Math.sqrt((x3-x1)*(x3-x1) + (y3-y1) * (y3-y1));  
    g.drawOval(x1-distance,y1-distance,distance*2,distance*2);
    g.drawLine(x1,y1,x3,y3);
    }
    else
    g.drawLine(x1, y1, x3, y3);
    }
    g.setColor(getForeground());
    g.setPaintMode();
    if (x2 != -1) {
    if (shape == RECTANGLES)
    {
    g.drawRect(x1>x2 ? x2 : x1,y1> y2 ? y2: y1 ,Math.abs(x1 - x2),Math.abs(y1 - y2));
    }
    else if(shape == CIRCLES){
    distance = (int) Math.sqrt((x3-x1)*(x3-x1) + (y3-y1) * (y3-y1));  
    g.drawOval(x1-distance,y1-distance,distance*2,distance*2);
    }
    else
    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.addItem("Rectangle");
    shapes.addItem("Circle");
    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,DrawPanel.LINES);
        } else if (choice.equals("Points")) {
    target.setDrawMode(DrawPanel.POINTS,DrawPanel.POINTS);
        }else if (choice.equals("Rectangle")) {
    target.setDrawMode(DrawPanel.LINES,DrawPanel.RECTANGLES);
        }else if (choice.equals("Circle")) {
    target.setDrawMode(DrawPanel.LINES,DrawPanel.CIRCLES);
        }
    }
    return true;
        }
    }
    //<applet code = DrawTest.class width = 400 height = 400 ></applet>
      

  2.   

    以上是自己仿照别人写的APPLET,多问个问题,怎样让图不重画,而让线和矩形重画呢?