Results 1 to 5 of 5
  1. #1
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Question JFrame background help?

    Hi im trying to create a game, ive started by creating a JFrame and i understand that much. It works till so far but now ive been trying to get a background from a PNG file which i created on paint.
    This is my code so far:
    Java Code:
    package meteorz;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    
    /**
     * @author Bimz
     */
    public class Meteorz extends JFrame{
    
        Image Background;
        
        public Meteorz(){
            
            setTitle("Meteorz - Bimz");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(800, 600);
            setLocation(100, 60);
            setResizable(false);
            
            setVisible(true);
            
            ImageIcon ii = new ImageIcon(this.getClass().getResource("images/background.png"));
            Background = ii.getImage();
        }
        
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(Background, 0, 0, null);
        }
        
        public static void main(String[] args) {
            new Meteorz();
        }
    }
    there are no errors before i run this, but once the program starts rather than my picture appearing the screen is white. please help, Bimz :)

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    You should never override the paint() method of a JFrame (unless you know what you are doing).

    Custom painting is done by override the paintComponent() method of a JPanel or a JComponent. Then you add the panel to the frame. Also, it helps if you read the image before showing the frame.

  3. #3
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default

    so should i create a seperate JLabel, put my image in the JLabel then add the Label to my JFrame?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Try it and see what happens.

  5. #5
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

Similar Threads

  1. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 06:55 PM
  2. JFrame image background
    By 851marc in forum NetBeans
    Replies: 5
    Last Post: 03-09-2010, 06:27 PM
  3. How to add a background image to JFrame
    By dunafrothint in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2010, 10:17 PM
  4. Need help with JFrame background image
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 04:17 PM
  5. Background in JFrame ( GUI).
    By Twister03 in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2009, 03:24 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
  •