Results 1 to 7 of 7
Thread: Java Cards Applet
- 07-05-2011, 03:30 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
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
- 07-05-2011, 04:28 AM #2
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.
- 07-05-2011, 05:00 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
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?
- 07-05-2011, 01:27 PM #4
It prints on the java console. If there was no output, the println statements were not executed.unable to find the output, where does it print to?
Where did you add the printlns that they were not executed?
- 07-05-2011, 08:37 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
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?
- 07-05-2011, 10:38 PM #6
Add a println to print the image that drawImage() is trying to draw.
- 07-09-2011, 02:36 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Help with creating a deck of cards
By Carouselification in forum New To JavaReplies: 5Last Post: 03-06-2011, 04:45 AM -
Deck of cards problem
By VelvetMirror in forum New To JavaReplies: 2Last Post: 02-16-2011, 08:02 PM -
Deck of Cards
By khunmato in forum New To JavaReplies: 13Last Post: 09-06-2009, 05:47 PM -
Help with Random cards
By carl in forum Java AppletsReplies: 1Last Post: 08-03-2007, 08:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks