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

Thread: A Background Image

  1. #1
    Junior Member Randor's Avatar
    Join Date
    Nov 2012
    Location
    Olean, NY
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default A Background Image

    Hello,

    Trying to get my background image to show up but it is not, can anyone see what I may be doing wrong? The image is in the right spot and all is spelled correctly on the path and image.

    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import javax.swing.*;
     
     
    public class Bongo extends JFrame{
     
        public static JFrame MainWindow = new JFrame();
     
        public static JFrame LogInWindow = new JFrame();
        public static JTextField TF_USERNAMEBOX = new JTextField(20);
        private static JButton B_ENTER = new JButton("ENTER");
        private static JLabel L_ENTERUSERNAME = new JLabel();
        private static JPanel P_LOGIN = new JPanel();
        private static JPanel panel = new JPanel();
     
        private static Image im = Toolkit.getDefaultToolkit().getImage("C:\\OtherStuff\\BongoBack.png");
        private static ImageIcon BG = new ImageIcon(im);
     
        public static void main(String[] args) {
     
            BuildMainWindow();
            BuildLogInWindow();
     
            Bongo b = new Bongo();
     
        }
     
        public static void BuildMainWindow() {
     
    		MainWindow.setTitle("Bongo");
    		MainWindow.setSize(1000,700);
    		MainWindow.setLocation(150,100);
    		MainWindow.setResizable(false);
                    MainWindow.setVisible(true);           
     
        }
     
        public static void BuildLogInWindow() {
     
                LogInWindow.setTitle("Whats Your Name?");
                LogInWindow.setSize(400,100);
                LogInWindow.setLocation(250,200);
                LogInWindow.setResizable(false);
                P_LOGIN = new JPanel();
                P_LOGIN.add(L_ENTERUSERNAME);
                P_LOGIN.add(TF_USERNAMEBOX);
                P_LOGIN.add(B_ENTER);
                LogInWindow.add(P_LOGIN);
     
               // Login_Action();
                LogInWindow.setVisible(true);
     
        }
     
        public void paint(Graphics g) {  
     
            g.drawImage(im,0,0,this);  
     
        }     
     
        }

    Thank you in advance..
    When I die, I want it to be peacful and in my sleep, like my grandfather. Not flailing and screaming like the people in his car!!


  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: A Background Image

    You have 3 JFrames here. Which JFrame are you trying to add the image to?
    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. #3
    Junior Member Randor's Avatar
    Join Date
    Nov 2012
    Location
    Olean, NY
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A Background Image

    MainWindow
    When I die, I want it to be peacful and in my sleep, like my grandfather. Not flailing and screaming like the people in his car!!

  4. #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: A Background Image

    Okay, and where is the code for displaying a background image in MainWindow? (which should be mainWindow according to the standard naming conventions, by the way)
    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!

  5. #5
    Junior Member Randor's Avatar
    Join Date
    Nov 2012
    Location
    Olean, NY
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: A Background Image

    Well, I created the image with this:

    private static Image im = Toolkit.getDefaultToolkit().getImage("C:\\OtherStuff\\BingoBack.png");

    Then, what I THOUGHT I was trying to do was call to the paint method which would put it on the screen at x=0,y=0 from here:

        public void paint(Graphics g) {  
     
            g.drawImage(im,0,0,this);  
     
        }

    But apperently I am not correct on my assumptions somewhere..

    I want to be able to put all my images on the screen by x and y coordinates, so this is why I was trying to use paint method as appose to panels.

    As for Mainmain.. I know common prcatice is to do mainWindow but since I do not see myself getting any programming jobs with Sun anytime soon, I find it a bit easier to for me read when i captiolize all first letters...
    When I die, I want it to be peacful and in my sleep, like my grandfather. Not flailing and screaming like the people in his car!!

  6. #6
    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: A Background Image

    Quote Originally Posted by Randor View Post
    As for Mainmain.. I know common prcatice is to do mainWindow but since I do not see myself getting any programming jobs with Sun anytime soon, I find it a bit easier to for me read when i captiolize all first letters...
    That might be easier for you to read, but it's much harder for us to read. It might seem like a small thing, but it goes a long way to making it easier for us to help you.

    Anyway, you're extending JFrame and painting an image on it, but you never actually show that JFrame. You show some other JFrames that have the default behavior (no image) instead.

    I would suggest you extend JPanel and override paintComponent() instead. Then just use that type of JPanel as the content pane of any JFrames you want to have a background 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!

  7. #7
    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: A Background Image

    I do not see myself getting any programming jobs with Sun anytime soon
    I agree. Unfortunately Sun is not the only entity who think following the standard is a good idea. But good luck where ever you get hired.

    I find it a bit easier to for me read when i captiolize all first letters...
    Standards are developed so it is easier for everyone to read. That is the whole idea. Here is my argument:
    1) You are the fly in the buttermilk. Not that buttermilk is good or bad to begin with, but definately not good with a fly in it. You cause problems for the group as a whole with your substandard methods.
    2) The author of the code is almost never the one to maintain the code for the life of the code. That means 2 things. First others will see your code, and second you will see others code. Which is why there is a standard.
    3) The fact that most code (at least mostly) follows the standard, you will eventually get used to it. You might as well make it easy on yourself from the start. The longer you wait the harder it will be.
    4) Most entities that hire people to write code fully expect them to adhere to the standard and produce code that is easy for 'everyone' to read. Sun just happens to be one of them.
    5) I speak from experience. I hated java and the java ways for so long. I even refused to learn java for a very long time. I am old, but even more I am stubborn. Bleh, life is happier without the war.
    So put down the weapons, throw in the towel, and join the rest of the crowd with a cup of java, some tea perhaps.


    See "How to make frames" and try to boil your code down to 1 reusable frame. Not that you have to but you should. That is what object oriented programming is about. You don't need 400 frames if you make one frame that can be adapted those 400 ways.


    I would suggest you extend JPanel and override paintComponent() instead. Then just use that type of JPanel as the content pane of any JFrames you want to have a background image.
    That is some golden advice there.

Similar Threads

  1. Replies: 1
    Last Post: August 4th, 2012, 10:02 AM
  2. adding background image to a frame???
    By overdriveboy in forum AWT / Java Swing
    Replies: 0
    Last Post: May 13th, 2012, 01:23 PM
  3. Cant get background image to take up whole panel
    By Lanuk in forum AWT / Java Swing
    Replies: 1
    Last Post: December 13th, 2011, 08:26 AM
  4. 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
  5. Background image on GUI
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 3
    Last Post: March 5th, 2010, 12:10 PM