0

I am trying to write a very simple Blackjack game.

This is the class that is supposed to show the currently drawn card:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ShowRandomCard {
    JFrame f = new JFrame();
    JPanel p = new JPanel();

public void ShowUsARandomCard() {
    f.setLayout(new BorderLayout());
    p.add(new JLabel("A Panel"));
    f.add(p, BorderLayout.NORTH);

    // Picture
    BufferedImage myPicture = null;
    try {
        myPicture = ImageIO.read(new File("somepicture"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    JLabel picLabel = new JLabel(new ImageIcon(myPicture));
    f.add(picLabel,BorderLayout.NORTH);

    // elements

    f.pack();
    f.setVisible(true);

    Blackjack jt = new Blackjack();
    jt.dialog();

}

public void hideCards() {
    f.setVisible(false);
    f.remove(p);
    f.dispose();
    f.repaint();
}
}

And this is the actual game class:

import static javax.swing.JOptionPane.*;

public class Blackjack {

    ShowRandomCard it = new ShowRandomCard();

   public void dialog() {

        int answer2 = showConfirmDialog(null, "some message", "some title",
                YES_NO_OPTION);
        if (answer2 == YES_OPTION) {
            garbageCollection();
            it.ShowUsARandomCard();
        if (answer2 == NO_OPTION || answer2 == CANCEL_OPTION) {
            garbageCollection();
        //  System.exit(0);
        }
        }
   }

public void garbageCollection() {
    it.hideCards();
}
 }

But the JPanel that holds the cards does not disappear. Any help would be appreciated.

4
  • For better help sooner, post an SSCCE. Apr 26, 2013 at 17:28
  • well i have tried my best to show only the essential parts
    – herecy
    Apr 26, 2013 at 17:30
  • remember to call JFrame.redraw() after removing the card Apr 26, 2013 at 17:32
  • I always get "The method redraw is undefined...". Tried with f.repaint(); but the panel is still shown.
    – herecy
    Apr 26, 2013 at 17:40

1 Answer 1

3

When you remove a component from a visible frame the basic code is:

panel.remove(...);
panel.revalidate();
panel.repaint();

However, there is rarely a good reason to use code like that. Instead you should be using a Card Layout to hide/show panels.

Method names do not start with an upper case character. "GarbageCollection()" should be GarbageCollection()

3
  • Unfortunately f.repaint(); has no effect. The panel still stays. Thank you for pointing out the name convention, overlooked it.
    – herecy
    Apr 26, 2013 at 17:50
  • The revalidate() is the key, but again you completely ignored the proper solution which is to use a Card Layout. You still haven't posted a proper SSCCE, so I have no more help to offer.
    – camickr
    Apr 26, 2013 at 17:51
  • No need for more help, revalidate was indeed the key. Thank you! Btw: have updated the code above, just for you ;-).
    – herecy
    Apr 26, 2013 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.