Welcome to Dream.In.Code
Become a Java Expert!

Join 150,053 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,733 people online right now. Registration is fast and FREE... Join Now!




Swing components on top of Images

 
Reply to this topicStart new topic

Swing components on top of Images

clatcho
7 Jun, 2008 - 07:15 AM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
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;
      }
   }
}


User is offlineProfile CardPM
+Quote Post

mensahero
RE: Swing Components On Top Of Images
7 Jun, 2008 - 07:41 AM
Post #2

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions

IMO.. if its an IMAGE .. (i.e. .jpg, .gif, etc) its better to load them inside a JLabel (ImageIcon) if your using swing.. that way the images would be more flexible.. rather done drawing them.. and it could be easily ported to applet if your doing it as an application and decided to convert..


java

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;
}
}



did you try removing the "this" in your g.drawimage..? I don't use it when I'm doing a paint method..
User is offlineProfile CardPM
+Quote Post

pbl
RE: Swing Components On Top Of Images
7 Jun, 2008 - 08:58 AM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(clatcho @ 7 Jun, 2008 - 08:15 AM) *

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


   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;
      }
   }
}



It is because you have overloaded the paint() method which draws the Swing components
So you have to call it
By the way put your cards in JLabel as Mensahero suggested then Swing will paint them for you

CODE

   public void paint(Graphics g)
   {
      g.drawImage(backgroundImage, 0, 0, this);
     // have the swing components repainted
     super.paint(g);  
    }


This post has been edited by pbl: 7 Jun, 2008 - 09:14 AM
User is online!Profile CardPM
+Quote Post

clatcho
RE: Swing Components On Top Of Images
7 Jun, 2008 - 09:19 AM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Much appreciated, got it working now
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month