Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Want two deck card in my applet

    My Java Applet code is:-
    Java Code:
    package com.progresso;
    
    import java.applet.AudioClip;
    import java.awt.Button;
    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.Random;
    
    import javax.swing.JApplet;
    
    public class ShowCard extends JApplet implements MouseListener, 
    MouseMotionListener {
    
    	
    	private static final long serialVersionUID = 1L;
    	Image image,image1,img,img1,Casino_table_img;
    	MediaTracker tr;
        int x;
        int y;
      	private static final int IMAGE_WIDTH = 73;
        private static final int IMAGE_HEIGHT = 97;
        //--- instance variables
        /** Initial image coords. */
        private int _initX     = 0;   // x coord - set from drag
        private int _initY     = 0;   // y coord - set from drag
        
        /** Position in image of mouse press to make dragging look better. */
        private int _dragFromX = 0;  // Displacement inside image of mouse press.
        private int _dragFromY = 0;
        
        private ShowCard[] _deck = new ShowCard[52];
        private ShowCard _currentCard = null;  // Current draggable card.
        Button play,stop;
        AudioClip audioClip;
        String suits = "shdcshdc";
        String faces = "a23456789tjqk";
        int cardPosition = 0;
        private Random ran;
    
        
            public ShowCard()
            {
            	Demo1(image,x,y);
            }
            public ShowCard(Image image, int x, int y) {
        	this.image = image;
        	this.image1 = image1;
            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.image1 = image1;
            this.x = x;
            this.y = y;
            
                //--- Add mouse listeners.
           }
    	
    	  public void init()
    	   {
    		audioClip = getAudioClip(getCodeBase(), "audio/TestSnd.wav");
    		
            try {                
                //image = ImageIO.read(new File("cards/2c.gif"));
            	System.out.println("1");
            for (int suit=0; suit<suits.length(); suit++) 
            {
                for (int face=0; face<faces.length(); face++) 
                {
                	
                    //ImageIcon img = new ImageIcon("cards/"+ faces.charAt(face)
                                                 //+ suits.charAt(suit) + ".gif");
                	tr = new MediaTracker(this);
                	img = getImage(getCodeBase(),"cards/"+ faces.charAt(face)
                              + suits.charAt(suit) + ".gif");
                	
                	img1 = getImage(getCodeBase(),"cards/"+ faces.charAt(face)
                            + suits.charAt(suit) + ".gif");
                	System.out.println(img);
                	Casino_table_img = getImage(getCodeBase(),"cards/casino_table2.gif");
                	//tr.addImage(img,0);
                	  //g.drawImage(img, 0, 0, this);
                    //image = ImageIO.read(new File("cards/"+ faces.charAt(face)
                          //  + suits.charAt(suit) + ".gif"));// all card fetch from here in loop
                    _deck[cardPosition++] = new ShowCard(img, _initX++, _initY++);
                                             
                    //System.out.println("1");
                   
                }
               
            }
           }catch (Exception ex) {
            	ex.printStackTrace(System.out); 
                // handle exception...
           }
            this.addMouseListener(this); 
            this.addMouseMotionListener(this);
    	   }
        public void paint(Graphics g) {
        	
            super.paintComponents(g);   // Required
            //-- Display the cards, starting with the first array element.
            //   The array order defines the z-axis depth.
            
            int crd=0;
            g.drawImage(Casino_table_img, 20, 20, this);
            try{
            for(int i=0;i<40;i++)
            {
            for (crd=0; crd<_deck.length; crd++) {
            	 ShowCard c = _deck[crd];
                
                //c.image.paintIcon(this, g, c.x, c.y);// here c.x=0-51 and c.y=0-51
                //g.drawOval(150, 150, 100, 100);
                g.drawImage(c.img, c.x,c.y, this);
               // g.drawImage(c.image1, c.x+52,c.y+52, this);
                //System.out.println("OK");
               	
    			} 
            
            }
            }catch (Exception e) {
            	e.printStackTrace(System.out); 
    		}
               //g.drawString("Progresso Application", 100, 50);
                
               
        
        }//end paintComponent
        
      //===================================================== method mousePressed
        /** When the mousePressed listener is called,
        check if it's in the area of a card..  If it is, 
        <ol>
        <li>_currentCard is set so mouseDragged knows what to drag.</li>
        <li>Record where in the image (relative to the upper left coordinates) 
            the mouse was clicked, because it looks best if we drag from there.</li>
        </ol>
        TODO: Move the card to the last position so that it displays on top.
        */
       
    	public void mousePressed(MouseEvent e) {
            int x = e.getX();   // Save the x coord of the click
            int y = e.getY();   // Save the y coord of the click
            
            //-- Find card image this is in.  Check from top down.
            _currentCard = null;  // Assume not in any image.
            for (int crd=_deck.length-1; crd>=0; crd--) {
            	ShowCard testCard = _deck[crd];
                if (x >= testCard.x && x <= (testCard.x + IMAGE_WIDTH)
                        && y >= testCard.y && y <= (testCard.y + IMAGE_HEIGHT)) {
                    _dragFromX = x - testCard.x;  // how far from left
                    _dragFromY = y - testCard.y;  // how far from top
                    _currentCard = testCard;  // Remember what we're dragging.
                    System.out.println(_currentCard);
                   
                    
                  
                    break;   // Stop when we find the first match.
                }
            }
        }//end mousePressed
        
     
        //============================================================ mouseDragged
        /** Set x,y to mouse position and repaint. */
        public void mouseDragged(MouseEvent e) {
            if (_currentCard != null) {   // Non-null if pressed inside card image.
                _currentCard.x = e.getX() - _dragFromX;
                _currentCard.y = e.getY() - _dragFromY;
                
                //--- Don't move the image off the screen sides
                _currentCard.x = Math.max(_currentCard.x, 0);
                _currentCard.x = Math.min(_currentCard.x, getWidth()-IMAGE_WIDTH);
                
                //--- Don't move the image off top or bottom
                _currentCard.y = Math.max(_currentCard.y, 0);
                _currentCard.y = Math.min(_currentCard.y, getHeight()-IMAGE_HEIGHT);
                audioClip.play();
                this.repaint(); // Repaint because position changed.
            }
        }//end mouseDragged
    
        //====================================================== method mouseExited
        /** Turn off dragging if mouse exits panel. */
        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) {  }  // ignore these events
    	
    	
    }

    This code is draw one deck of cards I want to two deck of same cards what ca I do, and what will the changes in the above code , Please help me ASAP.

    Last edited by Norm; 04-16-2012 at 01:21 PM. Reason: added code tags

  2. #2
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Want two deck card in my applet

    I solved my Double deck problem, but I want to use shuffle of cards for 3 second and then deal it, I am new in applet and this functionality so please give a way that how I can add this shuffle and deal functionality in my program. And also give me the code or demo for my problem if possible.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Want two deck card in my applet

    Quote Originally Posted by ganesh.gothi@gmail.com View Post
    And also give me the code or demo for my problem if possible.
    This is a forum, not a code factory. We don't hand out code, so don't even ask.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Want two deck card in my applet

    Ok.
    So please tell me the detail or way that how I can do this shuffle and Deal of card in the post #1 Applet.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Want two deck card in my applet

    The Collections class has a shuffle() method.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 11
    Last Post: 11-25-2011, 04:56 AM
  2. Card memory game applet.
    By Soufly in forum AWT / Swing
    Replies: 2
    Last Post: 04-21-2011, 04:02 AM
  3. Creating a card class for a Deck of cards
    By adjit in forum New To Java
    Replies: 13
    Last Post: 04-06-2011, 10:20 PM
  4. Card deck arrays?
    By linc186 in forum New To Java
    Replies: 2
    Last Post: 03-16-2011, 09:56 PM
  5. Replies: 2
    Last Post: 12-13-2010, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •