1

我只能从多个图像中选择一个图像,并且无法在整个屏幕上拖动图像,以下是显示的三个图像 Java Applet,我想选择它然后将其拖动到 Applet Frame 上的任何位置

     import java.applet.*;
     import java.awt.*;
     import java.awt.event.*;

      /*
     <applet code="SelectingImage" width=500 height=300>
     <param name=img1 value=arrow1.jpg>
     <param name=img2 value=Home.jpg> 
     <param name=img3 value=arrow2.jpg>
     </applet>
      */
   public class SelectingImage extends Applet implements                   
    MouseListener,MouseMotionListener{                     
Image img[]=new Image[3];
int y=10;
int a;
int dx,dy;
int xpos,ypos;
int x[]=new int[3];
boolean imgdrag,imgclick,enabled=true;
public void init(){
try{
    MediaTracker mt=new MediaTracker(this);
    img[0]=getImage(getDocumentBase(),getParameter("img1"));
    img[1]=getImage(getDocumentBase(),getParameter("img2"));
    img[2]=getImage(getDocumentBase(),getParameter("img3"));
    for(int i=0;i<3;i++){
        x[i]=(i+1)*150;
        mt.addImage(img[i],i);
    }
    mt.waitForAll();
    addMouseListener(this);
    }
    catch (InterruptedException e) { };
}


public void paint(Graphics g){
    for(int i=0;i<3;i++){
            g.drawImage(img[i],x[i],y,null);
            }
            for(int i=0;i<3;i++){
            if(xpos>=x[i] && xpos<=img[i].getWidth(null)+x[i] &&     
                        ypos>=10 && ypos<=img[i].getHeight(null))
                             {                                                                                                
                   g.drawRect(x[i],10,img[i].getWidth(null),img[i].getHeight(null));                                                                 

                a=i;
                imgclick=true;
                break;
            }
            else{
                imgclick=false;
            }
        }
        if(imgclick){
            g.drawImage(img[a],xpos,ypos,null);
        }
}
               public void mouseClicked (MouseEvent me) 
               {
            xpos=me.getX(); 
                ypos=me.getY(); 
            showStatus("Mouse at"+me.getX());
            repaint();
                }
               public void mouseEntered (MouseEvent me) {
                } 
               public void mousePressed (MouseEvent me) {} 
               public void mouseReleased (MouseEvent me) {}  
               public void mouseExited (MouseEvent me) {} 
               public void mouseDragged(MouseEvent me){
        xpos=me.getX();
        ypos=me.getY();

    showStatus("Mouse at"+me.getX());
    repaint();
              }
         public void mouseMoved(MouseEvent me){
           xpos=me.getX();
    ypos=me.getY();
    repaint();
          }

          }

对不起,因为我是新手,所以代码缩进错误

4

0 回答 0