Results 1 to 10 of 10
  1. #1
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default can't get anything to display on my JPanel

    I'm writing this program to screw with my cousin, she's 15 and learning to drive and she drove a car her first time with me and was so excited and freaked out she was forgetting right and left. SO I'm writing this program to 'quiz' her on lefts and rights hehe.

    Anyway, I laid everything out but no dice, nothing is showing up. I get the feeling I have a case of heavyweight vs. lightweight components on my hands, but if someone could point me toward the light I would be very grateful.

    I'm pretty green when it comes to Swing so I've been coding a little at a time then compiling and getting it to display and moving on, so yea I do see that my buttons don't do anything ;) I'm just trying to see them first.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class BG extends JPanel
    {   
        Image image;
        
        public BG()
        {
            
            try
            {
                image = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("bgImage.jpg"), "bgImage.jpg"));
            }
            catch (Exception e) { /*handled in paintComponent()*/ }
        }
    
        protected void paintComponent(Graphics g)
        {
            
            if (image != null)
                g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
        }
          
        public void prompt()
          {  
             GridLayout gridlayout1 = new GridLayout( 4, 3, 5, 5 );
             setLayout( gridlayout1 );
             
    
             
             add( new JLabel( "" )); //1,1
             
             JLabel intro = new JLabel( "Is it left or is it right?" );
                intro.setToolTipText( "Quit screwing around." );        //1,2
                intro.setHorizontalAlignment( SwingConstants.CENTER );
                add( intro );
                
             add( new JLabel( "" )); //1, 3
             
             add( new JLabel( "" ));  //2, 1
             
             Icon arrowFace = new ImageIcon( getClass().getResource( "question.png" ) ); //2,2
             JLabel arrow = new JLabel( arrowFace );
                add( arrow );
             
             add( new JLabel( "" )); //2,3
             
             add( new JLabel( "" )); //3,1
             
             JButton left = new JButton( "Left?" ); //3,2
                add( left );
             
             add( new JLabel( "" )); //3,3
             
             add( new JLabel( "" )); //4,1
             
             JButton right = new JButton( "Right?" ); //4,2
                add( right );
         
             Random genRandom = new Random();
             int direction = genRandom.nextInt( 1 );     
                
             Icon arrowRight = new ImageIcon( "arrowRight.png" );
             Icon arrowLeft = new ImageIcon( "arrowLeft.png" );
                  
                    
         }
    }

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class main
    {
    	public static void main( String args[] )
    	{
    		JFrame frame = new JFrame( "Left or Right??" );
    		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		
    		BG backGround = new BG();
    		frame.add( backGround );
    		frame.setSize( 700, 700 );
    		frame.setVisible( true );
    		
    	}
    }
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    Add this at the top of your paintComponent override:
    Java Code:
    super.paintComponent(g);
    Recommended reading:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI with JFC/Swing)

    db

  3. #3
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Thanks for the response, I added the call to superclass at the top of the paintcomponent method as such:

    Java Code:
        protected void paintComponent( Graphics g )
        {
            super.paintComponent( g );
            if (image != null)
                g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
        }
    But still get nothing but my background when I try running it. It's not the background I'm having trouble seeing, it's the JButtons and the JLabels I'm not seeing.
    Last edited by v1nsai; 08-23-2009 at 11:38 PM.
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Don't do this
    Java Code:
    catch (Exception e) { /*handled in paintComponent()*/ }
    The potential exception is neither displayed nor handled. It's just hidden.

    You are probably getting a MalformedURLException on the line with
    Java Code:
    image = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("bgImage.jpg"), "bgImage.jpg"));
    You also seem to be creating some Icons that you do nothing with.

  5. #5
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Does one ever do anything with Icons? They're just there for display. The left and right arrow icons are declared so that they can be plugged into the one that is being displayed, I only need one at a time.

    The try and catch aren't my code, I found it and modified it for my use so I'm not completely clear on what it does. Is there an easier way to get the backgrounded painted on the screen? Class Image objects can't be instantiated directly, I've definitely been having some trouble getting the background image set.

    Thanks for the suggestion though, I took the try and catch lines out and just left the 'image = ' line and sure enough there was a malformedURL caught by the compiler. Is this what's keeping everything else besides the background from being displayed?
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The main reason nothing is displayed is because you create your interface in a prompt method which you never call.
    Last edited by r035198x; 08-24-2009 at 08:42 PM.

  7. #7
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    I can't just have a button sit there? I want to get everything organized and looking the way I want before I start adding event handles, I can't do that?
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    We are no longer getting each other here. In my last post I talked about the method called prompt. You never call it but that is where you create the JLabels and add them to the JPanel. You need to call a method for the method's code to execute.
    I suggest going through Sun's Java tutorial to understand the basics of Java before trying to write swing programs.

  9. #9
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Aha, I see. I didn't understand what you said the first time. Nothing is displayed because I never call prompt() method.

    You confused me with calling it an interface, I thought you were referring to the buttons and the lack of a listener interface being attached to them. Everything displays now, awesome, thank you.

    And what were you getting at with the try statement? That was working perfectly fine, as I stated.
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    About the exception thing, just read my reply #4 again. try/catch should be used for exception handling not for exception hiding.

Similar Threads

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM
  2. Display output in Jframe or JPanel
    By rammurugesan in forum AWT / Swing
    Replies: 12
    Last Post: 04-14-2009, 01:45 PM
  3. How to display image from byte array in JPANEL
    By waqasdaar in forum AWT / Swing
    Replies: 0
    Last Post: 03-22-2009, 12:11 AM
  4. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  5. Replies: 0
    Last Post: 06-28-2008, 09:26 PM

Posting Permissions

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