1

I am just able to select one images from multiple images and dragging of images is not possible on entire screen,Below are the three images displayed Java Applet ,I wanted to select it and then drag it anywhere on the 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();
          }

          }

sorry for wrong indentation of code since iam newbie

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CY5
  • 1,551
  • 17
  • 23
  • 1
    The applet can't do anything outside of it's frame. – James Black May 26 '13 at 02:27
  • 1) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson May 26 '13 at 02:30
  • See [Auto-Indent in Notepad++](http://stackoverflow.com/q/412427/418556) & [Adding Code Formatting Tools in Notepad++](http://www.blogetcetera.com/2012/adding-code-formatting-tools-notepad/). I don't use Notepad++ but that is what I stumbled across on a quick search based on "Notepad++ format code". Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow! – Andrew Thompson May 26 '13 at 02:38
  • See also the advice of @JamesBlack re the applet security sand-box. By default, an applet would *not* be able to D-n-D outside its own applet context. Yet another reason to achieve this in a standard desktop app. first. – Andrew Thompson May 26 '13 at 02:48
  • i donot want applet to D-n-D out side its frame but instead i wanted to D-n-D inside Applet frame,Please run the code you will understand the problem – CY5 May 26 '13 at 02:57
  • You may want to look at https://eyeasme.com/Shayne/JAVA_DND_EXAMPLE/ as you are missing the DnD parts. – James Black May 28 '13 at 12:20
  • even though i have jdk1.7.0, i unable to run that applet on browser and my none of the applet are running on browser please help – CY5 Jun 14 '13 at 05:53

0 Answers0