Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Selecting and dragging a image from multiple image in Java Applet

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Selecting and dragging a image from multiple image in Java Applet

    I am just able to select one images from multiple images and dragging of images is not possible on entire screen of Java Applet Frame,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();
                  }
     
                  }
    arrow1.jpg
    arrow2.jpg
    Home.jpg


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Selecting and dragging a image from multiple image in Java Applet

    I do not fully understand what it should do and what it does do, please try to explain it again for me?
    Also what have you tried in fixing this already? Any idea what you think is wrong or what to look for?

Similar Threads

  1. How to print image on applet
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 21st, 2012, 05:57 AM
  2. error in print image onto applet
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2012, 08:17 PM
  3. Applet Image Reading
    By 77times in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 29th, 2012, 11:25 PM
  4. i need Image printing in Multiple pages.
    By srinivas Reddy in forum AWT / Java Swing
    Replies: 1
    Last Post: February 29th, 2012, 10:47 AM
  5. Replies: 2
    Last Post: June 29th, 2009, 03:06 PM

Tags for this Thread