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

Thread: Setting a background image on a gameFrame

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Setting a background image on a gameFrame

    I'm trying to make minesweeper and a struggling trying to make a background image for my JFrame. I think the program breaks when setContentPane(screen) is run.


     
     
     
    //pakke der styrer farver
    import java.awt.Color;
    import javax.swing.*;
    //Pakke der hjælper med at lave vinduet
    import javax.swing.JFrame;
    import java.awt.*;
     
    import java.awt.Font;
    import java.awt.FontFormatException;
     
    import java.io.File;
    import java.io.IOException;
    import java.awt.event.*;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.awt.GraphicsEnvironment;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
     
    public class GameFrame extends JFrame {
        // CENTER regions størrelse
        static final int SCREEN_WIDTH = 1100;
        static final int SCREEN_HEIGHT = 800;
        Font technology;
     
        // De forskellige JPanels
        JPanel topMenu;
        JPanel screen;
        JLabel mineCounter;
        JLabel timer;
        JButton btnNewButton;
     
        GameFrame() {
     
            try {
                setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("Images/background.png")))));
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            pack();
     
            try {
                // load a custom font in your project folder
                technology = Font.createFont(Font.TRUETYPE_FONT, new File("technology.regular.ttf")).deriveFont(100f);
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("technology.regular.ttf")));
            } catch (IOException | FontFormatException e) {
     
            }
     
            screen = new JPanel();
            screen.setLayout(new BorderLayout());
     
            // adding top menu
            addTopMenu();
            // adding game
            screen.add(new GamePanel(), BorderLayout.CENTER);
            // adding side menu
     
            //add(screen);
     
            setContentPane(screen);
     
            // Stopper programmet når vinduet lukkes
     
     
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            // Vinduets størrelse
            // setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
     
            // Vinduets baggrundsfarve
            //this.setBackground(Color.BLACK);
     
            // Viser vinduet
            //setVisible(true);
     
     
            setVisible(true);
     
            // Gør at man ikke kan ændre størrelse på vinduet
            setResizable(false);
     
        }
     
        // Indeling af kode. Den del af koden som laver top menuen.
        void addTopMenu() {
            topMenu = new JPanel();
            topMenu.setLayout(new BoxLayout(topMenu, BoxLayout.X_AXIS));
     
            topMenu.setBackground(Color.gray);
     
            mineCounter = new JLabel(Mine.remaningMines + "");
            mineCounter.setBackground(new Color(192, 192, 192));
            mineCounter.setForeground(Color.red);
            mineCounter.setFont(technology);
            topMenu.add(mineCounter);
     
            topMenu.add(Box.createRigidArea(new Dimension(390, 0)));
     
            btnNewButton = new JButton();
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    GamePanel.newGame();
                }
            });
     
            String iconfilePath = this.getClass().getClassLoader().getResource("Images/Defualt.png").getFile();
            btnNewButton.setIcon(new ImageIcon(iconfilePath));
            btnNewButton.setBorder(BorderFactory.createEmptyBorder());
            btnNewButton.setContentAreaFilled(false);
            btnNewButton.setFocusable(false);
            topMenu.add(btnNewButton);
     
            topMenu.add(Box.createRigidArea(new Dimension(390, 0)));
     
            timer = new JLabel(GamePanel.time + "00");
            timer.setBackground(new Color(192, 192, 192));
            timer.setForeground(Color.red);
            timer.setFont(technology);
            topMenu.add(timer);
            screen.add(topMenu, BorderLayout.NORTH);
     
        }
     
        public void changeIcon(String imagePath) {
            String iconfilePath = this.getClass().getClassLoader().getResource(imagePath).getFile();
            btnNewButton.setIcon(new ImageIcon(iconfilePath));
     
        }
     
    }
    Last edited by luckduck312; August 15th, 2022 at 10:11 AM. Reason: New information from testing

  2. #2
    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: Setting a background image on a gameFrame

    program breaks when
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Setting a background image on a gameFrame

    I dont get an error the background just does not seem to load. If i remove the line setContentPane Only the background is shown else everything but the background is show.

  4. #4
    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: Setting a background image on a gameFrame

    There are missing classes and no main method -> the code can not be compiled and executed for testing.

    Please make and post a small, complete program that compiles and executes for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting a background image on a gameFrame

    Quote Originally Posted by Norm View Post
    There are missing classes and no main method -> the code can not be compiled and executed for testing.

    Please make and post a small, complete program that compiles and executes for testing.


    Here is a small test program. What I want is a background that other components can be painten on top off (JLabels, canvas...) This backgroundimage should be the size of the image so its bassicly works instead of using setBackground();

     
     
     
    //pakke der styrer farver
    import java.awt.Color;
    import javax.swing.*;
    //Pakke der hjælper med at lave vinduet
    import javax.swing.JFrame;
    import java.awt.*;
     
    import java.awt.Font;
     
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    import javax.swing.ImageIcon;
     
    public class GameFrame extends JFrame {
        // CENTER regions størrelse
        static final int SCREEN_WIDTH = 1100;
        static final int SCREEN_HEIGHT = 800;
        Font technology;
     
        // De forskellige JPanels
        JPanel topMenu;
        JPanel screen;
        JLabel mineCounter;
        JLabel timer;
        JButton btnNewButton;
        public static GameFrame frame;
     
        GameFrame() {
     
            screen = new JPanel();
            screen.setLayout(new BorderLayout());
            setContentPane(screen);
     
            addTopMenu();
     
            setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
     
     
        }
     
     
        public static void main(String[] args) {
     
            frame = new GameFrame();
        }
     
     
     
     
     
        // Indeling af kode. Den del af koden som laver top menuen.
        void addTopMenu() {
            topMenu = new JPanel();
            topMenu.setLayout(new BoxLayout(topMenu, BoxLayout.X_AXIS));
     
            topMenu.setBackground(Color.gray);
     
            mineCounter = new JLabel("dasf");
            mineCounter.setBackground(new Color(192, 192, 192));
            mineCounter.setForeground(Color.red);
            mineCounter.setFont(technology);
            topMenu.add(mineCounter);
     
            topMenu.add(Box.createRigidArea(new Dimension(390, 0)));
     
     
            topMenu.add(Box.createRigidArea(new Dimension(390, 0)));
     
     
            timer = new JLabel( "00");
            timer.setBackground(new Color(192, 192, 192));
            timer.setForeground(Color.red);
            timer.setFont(technology);
            topMenu.add(timer);
            screen.add(topMenu, BorderLayout.NORTH);
     
        }
     
        public void changeIcon(String imagePath) {
            String iconfilePath = this.getClass().getClassLoader().getResource(imagePath).getFile();
            btnNewButton.setIcon(new ImageIcon(iconfilePath));
     
        }
     
    }

  6. #6
    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: Setting a background image on a gameFrame

    Have you tried asking with Google? Here is one link I got: https://java-demos.blogspot.com/2012...in-jframe.html
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting a background image on a gameFrame

    I found another method that gives me the correct result. Thanks for alle the help

  8. #8
    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: Setting a background image on a gameFrame

    Can you post the working code here for others to see?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] JButton Image does not show. on background image.
    By chengnetz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2014, 12:06 PM
  2. Setting background color to translucent?!?!
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 23rd, 2013, 07:58 AM
  3. A Background Image
    By Randor in forum AWT / Java Swing
    Replies: 6
    Last Post: November 14th, 2012, 05:28 PM
  4. Replies: 1
    Last Post: August 4th, 2012, 10:02 AM
  5. Replies: 3
    Last Post: July 17th, 2012, 11:59 PM