http://expert.csdn.net/Expert/topic/1320/1320496.xml?temp=.4722254
希望你结贴!

解决方案 »

  1.   

    真该结贴了
    刚看了
    to beyond_xiruo(希偌) 
    看了你的程序,功底真深,佩服!!! 
      

  2.   

    现在程序中问题和您上次回答的不一样?
        图片都是新生成的,不知道其名称,所以图片动时,线不随着动, beyond_xiruo(希偌)能否帮我看一下代码,谢谢
       我会马上结帖的?
       等
      

  3.   

    其余帖子已结,图片如下(随便几个图片):
    http://www.itfuture.cc/images/1.gif    http://www.itfuture.cc/images/2.gif
    http://www.itfuture.cc/images/1.jpg    http://www.itfuture.cc/images/2.jpg
    http://www.itfuture.cc/images/3.jpg    http://www.itfuture.cc/images/4.jpg
    等您回音。
    如果可以很快也会结帖的!
    谢谢您!
      

  4.   

    我刚学用Appelt做GUI,我参考beyond_xiruo(希偌)也写了程序,还有一些问题我没办法解决,所以再请他帮忙。
        我很想自己做,但时间很有限。
        我慢慢会熟练的!
      

  5.   

    import javax.swing.*;
       import java.awt.*;
       import java.awt.image.*;
       import java.awt.event.*;
       import java.applet.*;
       import java.util.Vector;
       import java.awt.geom.*;public class applet5 extends Applet {
        JButton linebutton=new JButton();
        Image img1=null;
        Image img2=null;
        Image img3=null;
        Image img4=null;
        Image img01=null,img02=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;
        canvas current=null;
        canvas startobj,endobj;
        canvas1 can01=null;
        canvas1 can02=null;
        canvas1 can03=null;
        canvas1 can04=null;
        canvas  check=null;
        MediaTracker mt=new MediaTracker(this);
        Vector objects=new Vector();
        Vector lines  =new Vector();
        boolean blnDrawLine;
        boolean blnAddObj;
        int startX=0,startY=0,endX=0,endY=0,start=0,end=0,order=0,a=0,b=0;
        //初始化小程序
        public void init()
        {
          img01=this.getImage(this.getCodeBase(),"help.gif");//开始1
          img02=this.getImage(this.getCodeBase(),"openFile.gif");//结束1      img1=this.getImage(this.getCodeBase(),"closeFile.gif");//图片1
          img2=this.getImage(this.getCodeBase(),"help.gif");//图片2
          img3=this.getImage(this.getCodeBase(),"openFile.gif");//图片3
          img4=this.getImage(this.getCodeBase(),"help.gif");//图片4
          this.setLayout(null);
    //      JPanel bar=new JPanel();
    //      FlowLayout flow=new FlowLayout();
    //      bar.setLayout(null);
    //      JPanel main=new JPanel();
    //      BorderLayout border=new BorderLayout();
    //      main.setLayout(null);
    //      this.add(bar,BorderLayout.NORTH);
    //      this.add(main,BorderLayout.CENTER);
          this.setSize(1000,600);
          mt.addImage(img01,0);
          mt.addImage(img02,0);
          mt.addImage(img1,0);
          mt.addImage(img2,0);
          mt.addImage(img3,0);
          mt.addImage(img4,0);
          try{
       mt.waitForAll();
     }catch(Exception ex){
       System.err.println(ex.toString());
     }
    can1=new canvas(img01);//实例化一个帆布
    can2=new canvas(img02);//实例化另一个帆布
    can01=new canvas1(img1);
    can02=new canvas1(img2);
    can03=new canvas1(img3);
    can04=new canvas1(img4);
    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,img01.getWidth(this),img01.getHeight(this));
    can2.setBounds(200,200,img02.getWidth(this),img02.getHeight(this));
    objects.add(objects.size(),can1);
    objects.add(objects.size(),can2);
    this.add(can01);
    this.add(can02);
    this.add(can03);
    this.add(can04);
    linebutton.setText("连线----");
    linebutton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
          btnLine_actionPerformed(e);
      }
     });
    can01.setBounds(0,0,img1.getWidth(this),img1.getHeight(this));
    can02.setBounds(100,0,img2.getWidth(this),img2.getWidth(this));
    can03.setBounds(200,0,img3.getWidth(this),img3.getWidth(this));
    can04.setBounds(300,0,img4.getWidth(this),img4.getWidth(this));
    //this.add(linebutton);
    this.add(linebutton);
    //linebutton.setBounds(400,0,100,69);
    this.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
          addcanvas(e);
        }
       });
        }    public void paint(Graphics g) {
          g.drawString(Integer.toString(lines.size()),100,100);      for(int i=0;i<lines.size();i++){
    cavLine line=(cavLine)lines.elementAt(i);
    int startX=line.start.getX()+line.start.getWidth()/2;
            int startY=line.start.getY()+line.start.getHeight()/2;
    int endX=line.end.getX()+line.end.getWidth()/2;
    int endY=line.end.getY()+line.end.getHeight()/2;
    if (line!=null) {
        g.drawLine(startX,startY,endX,endY);
    }
          }
         // g.setXORMode(Color.white);
         // g.drawLine(wid1+width1/2,hei1+height1/2,wid2+width2/2,hei2+height2/2);
        }
        public void removecanvas(KeyEvent e)
        {
    this.remove(this.current);
    this.validate();
    this.repaint();
         }    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()==current)
        {
          w1=e.getX();
          h1=e.getY();       //wid1=((applet5)((canvas)e.getSource()).getParent()).can1.getX();
          //hei1=((applet5)((canvas)e.getSource()).getParent()).can1.getY();
          wid1=current.getX();
          hei1=current.getY();
          ((applet5)((canvas)e.getSource()).getParent()).current.setBounds(wid1+w1-w11,hei1+h1-h11,current.getWidth(),current.getHeight());
          ((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()==current) {
          w11=e.getX();
          h11=e.getY();
        }
       if(start==0&&blnDrawLine){
         a=findObj(e);
         startobj=(canvas)objects.get(a);
         startX=startobj.getX()+startobj.getWidth()/2;
         startY=startobj.getY()+startobj.getHeight()/2;
         start=1;
       }else if(start==1&&blnDrawLine){
         b=findObj(e);
         if(b!=a){
         endobj=(canvas)objects.get(b);
         endX=endobj.getX()+endobj.getWidth()/2;
         endY=endobj.getY()+endobj.getHeight()/2;
         lines.addElement(new cavLine(startobj,endobj)); //
         startobj=null;
         endobj  =null;
         blnDrawLine=false;
         start=0;
         ((applet5)(((canvas)e.getSource()).getParent())).repaint();
         repaint();
         }else{
         start=1;
         }
       }
      }
      public void mouseEntered(MouseEvent e){
         ((applet5)(((canvas)e.getSource()).getParent())).current=(canvas)e.getSource();
      }
    });
    this.addKeyListener(new KeyAdapter(){
       public void keyPressed(KeyEvent e){
           if(e.getKeyChar()=='o'){
      ((applet5)((canvas)e.getSource()).getParent()).removecanvas(e);
           }else if(e.getKeyCode()==e.VK_DELETE){
      ((applet5)((canvas)e.getSource()).getParent()).removecanvas(e);
           }
       }
    });      }      public void paint(Graphics g) {
    g.drawImage(im1,0,0,this);
          }
        }    class canvas1 extends Canvas
        {
           Image im2=null;
           public canvas1(Image im)
           {
     super();
     im2=im;
     this.addMouseListener(new MouseAdapter(){
        public void  mouseClicked(MouseEvent e){
       //((applet5)((canvas1)e.getSource()).getParent()).check=(canvas1)e.getSource();
         ((applet5)((canvas1)e.getSource()).getParent()).check=new canvas(((canvas1)e.getSource()).im2);
           blnDrawLine = false;
           blnAddObj = true;
       }
     });
           }
           public void paint(Graphics g) {
    g.drawImage(im2,0,0,this);
          }
        }    public void addcanvas(MouseEvent e) {
           if(check!=null&&blnAddObj)
           {
     if (e.getModifiers() == MouseEvent.BUTTON3_MASK)
     {
       this.add(check);
       check.setBounds(e.getX(), e.getY(), width1, height1);
       objects.add(objects.size(),check);
       this.validate();
       current=check;
       check=null;
      }
           }else{
       return;
           }
         }
         //点击工具栏上的连线图标才能画线
         void btnLine_actionPerformed(ActionEvent e)
         {
           blnDrawLine = true;
           blnAddObj = false;
         }     public int findObj(MouseEvent e)
         {
           order=objects.indexOf(e.getSource());
           return order;
        }  class cavLine {
          public canvas start;
          public canvas end;
          cavLine(canvas start,canvas end){
          this.start=start;
          this.end=end;
          }    }
    }程序又点乱,经小改动
    测试过正常
      

  6.   

    文档终于完成,你看看吧
    http://www.csdn.net/develop/read_article.asp?id=16454
      

  7.   

    谢谢 beyond_xiruo(希偌):
        在您的帮助下我会学好java的GUI编程,而且本版在在您的管理下会越来越好。一有时间就会向您学习。
        谢谢