6 Replies - 1506 Views - Last Post: 19 April 2012 - 09:28 PM Rate Topic: -----

#1 ganesh.gothi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-April 12

Applet is not working first time and not load fully, also it's han

Posted 18 April 2012 - 12:22 AM

Please help me, my applet is not working first time and not fully loaded on browser, when I do reload the applet its working, and also this applet is hanging all the time,help It's urgent now.....

My full java code is:-

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.net.URL;
import java.util.ArrayList;
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=5;
    int y=5;
  	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     = 150;   // 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;
    
    static ShowCard[] _deck = new ShowCard[104];
    public Image[] cards = new Image[52];
    public Boolean[] nums = new Boolean[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;
    static ArrayList<Image> card;
    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[] x3 = {305};
    private int[] y3 = {210};



    
        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;
        
            //--- Add mouse listeners.
       }
	
	  public void init()
	   {
		  setSize(500, 500);
		  audioClip = getAudioClip(getCodeBase(), "audio/TestSnd.wav");
		  URL url = getDocumentBase();
	      card = new ArrayList<Image>();
	      ran = new Random();
        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/b.gif");
            	card.add(getImage(url,"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 start()
	  {
		  System.out.println("Start");
		  
	  }
	  
	  
	  
    public void paint(Graphics g) {
    	System.out.println("paint");
        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 (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.image, c.x,c.y, this);
            g.drawImage(img1, c.x,c.y, this);
            //g.drawImage( card.remove(ran.nextInt(card.size())), c.x,c.y,  this );
        } 
        
        }catch (Exception e) {
        	e.printStackTrace(System.out);
        	System.err.println("Caught IOException: " + e.getMessage());
		
        }
        
        // FOR TEN CARDS
        try{
        
            for(int j =0; j <10; j++)
            {
            	System.out.println("First");
            	
               g.drawImage( card.remove(ran.nextInt(card.size())), x1[j],y1[j], this );
               
               g.drawImage( card.remove(ran.nextInt(card.size())), x2[j],y2[j], this );
               g.drawImage(img1,x2[j],y2[j], this);
                //System.out.println( card.remove(ran.nextInt(card.size())));//randomlly generate all 104 cards.
                //continue;
                
            }
            g.drawImage( card.remove(ran.nextInt(card.size())), x3[0],y3[0], this );
            
       
           }catch (Exception e) {
           	e.printStackTrace(System.out);
           	System.err.println("Caught IOException: " + e.getMessage());
   		
           }
      
           //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.
                              
                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);
            
            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) { 
    	audioClip.play();
    }  // ignore these events
	
	
}




Please help me I am in frustration for solve this problem.

Is This A Good Question/Topic? 0
  • +

Replies To: Applet is not working first time and not load fully, also it's han

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,613
  • Joined: 20-September 08

Re: Applet is not working first time and not load fully, also it's han

Posted 18 April 2012 - 04:07 AM

It's almost certainly because the gui thread is being blocked by too much work. You might have to load your resources in the background before your applet is ready for use.

In the meantime though, enable the Java Applet Console and make sure there are no errors showing
Was This Post Helpful? 0
  • +
  • -

#3 ganesh.gothi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-April 12

Re: Applet is not working first time and not load fully, also it's han

Posted 18 April 2012 - 04:34 AM

When I running applet no error at console, when I am dragging card in bunch of 104 its giving error, The error is---


java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:250)
at com.progresso.ShowCard.paint(ShowCard.java:151)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:734)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
Caught IOException: n must be positive
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
paint
First
java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:250)
at com.progresso.ShowCard.paint(ShowCard.java:145)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:734)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caught IOException: n must be positive

Line tahi occure error is:-
com.progresso.ShowCard.paint(ShowCard.java:151) In my program line is-
g.drawImage( card.remove(ran.nextInt(card.size())), x3[0],y3[0], this );
and
com.progresso.ShowCard.paint(ShowCard.java:145) In my program line is-
g.drawImage( card.remove(ran.nextInt(card.size())), x1[j],y1[j], this );

This error is important to show applet at first time on browser, second time is working , please help me what can I do.

please check my applet at My link
click play game and window will be open but not show full applet , after this close this window and again click play game, then o/p will be 10 cards above and 10 cards bellow and 104 cards in the bunch, so please help me to find out this problem .
My code is same posted.

My link is---

http://mindcrew.co.i...ion/WebContent/

please try this.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,613
  • Joined: 20-September 08

Re: Applet is not working first time and not load fully, also it's han

Posted 18 April 2012 - 11:10 AM

Please think carefully before you answer this question: are you certain that the code you posted above is the code that is running in the applet?
Was This Post Helpful? 0
  • +
  • -

#5 ganesh.gothi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-April 12

Re: Applet is not working first time and not load fully, also it's han

Posted 18 April 2012 - 10:57 PM

Yes its working and not giving ERROR at all.....
only my browser is not showing applet when its goes down , first time applet is showing on browser.......
You have checked link that I have posted,again I am giving a link so please understand my problem, please check link at once,........please http://mindcrew.co.i...ion/WebContent/
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,613
  • Joined: 20-September 08

Re: Applet is not working first time and not load fully, also it's han

Posted 19 April 2012 - 08:08 AM

You didn't answer the question i asked you
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Applet is not working first time and not load fully, also it's han

Posted 19 April 2012 - 09:28 PM

g.drawImage( card.remove(ran.nextInt(card.size())), x3[0],y3[0], this );

how can an ArrayList size() method return a negative number ???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1