-1

I am trying to put am image for the background. When I run my code, a screen with nothing shows up and another screen with 2 buttons and the yellow background. How do I replace the yellow and put in the image that I want??

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GameFrame extends JFrame {

    public BufferedImage image;
    public JFrame gameframe;
    public JPanel panel;
    public JButton b1;
    public JButton b2;

    public JLabel lab;

    public GameFrame() {
        //SETTING GAMEFRAME
        gameframe = new JFrame("Poop MAN");

        gameframe.setVisible(true);
        gameframe.setSize(1350, 1000);
        gameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();

        panel.setBackground(Color.yellow);

        //BUTTONS
        panel.setLayout(null);
        b1 = new JButton("CONTROLS");
        b2 = new JButton("START");
        b1.setBounds(500, 200, 150, 150);
        b2.setBounds(500, 400, 150, 150);

        b1.setPreferredSize(new Dimension(50, 50));
        b2.setPreferredSize(new Dimension(30, 50));

        panel.add(b1, BorderLayout.SOUTH);
        panel.add(b2, BorderLayout.SOUTH);

        gameframe.add(panel);
        gameframe.repaint();

    }

    public void ImagePanel() {
        try {
            image = ImageIO.read(new File("unnamed"));
        } catch (IOException ex) {

        }
    }

    public BufferedImage getImg() {
        return image;
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Hyunseok Song
  • 21
  • 1
  • 6
  • google it, really helps – Lrrr Apr 29 '15 at 05:28
  • 5
    You mean something like [this](http://stackoverflow.com/questions/13791984/add-an-background-image-to-a-panel/13792503#13792503) or [this](http://stackoverflow.com/questions/13944795/trouble-figuring-out-how-to-set-background-image/13945145#13945145) or [this](http://stackoverflow.com/questions/12253979/place-jlabel-on-top-of-jlabel-with-image-in/12254063#12254063) or [this](http://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430)? – MadProgrammer Apr 29 '15 at 05:29
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Apr 29 '15 at 05:29
  • I did. But everybody has different ways and I don't know where to put in the codes... – Hyunseok Song Apr 29 '15 at 05:29
  • Simply put, the "best" way would be to create a custom component, extending from something like `JPanel` and paint the image via the panel's `paintComponent` method. – MadProgrammer Apr 29 '15 at 05:30
  • what does that mean? – Hyunseok Song Apr 29 '15 at 05:34
  • 1
    See the four linked examples? Take a look to see how it's done. If you're unfamiliar with extending classes, overriding methods or custom painting, I would suggest taking the time to learn them before embarking on making a game, you will need to understand these terms and how to apply them. You may find [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html), [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) and [2D Graphics](http://docs.oracle.com/javase/tutorial/2d/) helpful – MadProgrammer Apr 29 '15 at 05:35
  • I have to create a game for my computer science class at school.... My teacher wants us to figure it by ourselves an i m struggling so bad :( – Hyunseok Song Apr 29 '15 at 05:39
  • You also have two potential windows that your program can create `GameFrame extends JFrame` and `public JFrame gameframe;`, do you know which one is actually getting onto the screen and which one you're adding components to...? – MadProgrammer Apr 29 '15 at 05:39
  • 1
    In that case [Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) might be a good read, as well as [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/), [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html), [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) and [How to Write a Mouse Listener](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html). Start with small ideas and figure out how to make those work, then incorporate them together – MadProgrammer Apr 29 '15 at 05:41
  • GameFrame extends JFrame creates the yellow screen with 2 buttons and there is other screen showing up with nothing. I don't understand what you are saying... – Hyunseok Song Apr 29 '15 at 05:43
  • 1
    Get rid of `extends JFrame`, it's complicating your code and adds no real value – MadProgrammer Apr 29 '15 at 05:44
  • i have a main class and isnt that why i need extends jframe? – Hyunseok Song Apr 29 '15 at 05:51
  • Nope, you're creating an instance of `JFrame` in the `GameFrame` class, the extra reference to `JFrame` is just confusing, you're not using it nor are you adding any value to the class – MadProgrammer Apr 29 '15 at 05:59

1 Answers1

-1

Its not so straight forward to make it look nice. Take a look at Background Panel https://tips4java.wordpress.com/2008/10/12/background-panel/

keuleJ
  • 3,418
  • 4
  • 30
  • 51