I am attempting to create a card game, 21, the game works fine, but i am having bother creating the GUI
I have created a JFrame which will have a background image (green felt) then the card will be displayed on top of that, so far so good
I now want to add swing components on top of the images, for just now a simple JButton
My problem is when i paint i can no longer see the JButton, i know this is something to do with the way paint works, i think when paint is called, its basically painting over the whole screen and i can no longer see it, i just can not figure it out.
Any help would be much appreciated.
CODE
package twentyone;
import javax.swing.*;
import java.awt.*;
import java.util.*;
/**
*
* @author andy
*/
public class GUI extends JFrame
{
private Image backgroundImage;
private MediaTracker tracker;
private ArrayList <Image> cardImages;
private Dealer theDealer;
private JPanel infoArea;
private JTextArea handValue;
private JButton button;
/** Creates a new instance of GUI */
public GUI()
{
// Create an instance of MediaTracker, an instance of the backgroundImage
// Add the image to the tracker
// Wait for the image to be loaded
theDealer = new Dealer();
cardImages = new ArrayList<Image>();
tracker = new MediaTracker(this);
backgroundImage = Toolkit.getDefaultToolkit().getImage("cardImages\\background.jpg");
tracker.addImage(backgroundImage,0);
try
{
tracker.waitForID(0);
}
catch(InterruptedException ie)
{
System.out.println("Error loading images : " + ie.getMessage());
}
// Set the size of the JFrame to the same size as the backgroundImage
this.setSize(backgroundImage.getWidth(this), backgroundImage.getHeight(this));
this.setTitle("TwentyOne");
this.setLocation(50,50);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// i need this button to be displayed on top of the background image
infoArea = new JPanel();
button = new JButton("Hello World");
infoArea.add(button);
Container cp = getContentPane();
cp.add(infoArea);
loadCard();
}
public void loadCard()
{
Image currentCard = Toolkit.getDefaultToolkit().getImage("cardImages\\" + theDealer.getCardImage());
System.out.println(theDealer.getCardImage());
tracker.addImage(currentCard,0);
try
{
tracker.waitForID(0);
}
catch(InterruptedException e)
{
System.out.println("Error in disaplyCard : " + e.getMessage());
}
cardImages.add(currentCard);
}
public void paint(Graphics g)
{
g.drawImage(backgroundImage, 0, 0, this);
int numbr = 20;
for(Image output : cardImages)
{
g.drawImage(output, numbr, 40, this);
numbr = numbr + 75;
}
}
}