Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Adding components to my GUI over a background image

  1. #1
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Adding components to my GUI over a background image

    So here is my frame with the background image i want and now i am just having a bit of trouble adding labels on top of it. Do i need to use LayeredPane to add the labels(the part about this tool will..., your atk/matk, your element, etc are all labels) on top of the background?

    also is there a layout manager i could use to place the components like this or would i have to use absolute positioning?

    any help and suggestions are very much appreciated thanks very much


    Form_mobile_2.jpg


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding components to my GUI over a background image

    How are you doing the background image? And what layouts have you tried? Check out your options here: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) and keep in mind that you can nest layouts.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    derekxec (August 18th, 2011)

  4. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Adding components to my GUI over a background image

    i just have a basic Jframe right now and i used this to add the image to it

    	public void paint(Graphics g) {
    		Image bg = Toolkit.getDefaultToolkit().getImage("bg.png");
    		g.drawImage(bg,0,0,getSize().width,getSize().height,this);
    		super.paint(g);
    	}

    and when i add a button to the frame and use null layout and i use absolute positioning the button is good. when i try to do the same adding a label the label doesnt appear

    import javax.swing.*;
    import java.awt.*;
     
    public class fdmgcalc extends JPanel {
    	private static final long serialVersionUID = 0L;
     
     
     
    	public fdmgcalc() {
    		setOpaque(false);
    		setLayout(null);
    	}
     
    	public static void main(String[] args) {
     
    		JFrame mFrame = new JFrame("Smack The Pet 1.0");
    		fdmgcalc c = new fdmgcalc();
    		mFrame.add(c);
    		mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		mFrame.setSize(400, 500);
    		mFrame.setVisible(true);
    	}
     
    	public void paint(Graphics g) {
    		Image bg = Toolkit.getDefaultToolkit().getImage("bg.png");
    		g.drawImage(bg,0,0,getSize().width,getSize().height,this);
    		super.paint(g);
    	}
     
    }

    this is the whole code so far..just messing around with making the frame with bg image and then adding stuff on top of it

    the program i made it has everything but the bg image so before i try to put it in there i figured i should mess with it some instead of screwing all up haha ill read that layout manager guide. any other stuff i should look at that might help too?

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding components to my GUI over a background image

    That's because you're adding a JPanel, not a JLabel. Unless you mean something else?

    And you probably don't want to load the image each time your JFrame is repainted. Load it once and store it in a variable, then just use the already loaded image.

    ...and why is your super.paint() happening after you paint the image? Doesn't that just clear the image?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Adding components to my GUI over a background image

    ah i took out the label stuff cause it kept removing the bg when it did go on the frame

    when would it get repainted? i thought once its up there thats it

    and i thought the super goes after lol guess i need to reread how to paint lol this is becoming harder than i originally thought

    Edit: ah what does the super part do? cause i removed it and it still puts the image on the bg...i also stored the image in a variable like you said

    edit again: i tried something different now for the label i used setBounds to move it around and put it before i made the window opaque and now it sits on top of the bg
    Last edited by derekxec; August 18th, 2011 at 06:17 PM.

  7. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Adding components to my GUI over a background image

    it does disappear

    when i run it in eclipse everything looks perfect but when i make it into a jar the bg disappears but everything else is good why does the bg disappear

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding components to my GUI over a background image

    Well, you've got a few problems here. I recommend you read through this: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Also, does your Jar have access to that file? What is being returned by your getImage() method?

    Read that tutorial, and figure out the file access issue, and post an updated SSCCE with your most recent code (after you read that tutorial and fix anything that you see in it), and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #8
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Adding components to my GUI over a background image

    thanks very much ill read through that right now and get to fixing

    and you know everything lol every time i post a question you are always the one answering first i very much appreciate it

  10. #9
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Adding components to my GUI over a background image

    finally got the image working i did like this

    	public void paintComponent(Graphics mFrame) {
    		super.paintComponent(mFrame);
    		URL bg = fdmgcalc.class.getResource("bg.png");
    		Image bgpic = Toolkit.getDefaultToolkit().getImage(bg);
    		mFrame.drawImage(bgpic,0,0,getSize().width,getSize().height,this);
    	}

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding components to my GUI over a background image

    You should move this code outside of the paint method so you only have to load the image one time.
                    URL bg = fdmgcalc.class.getResource("bg.png");
    		Image bgpic = Toolkit.getDefaultToolkit().getImage(bg);

  12. #11
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Adding components to my GUI over a background image

    thanks norm i did that now and i read through that paint guide and figured i need a panel so i added that and works good

    i also took everything out of the main() and put it its own methods

    now i just have 1 more question...how come you need a panel instead of painting on the jframe?

    package smackthepet;
     
    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
     
    public class MyPanel extends JPanel{
    	private static final long serialVersionUID = 1L;
     
    	public Image bgpic;
     
    	public MyPanel() {
    		setBorder(BorderFactory.createLineBorder(Color.black));
    		URL bg = Msmack.class.getResource("bg.png");
    		bgpic = Toolkit.getDefaultToolkit().getImage(bg);
     
    		JLabel heading = new JLabel("Smack The Pet 1.0");
    		heading.setFont(new Font("Serif", Font.PLAIN, 32));
    		Dimension headingSize = heading.getPreferredSize();
    		heading.setBounds(79,22,headingSize.width,headingSize.height);
    		add(heading);
    	}
    	public Dimension getPreferredSize() {
    		return new Dimension(410, 510);
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.drawImage(bgpic,0,0,getSize().width,getSize().height,this);
    	}
     
    }


    package smackthepet;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class Msmack {
     
    	public Msmack() {
     
    	}
     
    	public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
     
    	}
     
    	private static void createAndShowGUI() {
    		JFrame mFrame = new JFrame("Smack The Pet 1.0");
    		mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		mFrame.add(new MyPanel());
    		mFrame.pack();
    		mFrame.setResizable(false);
    		mFrame.setVisible(true);
    	}
     
    }

    so this is how i have it now anything i should do differently? and thanks for all the help everyone
    Last edited by derekxec; August 22nd, 2011 at 10:41 AM.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding components to my GUI over a background image

    a panel instead of painting on the jframe?
    I think it is because JPanel extends JComponent which has useful Swing methods and features.
    JFrame extends Frame which doesn't.

  14. #13
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Adding components to my GUI over a background image

    sounds pretty good to me thanks

Similar Threads

  1. Background image trouble on GUI.
    By Pydra in forum AWT / Java Swing
    Replies: 2
    Last Post: July 7th, 2011, 11:44 AM
  2. Need help adding background image (noob here)
    By OpX316 in forum AWT / Java Swing
    Replies: 18
    Last Post: July 7th, 2011, 09:10 AM
  3. Is it possible to make a background to in image transparent
    By Zein in forum Java Theory & Questions
    Replies: 10
    Last Post: June 13th, 2011, 04:18 AM
  4. background
    By b109 in forum AWT / Java Swing
    Replies: 0
    Last Post: May 24th, 2010, 06:37 AM
  5. Background image on GUI
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 3
    Last Post: March 5th, 2010, 12:10 PM