Results 1 to 5 of 5
Thread: how to move my drag images.
- 05-01-2012, 11:36 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 90
- Rep Power
- 0
how to move my drag images.
Hi,
I have successfully draw images on applet , also its dragging cards images now I have dragged one images , now after that I want to shift this images onto another place after mousereleased event, please help me,,,,,,
My code for read is given bellow.
Java Code:package com.progresso; import java.applet.AudioClip; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JApplet; public class ShowCard extends JApplet implements MouseListener,Runnable, MouseMotionListener { private static final long serialVersionUID = 1L; Image image,image1,img,img1,Casino_table_img; MediaTracker tr; int x=105; int y=210; private int _initX = 105; private int _initY = 210; private ShowCard _currentCard; int my_hand1=0; private int _dragFromX = 105; private int _dragFromY = 210; private static final int IMAGE_WIDTH = 73; private static final int IMAGE_HEIGHT = 97; static ShowCard[] _deck = new ShowCard[104]; static ShowCard[] _deck83 = new ShowCard[83]; int[] cards = new int[104]; public Boolean[] nums = new Boolean[104]; AudioClip audioClip; String suits = "shdcshdc"; String faces = "a23456789tjqk"; int cardPosition = 0; Image card; Graphics offscreen; int n=104; private int[] x1 = {200,220,240,260,280,300,320,340,360,380}; private int[] y1 = {320,320,320,320,320,320,320,320,320,320}; private int[] x2 = {200,220,240,260,280,300,320,340,360,380}; private int[] y2 = {110,110,110,110,110,110,110,110,110,110}; private int[] x83= {100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183}; private int[] y83= {210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210,210}; private int[] x3 = {305}; private int[] y3 = {210}; private int[] My_one_drag = {400}; private int[] My_one_drag1 = {320}; Random rgen = new Random(); List<Integer> intList = new ArrayList<Integer>(); List<Integer> intList_ten = new ArrayList<Integer>(); List<Integer> intList_ten_user = new ArrayList<Integer>(); List<Integer> intList_ten_83 = new ArrayList<Integer>(); List<Integer> intList_ten_one = new ArrayList<Integer>(); int frame = 0; volatile Thread thread; boolean shouldDrawImage = false; public ShowCard() { Demo1(image,x,y); } public ShowCard(Image image, int x, int y) { this.image = image; this.x = x; this.y = y; //System.out.println("Card is: "+this.image); } public void Demo1(Image image, int x, int y) //here x and y is 0-51; { this.image = image; this.x = x; this.y = y; repaint(); //--- Add mouse listeners. } public void init() { setSize(800,530); setPreferredSize(new Dimension(300, 300)); audioClip = getAudioClip(getCodeBase(), "audio/TestSnd.wav"); img1 = getImage(getCodeBase(),"cards/b.gif"); Casino_table_img = getImage(getCodeBase(),"cards/casino_table2.gif"); try { System.out.println("1"); for (int suit=0; suit<suits.length(); suit++) { for (int face=0; face<faces.length(); face++) { //img = getImage(getCodeBase(),"cards/"+ faces.charAt(face) // + suits.charAt(suit) + ".gif"); card=getImage(getCodeBase(),"cards/"+ faces.charAt(face) + suits.charAt(suit) + ".gif"); _deck[cardPosition++] = new ShowCard(card, _initX++,_initY); } } }catch (Exception ex) { System.err.println("Caught Exception: " + ex.getMessage()); // handle exception... } for (int crd=0; crd<_deck.length; crd++) { intList.add(crd); } try { for(int j =0; j <10; j++) { //System.out.println(j); int randomPosition = rgen.nextInt(intList.size()); //System.out.println("Random no. is: "+randomPosition); intList_ten.add(randomPosition); intList.remove(randomPosition); } for(int j =0; j <10; j++) { int randomPosition = rgen.nextInt(intList.size()); // System.out.println("Random no. is: "+randomPosition); intList_ten_user.add(randomPosition); intList.remove(randomPosition); } for (int crd=0; crd<1; crd++) { int randomPosition = rgen.nextInt(intList.size()); intList_ten_one.add(randomPosition); intList.remove(randomPosition); } for (int crd=0; crd<intList.size(); crd++) { int randomPosition = rgen.nextInt(intList.size()); intList_ten_83.add(randomPosition); } } catch (Exception e) { System.err.println("Caught IOException: " + e.getMessage()); } this.addMouseListener(this); this.addMouseMotionListener(this); } public void start() { System.out.println("Start"); } public void stop() { } public void paint(Graphics g) { update(g); } public void update(Graphics g) { //System.out.println("paint"); super.paintComponents(g); // Required g.drawImage(Casino_table_img, 20, 20, this); try{ for(int j =0; j <10; j++)//for ten { ShowCard c = _deck[intList_ten.get(j)]; g.drawImage(c.image, x1[j],y1[j], this ); } for(int j =0; j <10; j++) { ShowCard c = _deck[intList_ten_user.get(j)]; g.drawImage(c.image,x2[j],y2[j], this); //g.drawImage(img1,x2[j],y2[j], this); } for(int j =0; j <1; j++) { ShowCard c = _deck[intList_ten_one.get(j)]; g.drawImage(c.image,x3[j],y3[j], this); } for (int crd=0; crd<intList_ten_83.size(); crd++) { ShowCard c = _deck[intList_ten_83.get(crd)]; //g.drawImage(c.image, c.x,c.y, this); g.drawImage(c.image,c.x83[crd],c.y83[crd], this); //g.drawImage(img1, c.x83[crd],c.y83[crd], this); if(crd==82) { break; } } if(my_hand1==1) { g.drawImage(_currentCard.image,My_one_drag[0],My_one_drag1[0], this); } } catch (Exception e) { System.err.println("Caught IOException: " + e.getMessage()); } g.dispose(); System.out.println("ok"); }//end paintComponent public void mousePressed(MouseEvent e) { try { for (int crd=82; crd<intList_ten_83.size(); crd--) { my_hand1=1; int x = e.getX(); // Save the x cord of the click int y = e.getY(); // _currentCard = null; ShowCard testCard = _deck[intList_ten_83.get(crd)]; if(intList_ten_83.get(crd)==-1) { break; } if (x >= testCard.x83[crd] && x <= (testCard.x83[crd] + IMAGE_WIDTH) && y>= testCard.x83[crd] && y <= (testCard.x83[crd] + IMAGE_HEIGHT)) { _dragFromX = x - testCard.x83[crd]; // how far from left _dragFromY = y - testCard.y83[crd]; // how far from top _currentCard = testCard; // Remember what we're dragging. //System.out.println(intList_ten_83.get(crd)); System.out.println( _currentCard); break; // Stop when we find the first match. } } } catch (Exception ex) { System.out.println(" mouse pressed Exception: " + ex.getMessage()); } }//end mousePressed /** Set x,y to mouse position and repaint. */ public void mouseDragged(MouseEvent e) { if (_currentCard != null) { for (int crd=0; crd<intList_ten_83.size(); crd++) { // Non-null if pressed inside card image. _currentCard.x83[crd] = e.getX() - _dragFromX; _currentCard.y83[crd] = e.getY() - _dragFromY; //--- Don't move the image off the screen sides _currentCard.x83[crd] = Math.max(_currentCard.x83[crd], 0); _currentCard.x83[crd] = Math.min(_currentCard.x83[crd], getWidth()-IMAGE_WIDTH); //System.out.println(e.getX() - _dragFromX); //--- Don't move the image off top or bottom _currentCard.y83[crd] = Math.max(_currentCard.y83[crd], 0); _currentCard.y83[crd] = Math.min(_currentCard.y83[crd], getHeight()-IMAGE_HEIGHT); repaint(); } } }//end mouseDragged public void mouseExited(MouseEvent e) { _currentCard = null; System.out.println("7"); }//end mouseExited //=============================================== Ignore other mouse events. public void mouseMoved (MouseEvent e) { } // ignore these events public void mouseEntered (MouseEvent e) { } // ignore these events public void mouseClicked (MouseEvent e) { } // ignore these events public void mouseReleased(MouseEvent e) { audioClip.play(); //paint(g); /*try { System.out.println("Released"); _currentCard.setSize(e.getX() - _currentCard.x, e.getY() - _currentCard.y); //Graphics g=image.getGraphics(); System.out.println("else"); g.drawImage(_currentCard.image,400,320, this); } catch (Exception ex) { System.out.println(" mouse relesed Exception: " + ex.getMessage()); }*/ } // ignore these events public void run() { Thread me = Thread.currentThread(); while (thread == me) { repaint(); } thread = null; } }
- 05-01-2012, 01:14 PM #2
Re: how to move my drag images.
Is this the same question, again?
how to move my card images after drop this drag image.
The code can not be tested without a set of images.If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 03:29 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 90
- Rep Power
- 0
- 05-01-2012, 04:07 PM #4
Re: how to move my drag images.
Try the PM.
If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 05:13 PM #5
Re: how to move my drag images.
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
how to move my card images after drop this drag image.
By ganesh.gothi@gmail.com in forum Java AppletsReplies: 4Last Post: 04-30-2012, 03:27 PM -
how to move this image
By melonka.sg in forum New To JavaReplies: 1Last Post: 01-08-2011, 04:53 PM -
how to mark on 2 images at a time,both images are on different JPanel
By smitharavi in forum AWT / SwingReplies: 0Last Post: 12-16-2010, 05:14 PM -
how to scroll 2 images at a time(synchronisation),both images are on different panels
By smitharavi in forum AWT / SwingReplies: 4Last Post: 12-16-2010, 04:32 PM -
Using Jscrollpane to move images on jpanel
By Lazairus in forum New To JavaReplies: 0Last Post: 12-06-2010, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks