0

I've been working on an applet and, for some reason, it erroring out and not reading some of the variables..I guess the last one may be connected to the first set of errors, but I'm not sure. Thank you(: I would like to fix this.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

public class Cards10 extends Applet
{
    Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10,      flipped;

public void init()
{
     String deckCards[] = {"c1.gif", "c2.gif", "c3.gif", "c4.gif", "c5.gif", "c6.gif", "c7.gif", "c8.gif", "c9.gif", "c10.gif", "cj.gif", "ck.gif", "cq.gif", "s1.gif", "s2.gif", "s3.gif", "s4.gif", "s5.gif", "s6.gif", "s7.gif", "s8.gif", "s9.gif", "s10.gif", "sj.gif", "sk.gif", "sq.gif", "d1.gif", "d2.gif", "d3.gif", "d4.gif", "d5.gif", "d6.gif", "d7.gif", "d8.gif", "d9.gif", "d10.gif", "dj.gif", "dk.gif", "dq.gif", "h1.gif", "h2.gif", "h3.gif", "h4.gif", "h5.gif", "h6.gif", "h7.gif", "h8.gif", "h9.gif", "h10.gif", "hj.gif", "hk.gif", "hq.gif"};
    for(int k = 0; k < 10; k++)
    {
        Random rand = new Random();
    int r = rand.nextInt(52);
    card(k) =  getImage( getDocumentBase(), String.format("%ss.gif", deckCards[r]) ); //I know I'm using String.format wrong here..is there anyway I can get it to format a string and include the .gif as it is now or must I take the '.gif" out of my list and simply do %2s.gif or is there any other way to avoid that?
        }
 flipped = getImage(getDocumentBase(), "b1fv.gif");
}

public void paint(Graphics g)
{
    g.drawImage(flipped, 10, 10, this);
    for (int i = 1; i < 6; i++)
    {
         g.drawImage(card(k), 10 + (20*i), 10, this);
    }
    for (int j = 6; j < 11; j++) /*I know this shouldn't be here as it would draw each card j times (same for i)..fixed on mine but is there any way to do this without listing them all out?*/ 
    {
         g.drawImage(card(k), 10 + (20*j), 125, this);
    }
}

public int shuffle(String [] deckCards)
{
    for (int n = 0; n < 10; n++);
    {
         Random rand = new Random();
         int r = rand.nextInt(52);
         return r;
    }   
}
}

here are the errors:

   Main.java:26: error: cannot find symbol
             g.drawImage(card(k), 10 + (20*i), 10, this);
                              ^
  symbol:   variable k
  location: class Cards10
Main.java:30: error: cannot find symbol
             g.drawImage(card(k), 10 + (20*j), 125, this);
                              ^
  symbol:   variable k
  location: class Cards10
LongGone
  • 35
  • 1
  • 1
  • 5
  • What is the error you are receiving? – matt forsythe Jan 03 '14 at 22:05
  • Editing code now, what do you want to achieve? This code has a lot of errors – nimsson Jan 03 '14 at 22:14
  • @mattforsythe I was about to..here they are now(: – LongGone Jan 03 '14 at 22:14
  • @nimson, I want to display random 10 cards in two rows of five. – LongGone Jan 03 '14 at 22:17
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jan 04 '14 at 03:28
  • Please use code formatting for code, **input/output** & structured documents like HTML or XML. To do that, select the sample and click the `{}` button above the messaged posting/editing form. – Andrew Thompson Jan 04 '14 at 03:28
  • @AndrewThompson for this one I think I'm going to stick to awt unless Swing would provide easier coding...Why is Swing so effective? Just due to it's more modern look? – LongGone Jan 04 '14 at 14:49
  • Swing is not inherently harder to learn than AWT, in fact, easier if only for the fact that when you ask a question about it, people are more likely to be able to help. The thing is, most of the people who ever *did* use AWT have largely forgotten the finer details. And the number of people who have *ever* worked with AWT (and visit Q&A sites like this) is shrinking rapidly. Some have retired from programming, others have passed away. Swing *does* have component that the AWT lacks, and might seem more difficult to learn for that reason. But if you only need to learn the AWT equivalents.. – Andrew Thompson Jan 04 '14 at 15:29
  • ..it should be easier due to 'the available help' discussed above. Oh, and of course, then there is the (ornery) people like me, who *did* program AWT, but simply won't help with it out of a purely stubborn desire to see it put to bed. – Andrew Thompson Jan 04 '14 at 15:30
  • Ahh, I see..I think my error actually has nothing to do wuth awt or Swing at this point.. Thank you for your advice(: – LongGone Jan 04 '14 at 15:34
  • BTW - note the 'top user' of both [applet](http://stackoverflow.com/tags/applet/topusers) & [AWT](http://stackoverflow.com/tags/awt/topusers). Look familiar. ;) – Andrew Thompson Jan 04 '14 at 15:36
  • @AndrewThompson :D Someone's on a roll(: I revised my code a bit to show what's happening a bit more... – LongGone Jan 04 '14 at 15:41
  • It's mostly an issue with string.format.. how can I fix this? and no, no card() – LongGone Jan 04 '14 at 15:57
  • Oh `String.format(..)` never used it much, to be quite honest. The best tip I can give for solving that is, create an [SSCCE](http://sscce.org/) that does not have anything to do with a GUI, and try and sort it in a normal app. with a `main(String[])`. Again, without the GUI, and especially without the applet, more people will be able to help. – Andrew Thompson Jan 04 '14 at 16:00
  • well, thank you(: what's a normal app? It just has a main method.. I didn't know there was such a thing. – LongGone Jan 04 '14 at 16:03
  • Weel, by 'normal' I simply mean an app. with a `main(String[])` that has no GUI, and dumps output using `System.out.println()` Do you remember that [Hello World](http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html) app. from the Java Tutorial? Like that. – Andrew Thompson Jan 04 '14 at 16:08
  • Ohh, just a regular program? – LongGone Jan 04 '14 at 16:14

2 Answers2

0

This is exactly what you want. We discussed the reasons in chat.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

public class Cards10 extends Applet
{

    String deckCards[] = {"c1.gif", "c2.gif", "c3.gif", "c4.gif", "c5.gif", "c6.gif", "c7.gif", "c8.gif", "c9.gif", "c10.gif", "cj.gif", "ck.gif", "cq.gif", "s1.gif", "s2.gif", "s3.gif", "s4.gif", "s5.gif", "s6.gif", "s7.gif", "s8.gif", "s9.gif", "s10.gif", "sj.gif", "sk.gif", "sq.gif", "d1.gif", "d2.gif", "d3.gif", "d4.gif", "d5.gif", "d6.gif", "d7.gif", "d8.gif", "d9.gif", "d10.gif", "dj.gif", "dk.gif", "dq.gif", "h1.gif", "h2.gif", "h3.gif", "h4.gif", "h5.gif", "h6.gif", "h7.gif", "h8.gif", "h9.gif", "h10.gif", "hj.gif", "hk.gif", "hq.gif"};
    Random rand = new Random();
    Image[] card = new Image[10];


    public void init()
    {
        for(int k = 0; k < 11; k++){
            int r = rand.nextInt(51);
            card[k] = getImage(getDocumentBase(), deckCards[r]);
        }
        flipped = getImage(getDocumentBase(), "b1fv.gif");
    }

    public void paint(Graphics g)
    {
        g.drawImage(flipped, 10, 10, this);
        for(int i = 1;i<6;i++) g.drawImage(card[i], 10 + (40*i), 10, this);
        for(int i = 6;i<10;i++) g.drawImage(card[i], 10 + (40*(i-6)), 125, this);
    }
}

This produces exactly the result that is wanted.

nimsson
  • 930
  • 1
  • 14
  • 27
  • What do you mean you'll edit when you get gifs? Okay, i mostly understand it, but I have a few questions.. 1. why return 0? 2. where did the return statement from image() go? 3. why is the second for statement needed in paint() 4. what happened to the returned r? Thank you so very much! – LongGone Jan 03 '14 at 22:24
  • I tried running it (compiled fine) but only the flipped card showed up. – LongGone Jan 03 '14 at 22:36
  • I mean that I want the gif files zipped so that I myself can run the program. I will explain everything when I have a final version out. I would suggest getting netbeans IDE. it will really help you. – nimsson Jan 03 '14 at 22:54
0

The errors are obvious. You are trying to access a k variable when there is none. You need to be using the correct i and j varables

g.drawImage(flipped, 10, 10, this);
for (int i = 1; i < 6; i++)
{
     g.drawImage(card(k), 10 + (20*i), 10, this);  <-- proabably want to access i
}
for (int j = 6; j < 11; j++) /*I know this shouldn't be here as it would draw each card j times (same for i)..fixed on mine but is there any way to do this without listing them all out?*/ 
{
     g.drawImage(card(k), 10 + (20*j), 125, this); <-- probably want to use j
}

Also,

  1. There is no card() method, unless I'm not just seeing.
  2. Maybe you want to use an Image[] array instead, because that's what it seems like that's what you are trying to do with card(v)

    Image[] cardImages = new Image[10];
    
  3. Then when you can initialize them like this

    cardImages[k] =  getImage( ...
    
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Right.. it doesn't read j either.. but I'll try that.. Although this helps, there's also an error with String.format that I'm trying to get around.. is there anyway I can get it to format a string and include the .gif as it is now or must I take the '.gif" out of my list and simply do %2s.gif or is there any other way to avoid that? } – LongGone Jan 04 '14 at 16:08
  • There's no need at all for the `String.format`, Just use `deckCard[k]` by itself, since the value is already the file name – Paul Samsotha Jan 04 '14 at 16:12
  • Yes.. It's within the init() method.. I have to generate random cards, which is why I'm using deckCards[r], hence string.format – LongGone Jan 04 '14 at 16:17
  • But you don't need to use a String format since the `deckCards[k]` is already a String. – Paul Samsotha Jan 04 '14 at 16:23
  • so i could just do deckCards[r] (generate random number that corresponds to index) without ther string format and it'd be fine. Wow, I totally overlooked that..I was originally doing something else, okay thanks(: – LongGone Jan 04 '14 at 16:25
  • Yeah, but I need to use string format because I'm formatting it as the file type.. like I want deckCards[r] to be in the form (which it already is..but how would I initialize it then?) "%2s.%3s"<-- I want it to replace that.. – LongGone Jan 04 '14 at 16:49
  • You don't need to initialize _anything_ . The array is _already_ initialized the way it is. – Paul Samsotha Jan 04 '14 at 16:53
  • But then it won't generate any random card.. do you see what i'm saying? like the format for getImage is: getImage( getDocumentBase(), "c1.gif" ) <-- how would I get deckCards[r] to be in place of the "c1.gif" or whatever? – LongGone Jan 04 '14 at 16:59
  • Why wouldn't it be random? The r is a random number between 0 and 51. You have 52 indices in your `deckCard` array. So it will generate a random index `r` . So I don't get why you don't think it won't be random. – Paul Samsotha Jan 04 '14 at 17:12
  • In other words, `r` is a random index of the `dekcCards` array. – Paul Samsotha Jan 04 '14 at 17:14
  • I just don't understand how to properly format it. would I just do getDocumentBase(), "deckCards[r]" ? – LongGone Jan 04 '14 at 17:18
  • You don't need the quotation mark. You really need to go over some basic array tutorials if you're going to try and use them. Here's a close to complete [Java tutorial](http://docs.oracle.com/javase/tutorial/reallybigindex.html) – Paul Samsotha Jan 04 '14 at 17:21
  • I understand the array, but it would initialize it as if it were something like "c2.gif" even without quotation marks and such? – LongGone Jan 04 '14 at 17:26
  • so I tried it that way and it works but only generates one random image 9 times.. – LongGone Jan 04 '14 at 17:30
  • Put a `System.out.println(deckCars[r]);` inside the loop after the getImage to see what it prints out. If they are different then it is likely you are drawing the images incorrectly. – Paul Samsotha Jan 04 '14 at 17:36