• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get a card deck applet working  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm trying to make an applet that displays images of playing cards. The applet should load a deck of 52 playing card images (the folder is called "images"). The applet should shuffle the deck (using a random number generator) and display the first 10 cards of the shuffled deck. I then have to display the cards in two rows of five cards each.

With this code I have about 100 errors and I'm not sure what I'm doing wrong:




Any help would be appreciated.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: this is the edited code, but I'm getting these errors.

unit12.java:155: error: illegal start of expression
public void paint(Graphics g)
^
unit12.java:155: error: illegal start of expression
public void paint(Graphics g)
^
unit12.java:155: error: ';' expected
public void paint(Graphics g)
^
unit12.java:155: error: ';' expected
public void paint(Graphics g)

 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Before going any further I'd recommend adopting a consistent indentation and bracing style. What you've got now is all over the place and isn't doing you any favors.

The reason for your errors is most likely unbalanced braces, but without having your indentation and braces lining up properly. It's really hard to say.

Proper formatting isn't just a matter of style -- it's an important coding tool.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry--the brackets had been fixed but it was still left in a weird format.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not looking fixed to me. The closing brackets are still rather randomly indented. This makes it difficult to see whether the braces are properly paired or not.

Again, this is not being nit-picky or overly pedantic. It's a useful tool to avoid these types of problems.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am positive that they were but I will align them accordingly. Edit: it appears that the exact indents don't translate exactly into the code format here no matter what I try.

 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the JavaScript thingy used to format code here probably does something different with spaces-vs.-tabs than your editor. Don't worry about it.

But Bear is right that it is a matter of missing closing braces. The last closing brace before the paint method (on line 146) matches the opening brace on line 142. So it can't possibly be the end of the method.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have also got the code inside the method indented one level; it should be two levels. You need consistent indentation as you have already been told, so you can see the structure of your code without having to read it.
We have some style suggestions on the Ranch, so you can have a third style convention to get confused about .
I recommend you learn to write backwards. Makes matching braces (and indentation if your editor supports it) much easier.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't the main method enclose the paint() method? That's what's confusing me.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method can't contain other methods.
 
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure your editor is configured to used spaces for indenting instead of tabs.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've added the one parenthesis that encloses the main method and keep it separate from the paint() method....but now I have new errors.

error: cannot find symbol
g.drawImage(card1, 10, 10, this);
^
symbol: variable card1
location: class unit12
unit12.java:158: error: cannot find symbol
g.drawImage(card2, 90, 10, this);
^
symbol: variable card2
location: class unit12
unit12.java:159: error: cannot find symbol
g.drawImage(card3, 170, 10, this);
^
symbol: variable card3
location: class unit12
unit12.java:160: error: cannot find symbol
g.drawImage(card4, 250, 10, this);
^
symbol: variable card4
location: class unit12
unit12.java:161: error: cannot find symbol
g.drawImage(card5, 330, 10, this);
^
symbol: variable card5
location: class unit12
unit12.java:163: error: cannot find symbol
g.drawImage(card6, 10, 50, this);
^
symbol: variable card6
location: class unit12
unit12.java:164: error: cannot find symbol
g.drawImage(card7, 90, 50, this);
^
symbol: variable card7
location: class unit12
unit12.java:165: error: cannot find symbol
g.drawImage(card8, 170, 50, this);
^
symbol: variable card8
location: class unit12
unit12.java:166: error: cannot find symbol
g.drawImage(card9, 250, 50, this);
^
symbol: variable card9
location: class unit12
unit12.java:167: error: cannot find symbol
g.drawImage(card10, 330, 50, this);
^
symbol: variable card10
location: class unit12
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've added the one parenthesis that encloses the main method and keep it separate from the paint() method

