Results 1 to 11 of 11
  1. #1
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Only one image being displayed at a time on my JFrame

    I don't know how to explain this properly but:

    main:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class GUI extends JFrame
    {
        private CardGamePanel computer = new CardGamePanel();
        private CardGamePanel player1 = new CardGamePanel();
        private CardGamePanel player2 = new CardGamePanel();
        
        private DisplayableDeck dd = new DisplayableDeck();
        private DisplayableHand dh1 = new DisplayableHand();
        private DisplayableHand dh2 = new DisplayableHand();
        
        DisplayableCard dc1 = new DisplayableCard(rank.Ace, suit.CLUBS);
        DisplayableCard dc2 = new DisplayableCard(rank.Six, suit.CLUBS);
        DisplayableCard dc3 = new DisplayableCard(rank.Queen, suit.HEARTS);
        
        public GUI()
        {
            setTitle("Card Game GUI");
            
            dd = new DisplayableDeck();
            dh1 = new DisplayableHand();
            dh2 = new DisplayableHand();
            
            dh1.addCard(dc1);
            dh1.addCard(dc2);
            dh1.addCard(dc3);
            
            dh2.addCard(dc1);
            dh2.addCard(dc2);
            dh2.addCard(dc3);
            
            computer.setItem(dd, 250,250);
            player1.setItem(dh1,100,100);
            player2.setItem(dh2,400,400);
            
            this.add(computer);
            this.add(player1);
            this.add(player2);
            
            setSize(600, 600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true); // This will paint the entire frame
            }
    
        public static void main(String[] args)
        {
            new GUI();
        }
    }
    Although it should be displaying images for computer, player1, and player2:

    this.add(computer);
    this.add(player1);
    this.add(player2);

    It only seems to display the final 'add' (ie. player 2 in this case). If i comment out player2, it displays player1.... but not all 3 at the same time :S

    how comes?

    Thank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Only one image being displayed at a time on my JFrame

    Google the Java Swing tutorial "Laying out Components in a Container" and read the BorderLayout section with great care. After reading this and understand that a JFrame's contentPane uses BorderLayout as its default layout, you will understand why your GUI is behaving as its behaving. (hint: when you add components in a default way to a BorderLayout using container, they get added by default to the BorderLayout.CENTER position).

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Only one image being displayed at a time on my JFrame

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Only one image being displayed at a time on my JFrame

    I've read up a bit on LayoutManagers, and ended up with this GUI

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GUI extends JFrame implements ActionListener
    
    {
        private JButton NewGame;
        private JButton NewHand;
        private CardGamePanel cg1;
        private DisplayableDeck dd;
        private DisplayableHand dh;
    
        public GUI()
            {
                super("Card Game GUI");
              
                setSize(600, 600);
                setLocation(200, 100);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
               
                getContentPane().setBackground(new Color(0xffff0f));
                getContentPane().add(setupSouthPanel(), BorderLayout.SOUTH);  
                getContentPane().add(setupCenterPanel(), BorderLayout.CENTER);
                setVisible(true);
            }
            
            private JPanel setupCenterPanel()
        {
            JPanel centerPanel = new JPanel();
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
            centerPanel.add(player1Panel());
            centerPanel.add(player2Panel());
            return centerPanel;
        }
            
            private JPanel player1Panel()
        {
            DisplayableCard dc1 = new DisplayableCard(rank.Ace, suit.CLUBS);
            DisplayableCard dc2 = new DisplayableCard(rank.Six, suit.CLUBS);
            DisplayableCard dc3 = new DisplayableCard(rank.Queen, suit.HEARTS);
            
            dh = new DisplayableHand();
            
            dh.addCard(dc1);
            dh.addCard(dc2);
            dh.addCard(dc3);
            
            cg1.setItem(dh,200,200);
    
            JPanel p = new JPanel();
            p.setLayout(new FlowLayout());
            
            p.setOpaque(true);
            p.add(new JLabel("Player 1", JLabel.LEFT));
            p.add(cg1);
            p.setBackground(new Color(0xffff0f));
            return p;
        }
        
        private JPanel player2Panel()
        {
            JPanel p = new JPanel();
            p.setLayout(new FlowLayout());
            
            p.setOpaque(true);
            p.add(new JLabel("Player 2", JLabel.RIGHT));
            p.setBackground(new Color(0xffff0f));
            return p;
        }
            
            private JPanel setupSouthPanel()
        {
            JPanel southPanel = new JPanel();
            southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
            southPanel.add(createNewGamePanel());
            southPanel.add(createNewHandPanel());
            return southPanel;
        }
            
            private JPanel createNewGamePanel()
        {
            NewGame = new JButton("New Game");
            NewGame.addActionListener(this);
            
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new FlowLayout());
            
            buttonPanel.setOpaque(true);
            buttonPanel.add(NewGame);
            return buttonPanel;
        }
        
        private JPanel createNewHandPanel()
        {
            NewHand = new JButton("New Hand");
            NewHand.addActionListener(this);
    
            JPanel p = new JPanel();
            p.setLayout(new FlowLayout());
            
            p.setOpaque(true);
            p.add(NewHand);
            return p;
        }
      
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (e.getSource() == NewGame)
                {
                    
                }
                else
                {
                    
                }
            }
            
            public static void main(String args[])
            {
                GUI d = new GUI();
            }
    }
    However, the player1Panel() is causing a NullPointerException for some reason :/

    Exception in thread "main" java.lang.NullPointerException
    at GUI.player1Panel(GUI.java:53)
    at GUI.setupCenterPanel(GUI.java:36)
    at GUI.<init>(GUI.java:28)
    at GUI.main(GUI.java:126)
    Java Result: 1
    Thanks for all your advice!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Only one image being displayed at a time on my JFrame

    Either post the code that resulted in the stack trace, or indicate which line is
    (GUI.java:53)
    in the actual code.

    Line 53 in the posted code is empty, and neither of lines 52 or 54 can cause a NPE.

    Or check for yourself which variable on line 53 hasn't been initialized.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Only one image being displayed at a time on my JFrame

    Look at that line. You're trying to dereference a variable that is null. You've declared the variable, but you never construct it, you never call its constructor.

  7. #7
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Only one image being displayed at a time on my JFrame

    Doh, so I've constructed the variables and the code now compiles but cg1 under both the player1Panel and player2Panel aren't actually being displayed although they are being added just like the buttons and text are... hmmm?



    cardgamepanel.java
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class CardGamePanel extends JPanel{
        private Displayable theItem;
        private int x, y;
    
        public void paint(Graphics g) {
            if (theItem != null)
                theItem.display(g, x, y);
        }
        
        public void setItem(Displayable item, int x, int y) {
            theItem = item;
            this.x = x;
            this.y = x;
        }
    }

  8. #8
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Only one image being displayed at a time on my JFrame

    I'm really not sure why it's no longer displaying, I'm doing the same as I was previously but using containers and LayoutManagers :/

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Only one image being displayed at a time on my JFrame

    Quote Originally Posted by ralph View Post
    I'm really not sure why it's no longer displaying, I'm doing the same as I was previously but using containers and LayoutManagers :/
    I find that it's hard for me to guess based on what you've posted so far. Consider creating and posting an SSCCE that demonstrates your problem.

  10. #10
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Only one image being displayed at a time on my JFrame

    I was going to upload a working version, before I used containers and layoutmanagers in a ZIP folder, but whenever I try to manage attachments on this website I just get a large grey box (Tried in both Chrome and IE)

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Only one image being displayed at a time on my JFrame

    Did you even go through the SSCCE link?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 08-09-2012, 06:18 PM
  2. How To Check What Image Is Displayed
    By anthropamorphic in forum New To Java
    Replies: 9
    Last Post: 08-14-2011, 12:58 PM
  3. Saving output displayed in a frame in image format
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 05-25-2011, 02:28 PM
  4. Do I make a new JFrame every time?
    By Swankee in forum AWT / Swing
    Replies: 2
    Last Post: 10-21-2009, 05:51 PM
  5. ImageIcon : Image not displayed
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •