Woof
Woof

Reputation: 1

When Loading Image Using BufferdImage and ImageIO it Clears my Whole Frame and Does Not Display Image

I and making a program using basic GUI involving buttons, frames, and panels, and everything was fine until I tried to load an image from my project folder. When i add the line of code

   try{
         titleImage = ImageIO.read(new File("mouse_title_resize.png"));
       } 
    catch(Exception e){}

After I run the program my whole frame just becomes blank whereas before I had some JButtons on it.All the code I had before the try-catch line worked perfectly fine and I tested to see that the only thing that breaks it is this line of code. I receive no errors or anything and I have the image in my project folder and it seems that the image loaded fine, except it wont show up on the frame, and everything else on the frame disappears. I just don't understand why it clears my whole frame when i load the image.

Here is the full code:

This is the class that extends JFrame

package mouse.click.game;

import java.awt.Dimension;

import java.awt.Point;

import java.awt.Toolkit;

import javax.swing.JFrame;

public class MouseClickGame extends JFrame {

//Constants to define the frame width and height including borders
public final int FRAME_WIDTH = 600;
public final int FRAME_HEIGHT = 600;
//Dimension from Toolkit to be able to get width and height of screen
public Dimension sizeTool = Toolkit.getDefaultToolkit().getScreenSize();
//Using sizeTool to get width of screen
public double xResolution = sizeTool.getWidth();
//Using sizeTool to get height of screen
public double yResolution = sizeTool.getHeight();
//Creating a point object that is defined as the center of the screen
public Point middleOfScreen = new Point((int) (xResolution / 2) - (FRAME_WIDTH / 2), (int) (yResolution / 2) - (FRAME_HEIGHT / 2));

public MouseClickGame() {
    super("WELCOME :D");
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLocation(middleOfScreen);
    add(new MouseClickPanel());

}

public static void main(String[] args) {

    //Calling constructor
    MouseClickGame mainClickGame = new MouseClickGame();

}

}

And here is the class that extends JPanel (these are the only two classes in my project)

package mouse.click.game;

import java.awt.Dimension;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class MouseClickPanel extends JPanel {

JButton buttonPlay = new JButton("Play");
JButton buttonContinue = new JButton("Continue");
JButton buttonOptions = new JButton("Options");
JButton buttonExit = new JButton("Exit");

BoxLayout boxLay = new BoxLayout(this, BoxLayout.Y_AXIS);

Dimension menuButtonSize = new Dimension(300, 30);
Dimension spacingBetweenButtons = new Dimension(0, 30);

BufferedImage titleImage;

public MouseClickPanel() {

    try {
        titleImage = ImageIO.read(new File("C:\\Users\\Justin\\Desktop\\mouse_title.png"));
    } catch (IOException e) {

    }

    setLayout(boxLay);
    add(Box.createVerticalGlue());
    add(new JLabel(new ImageIcon(titleImage)));

    //Adding glue to force buttons away from top of panel
    add(Box.createVerticalGlue());
    add(buttonPlay);
    //Vertical spacing between buttons
    add(Box.createRigidArea(spacingBetweenButtons));
    add(buttonContinue);

    add(Box.createRigidArea(spacingBetweenButtons));
    add(buttonOptions);

    add(Box.createRigidArea(spacingBetweenButtons));
    add(buttonExit);
    //Adding glue to force buttons away from bottom of panel
    add(Box.createVerticalGlue());

    //Aligning all buttons to centered horizontally
    buttonPlay.setAlignmentX(Box.CENTER_ALIGNMENT);
    buttonContinue.setAlignmentX(Box.CENTER_ALIGNMENT);
    buttonOptions.setAlignmentX(Box.CENTER_ALIGNMENT);
    buttonExit.setAlignmentX(Box.CENTER_ALIGNMENT);

    //Setting button sizes
    buttonPlay.setMaximumSize(menuButtonSize);
    buttonContinue.setMaximumSize(menuButtonSize);
    buttonOptions.setMaximumSize(menuButtonSize);
    buttonExit.setMaximumSize(menuButtonSize);

}

}

Literally if i get ride of the titleImage = and add(new JLabel) lines everything goes back to normal

Upvotes: 0

Views: 188

Answers (2)

Woof
Woof

Reputation: 1

UPDATE: Wow literally the reason it wasn't showing up was because i called setVisble(true) too early. I can't believe it was something that simple. I guess that explains why one time everything would show up and it would be fine but then every time after nothing showed up. So in my constructor I had

public class MouseClickGame extends JFrame {

public MouseClickGame() {
    super("WELCOME :D");
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(middleOfScreen);
    setVisible(true);
    add(new MouseClickPanel());


}

public static void main(String[] args) {

    //Calling constructor
    MouseClickGame mainClickGame = new MouseClickGame();

}

}

when all I had to do was put the setVisble() after the add(newMouseClickPanel())

Thank you DavidS for your suggestions :D

Upvotes: 0

user201891
user201891

Reputation:

My guess is that you've just got the path wrong -- a common mistake. In that case, you should be getting an exception like:

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at jonathanrmiproject.MyProject.main(JonathanRmiProject.java:24)

This simple example works for me:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.naming.NamingException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyProject {
    public static void main(String[] args) throws NamingException, IOException {
        BufferedImage img = ImageIO.read(new File("myimage.jpg"));
        JLabel label = new JLabel(new ImageIcon(img));
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(label);
        f.pack();
        f.setLocation(200, 200);
        f.setVisible(true);
    }
}

I am using Netbeans IDE, and I have saved the image file to "C:\Users\David.Sharpe\MyProject\myimage.jpg".

I should add that if this is not the case, and you do have the correct path, then you need to post a more detailed question so that someone can help you. Include the code to reproduce the problem.

Upvotes: 0

Related Questions