import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;public class applet5 extends Applet {
Image img1=null;
Image img2=null;
int width1=0,height1=0,w1=0,h1=0,wid1=0,hei1=0,w11=0,h11=0;
int width2=0,height2=0,w2=0,h2=0,wid2=0,hei2=0,w22=0,h22=0;
canvas can1=null;
canvas can2=null;
MediaTracker mt=new MediaTracker(this);
public void init() {
img1=this.getImage(this.getCodeBase(),"1.gif");
img2=this.getImage(this.getCodeBase(),"2.gif");
this.setLayout(null);
mt.addImage(img1,0);
mt.addImage(img2,0);
try {
  mt.waitForAll();
} catch(Exception ex) {System.err.println(ex.toString());}
can1=new canvas(img1);
can2=new canvas(img2);
width1=img1.getWidth(this);
height1=img1.getHeight(this);
width2=img2.getWidth(this);
height2=img2.getHeight(this);
wid1=hei1=100;
wid2=hei2=200;
this.add(can1);
this.add(can2);
can1.setBounds(100,100,width1,height1);
can2.setBounds(200,200,width2,height2);
}public void paint(Graphics g) {
g.drawLine(wid1+width1/2,hei1+height1/2,wid2+width2/2,hei2+height2/2);
}class canvas extends Canvas {
Image im1=null;
public canvas(Image im) {
super();
im1=im;
this.addMouseMotionListener(new MouseMotionAdapter() {
  public void mouseDragged(MouseEvent e) {
    if(e.getSource()==can1) {
    w1=e.getX();
    h1=e.getY();
    wid1=((applet5)((canvas)e.getSource()).getParent()).can1.getX();
    hei1=((applet5)((canvas)e.getSource()).getParent()).can1.getY();
    ((applet5)((canvas)e.getSource()).getParent()).can1.setBounds(wid1+w1-w11,hei1+h1-h11,width1,height1);
    ((applet5)((canvas)e.getSource()).getParent()).validate();
    ((canvas)e.getSource()).repaint();
    ((applet5)((canvas)e.getSource()).getParent()).repaint();
    } else if(e.getSource()==can2) {
      w2=e.getX();
      h2=e.getY();
      wid2=((applet5)((canvas)e.getSource()).getParent()).can2.getX();
      hei2=((applet5)((canvas)e.getSource()).getParent()).can2.getY();
      ((applet5)((canvas)e.getSource()).getParent()).can2.setBounds(wid2+w2-w22,hei2+h2-h22,width2,height2);
      ((applet5)((canvas)e.getSource()).getParent()).validate();
      ((canvas)e.getSource()).repaint();
    ((applet5)((canvas)e.getSource()).getParent()).repaint();
    }
  }
});
this.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent e) {
    if(e.getSource()==can1) {
      w11=e.getX();
      h11=e.getY();
    } else if(e.getSource()==can2) {
      w22=e.getX();
      h22=e.getY();
    }
  }
});
}public void paint(Graphics g) {
g.drawImage(im1,0,0,this);
}
}
}

解决方案 »

  1.   

    特别感谢 :beyond_xiruo(希偌),您的程序很好,我很高兴,我觉得我应向您学习很多知识 ,不知您乐意作我的老师吗,我来自西安,毕业于西安交通大学,以前是做linux下C开发的,但很喜欢java。我昨天给您发留言,但系统老提示出错(在csdn),我们可以作为朋友吗,我有许多知识要向您请教。我的信箱是:[email protected];我的QQ号:105696 。很高兴能收到您的来信。
       我还有一个问题是:如何在两个任意图片之间,画出带箭头的直线连这两个图片,事先这两个图片单独存在,用鼠标从一个图片到另一个图片连一下,就会画出一条带箭头直线,箭头代表画时的方向(从开始的图到结束的图)。不知您能否告诉怎样入手。等您回音。
      

  2.   

    in fact this examples in the jdk1.3/example!
      

  3.   

    的确,在JDK 的EXAMPLE 有很多很好的例子,我们有时间应该多点学习一下。