What does that mean, specifically? Apparently, something went wrong with that.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I mean (indicated by //) :

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, right. I read the error message too quickly - the braces are fine now. The problem is that all the variables are defined in the main method, so they're only visible in that method. If other methods need to use them, then you need to declare them outside of any method.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I declared the Image objects outside the main method....and the file compiled....but when I try to run it, I get this:

Main method is not static in class unit12, please define the main method as:
public static void main(String[] args)

But when I did define the main method as 'static'....100 errors occurred.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then those errors need to be addressed. Any variables referenced from a static context must also be static.

So you can make variables static -- which should only be done with great care and when warranted. Or jump into a non-static context as soon as possible.

I recommend the latter. Your main() method should do nothing except create an instance of the class and call into a method so that the code immediately enters a non-static context. It's a common mistake to try and do a lot of stuff in main().
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it keeps saying "error: non-static method getImage(URL,String) cannot be referenced from a static context." Same thing for getDocumentBase(), if that helps. Those are the only types of errors I'm getting.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That brings up a good point: are you writing a desktop app or an applet? The code currently mixes them both in a way that won't work for either.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An applet.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you should not have a main method anyway, because it will not be used. Read up on the applet lifecycle methods like init, start and stop.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should public void init() be used in place of the main method?

Edit: I tried it---still getting the same error when trying to run it.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paige An wrote:So I declared the Image objects outside the main method....and the file compiled....but when I try to run it, I get this:

Main method is not static in class unit12, please define the main method as:
public static void main(String[] args)

But when I did define the main method as 'static'....100 errors occurred.


First of all, are you running the program with the "java" command. That is not how Applets are run -- the "java" command, which uses the main() method, is for applications and not applet.

Second, regarding the "100 errors occured" argument, that's not how you fix applications. It is not, "you try (A) and you try (B)", and "(A) has less errors, so that's the right answer". In this case, the right answer is the main() method needs to be static for applications. So, you need to make the main() method static, *and* you need to fix the 100 errors.

Henry
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

I mentioned what these errors were earlier in the thread--they all had to do with getImage and getDocumentBase not being found because they are non-static and I had added a static main method. This is an applet, so main methods supposedly don't work.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Perhaps, we need to restart from square one -- if only to get us on the same page as you. Can you tell us how are you running this applet? Can you show us the HTML that is used to load the applet?

Henry
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd assume I'd be running it from appletviewer.

the HTML file would look like this:

<html>
<head>
<title>Applet</title>
</head>
<body>
<applet
code="unit12.class"
width="400" height="200"> </applet>
</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And... you have rewritten the code to conform to the Applet lifecycle methods as Ulf suggested? If so, maybe you should show the code.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've probably said this hundreds of times in these forums, but I would suggest you throw all that code away and start over.

Never never never NEVER write that much code before you compile and get what you have to WORK. My rule of thumb is about 3 lines of new code MAX before I recompile and test. Writing 164 lines and then trying to figure out why you have 100+ errors is nothing but a way to drive you to drink.

If you feel most of this code is correct, then at MINIMUM comment out 99% of it, and just add in a piece at a time.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paige An wrote:I'd assume I'd be running it from appletviewer.

the HTML file would look like this:

<html>
<head>
<title>Applet</title>
</head>
<body>
<applet
code="unit12.class"
width="400" height="200"> </applet>
</body>
</html>



What do you mean by "assume"? Did you run it with appletviewer? or didn't you?

In other words, what did you get when you ran it?

Henry
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran this with appletviewer and nothing happened:

<html>
<head>
<title>Applet</title>
</head>
<body>
<applet
code="unit12.class"
width="600" height="600"> </applet>
</body>
</html>

Those 100 errors ALL have to do with the same thing: getDocumentBase() and getImage() do not work with the static method.

This is the new code:

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run it, the first thing that happens is an ArrayIndexOutOfBoundsException - because you're assigning one more element to the "cards" array than it is long. Once that is fixed nothing visible happens - but that is expected because I have no "images" subdirectory.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So should it be Image[] cards = new Image[52]?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paige An wrote:So should it be Image[] cards = new Image[52]?


you should declare the array with how many elements it needs to have. If your array needs 52 elements, then yes. If it needs 54 (jokers?) then declare it to be 54.

but whatever you declare it, the elements are numbers 0 through <one less than what you declare>
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Paige An wrote:So should it be Image[] cards = new Image[52]?


you should declare the array with how many elements it needs to have. If your array needs 52 elements, then yes. If it needs 54 (jokers?) then declare it to be 54.

but whatever you declare it, the elements are numbers 0 through <one less than what you declare>


That makes a lot more sense. Thanks everyone for the help so far.

So the change was made to the "cards" array. I created an html file:

<html>
<head>
<title>Applet</title>
</head>
<body>
<applet
code="unit12.class"
width=“1200” height=“1200”> </applet>
</body>
</html>

And tried running it on appletviewer....and absolutely nothing happened. The HTML file is called 'unit12' and I entered 'appletviewer unit12.html' (without quotes) after declaring the directory first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!