Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Java Cards Applet

    I am working on a simple applet (as practice, i'm new to java applets), but whenever i load the web page, nothing appears (im pretty sure my html tag is good). there are no errors that are obvious to me, but, as i said i'm new to java applets and there is probably an obvious error. does anyone have an idea as to why its not working? all i'm trying to do is shuffle the deck and then display 10 cards
    Java Code:
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.net.URL;
    import java.net.MalformedURLException;
    
    public class CTYCardsApp extends Applet {
    	public Image[] cards = new Image[52];
    	public Boolean[] nums = new Boolean[52];
    	public void init() {
    		int i;
    		for (i = 0; i < 52; i++) {
    			nums[i] = false;
    		}
    		URL docbase = getDocumentBase();
    		try {
    			docbase = new URL(getDocumentBase(), "\\images");
    		} catch (MalformedURLException e) { }
    		int c = 0;
    		for(i = 1; i <= 10; i++) {
    			cards[c] = getImage(docbase, "c" + Integer.toString(i) + ".gif");
    			c++;
    		}
    		for(i = 1; i <= 10; i++) {
    			cards[c] = getImage(docbase, "s" + Integer.toString(i) + ".gif");
    			c++;
    		}
    		for(i = 1; i <= 10; i++) {
    			cards[c] = getImage(docbase, "d" + Integer.toString(i) + ".gif");
    			c++;
    		}
    		for(i = 1; i <= 10; i++) {
    			cards[c] = getImage(docbase, "h" + Integer.toString(i) + ".gif");
    			c++;
    		}
    		cards[40] = getImage(docbase, "cj.gif");
    		cards[41] = getImage(docbase, "ck.gif");
    		cards[42] = getImage(docbase, "cq.gif");
    		cards[43] = getImage(docbase, "dj.gif");
    		cards[44] = getImage(docbase, "dk.gif");
    		cards[45] = getImage(docbase, "dq.gif");
    		cards[46] = getImage(docbase, "hj.gif");
    		cards[47] = getImage(docbase, "hk.gif");
    		cards[48] = getImage(docbase, "hq.gif");
    		cards[49] = getImage(docbase, "sj.gif");
    		cards[50] = getImage(docbase, "sk.gif");
    		cards[51] = getImage(docbase, "sq.gif");
    		shuffle();
    	}	
    	public void paint(Graphics g) {
    		int c = 20, i;
    		for(i = 0; i < 5; i++) {
    			g.drawImage(cards[i], c, 20, this);
    			c += 100;
    		}
    		c = 20;
    		for(i = 5; i < 10; i++) {
    			g.drawImage(cards[i], c, 140, this);
    			c += 100;
    		}
    	}
    	private void shuffle() {
    		int i;
    		Image[] temp = new Image[52];
    		for(i = 0; i < 52; i++) {
    			temp[i] = cards[i];
    		}
    		for(i = 0; i < 52; i++) {
    			cards[i] = temp[rand()];
    		}
    	}
    	private int rand() {
    		int num;
    		double n;
    		do {
    			n = Math.random() * 52.;
    			num = (int) n;
    		} while (nums[num]);
    		nums[num] = true;
    		return num;
    	}
    }

    html code:
    Java Code:
    <html>
    <applet code="CTYCardsApp.class" width="500" height="300"/>
    </html>
    Last edited by thisismyusername; 07-05-2011 at 04:56 AM. Reason: adding info

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    You need to show the HTML you are using also.
    Add some println() statements to the code to show where execution flow is going.

    Look in the browser's Java console for error messages.

  3. #3
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You need to show the HTML you are using also.
    Add some println() statements to the code to show where execution flow is going.

    Look in the browser's Java console for error messages.
    Norm, thanks for the suggestions. I added the html to my post. I'd already tried looking at the java console, but it didn't show any errors and from looking at it I would have thought the applet was running normally (it also said the images were succesfully loaded). Also, about the println(), I tried that earlier too, but was unable to find the output, where does it print to?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    unable to find the output, where does it print to?
    It prints on the java console. If there was no output, the println statements were not executed.
    Where did you add the printlns that they were not executed?

  5. #5
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    It prints on the java console. If there was no output, the println statements were not executed.
    Where did you add the printlns that they were not executed?
    I had them at various places throughout the program, but I didn't realize they'd output to the console. So I checked the java console, and the println() statements appeared. I used the println() to find out the rand() function is returning correctly, and paint() is running (a println() at the end of it worked). So the drawImage() statement isn't working?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Add a println to print the image that drawImage() is trying to draw.

  7. #7
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    docbase = new URL(getDocumentBase(), "\\images");
    this statement turned out to be the problem. For whatever reason, it didn't work, and all the Image objects were empty of data. I fixed the problem by moving the images to the same folder as the .class file and removing this statement.

Similar Threads

  1. Help with creating a deck of cards
    By Carouselification in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 04:45 AM
  2. Deck of cards problem
    By VelvetMirror in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 08:02 PM
  3. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 PM
  4. Help with Random cards
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 08:48 PM

Posting Permissions

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