问题描述
importjava.applet.Applet;importjava.awt.*;importjava.awt.event.MouseEvent;importjava.awt.event.MouseMotionAdapter;publicclassappextendsApplet{Imageimage;ScrollPanescrollp=newScrollPane();//图片的大小//位置intimaWid=0,imaHei=0,mouseWid=0,mouseHei=0,locationWid=0,localtionHei=0;canvascan=null;MediaTrackermt=newMediaTracker(this);publicvoidinit(){setLayout(newGridLayout(1,2));//setLayout(null);scrollp=newScrollPane(ScrollPane.SCROLLBARS_ALWAYS);image=this.getImage(this.getCodeBase(),"1.gif");//图片1mt.addImage(image,0);try{mt.waitForAll();}catch(Exceptionex){System.err.println(ex.toString());}can=newcanvas(image);imaWid=image.getWidth(this);imaHei=image.getHeight(this);locationWid=10;localtionHei=60;scrollp.add(can);add(scrollp);this.setVisible(true);}classcanvasextendsCanvas{Imageimag;publiccanvas(Imageimage){imag=image;addMouseMotionListener(newMouseMotionAdapter(){publicvoidmouseDragged(MouseEvente){locationWid=e.getX();localtionHei=e.getY();((canvas)e.getSource()).repaint();((app)((canvas)e.getSource()).getParent().getParent()).validate();((app)((canvas)e.getSource()).getParent().getParent()).repaint();/*((app)((canvas)e.getSource()).getParent().getParent()).validate();((canvas)e.getSource()).repaint();((app)((canvas)e.getSource()).getParent().getParent()).repaint();*/}});}publicvoidpaint(Graphicsg){g.drawImage(image,locationWid,localtionHei,this);}}}