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 3 of 3

Thread: How do i set a background image to an extended (2 class) JFrame program?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do i set a background image to an extended (2 class) JFrame program?

    I need some help to make an image a background for a JFrame.
    I tried researching and i found some ways like using the graphics g but i am not sure what class i place it in.
    thanks in advance!

    here is the first class of my code.

    ------------------------------------------
    package classes;
     
    import java.awt.Color;
    import java.awt.Component;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.BoxLayout;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
     
    public class rohwcg extends JFrame
    {
     
    // adds the buttons
    private JButton minerbutton;
    private JButton farmerbutton;
    private JButton lumberjackbutton;
    private JButton blacksmithbutton;
     
     
    public rohwcg()
    {
        super ("Realms of Havenwood Class Guide");
        this.getContentPane().setBackground(Color.black);
        this.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
     
     
     
     
     
        Icon mbutton = new ImageIcon (getClass() .getResource("miner.png"));
        minerbutton = new JButton(mbutton);
        add(minerbutton);
     
        //farmer button
        Icon fbutton = new ImageIcon (getClass() .getResource("farmer.png"));
        farmerbutton = new JButton(fbutton);
        add(farmerbutton);
     
        //lumberJack button
        Icon lbutton = new ImageIcon (getClass() .getResource("lumberjack.png"));
        lumberjackbutton = new JButton(lbutton);
        add(lumberjackbutton);
     
        //blacksmith button
        Icon bbutton = new ImageIcon (getClass() .getResource("blacksmith.png"));
        blacksmithbutton = new JButton(bbutton);
        add(blacksmithbutton);

    I didn't paste all of it because it wasn't needed.
    --------------------------------------------------
    2nd class
    --------------------------------------------------
    package classes;
     
    import java.awt.Dimension;
    import java.awt.Toolkit;
     
     
    import javax.swing.JFrame;
     
     
    public class thehandler {
    public static void main(String args []) 
    {
        rohwcg classes1 = new rohwcg();
        classes1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        classes1.setSize(700,300);
        classes1.setVisible(true);
     
     
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        int x = d.width / 2;
        int y = (d.height / 2 ) - classes1.getHeight();
        classes1.setLocation(x,y);
    }
     
    }

    i imported Jframe its just hidden.


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do i set a background image to an extended (2 class) JFrame program?

    i am also having the same problem

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do i set a background image to an extended (2 class) JFrame program?

    Your favorite search engine will offer plenty of information on your problem. This is not quite the place for such a lengthy set of instructions. Not to mention there are many ways to do it, and no one way is the always the best way to go. It depends on the overall use etc.

Similar Threads

  1. Replies: 1
    Last Post: August 4th, 2012, 10:02 AM
  2. Creating an open-ended class that can be extended:
    By dedshaw1612 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 9th, 2012, 08:34 AM
  3. Need help correcting a JFrame program for class!
    By AlexAndAHalf in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 23rd, 2011, 03:03 AM
  4. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 24th, 2010, 11:15 PM
  5. Imitating a JFrame extended program with JPanel; help needed...
    By emigrant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 01:30 PM

Tags for this Thread