I am new to Java.
My Problem is that my Java program cannot find the image file im trying to use as
a JButton. (Currently this code does nothing, as I am simply getting a desirable look
first).
THIS IS MY MAIN CLASS
Code:
package mainClasses;
/*
* Frame Info and all that shit,
* mainFrame is the actual frame itself
* it will refer to MainC.java a lot Main class = Main Class
*/
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.io.File;
import resources.ResourcesManager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
@SuppressWarnings({ "unused", "serial" })
public class mainFrame extends JFrame {
ResourcesManager rManager = new ResourcesManager();
public mainFrame() {
JButton playButton = new JButton(rManager.pButton);
JButton infoButton = new JButton();
JButton exitButton = new JButton();
int x = 310, y = 300;
setSize(x, y);
setVisible(true);
setLayout(null);
setTitle("Kingdom Raider");
setDefaultCloseOperation(EXIT_ON_CLOSE);
/*Buttons and Properties*/
/*X, Y, X, Y*/
playButton.setBounds(10, 10, 70, 40);
/* Add if problem cannot be sorted */ // playButton.setText("Play");
add(playButton);
infoButton.setBounds(90, 10, 110, 40);
infoButton.setText("Information");
add(infoButton);
exitButton.setBounds(210, 10, 70, 40);
exitButton.setText("Exit");
add(exitButton);
//This is for checking if the file is here.
File imageCheck = new File("/JavaGame/src/resources/playButton.png");
if(imageCheck.exists())
{
System.out.println("File found!");
}
else
{
System.out.println("File not found!");
}
repaint();
}
public void Painting (Graphics g) {
}
}
THIS IS MY RESOURCESMANAGER.JAVA
package resources;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class ResourcesManager {
/*Here, your going to want to declare anything
* needed.
*/
public ImageIcon KRLogo = new ImageIcon("/JavaGame/src/resources/kingdomraiderlogo.png");
public ImageIcon pButton = new ImageIcon("/JavaGame/src/resources/playButton.png");
public void settings() { //Set the stuff settings, locations e.t.c.
//BLAH
}
}
So basically the Button needs pButton from the ResourcesManager
Which is Practically playButton.png which is located in the resources package,
the same folder as the ResourcesManager.java
Please Help me with my directory issues.
EDIT: SORRY FORGOT, NO ERROR, JUST A BLANK BUTTON COMES UP WITHOUT MY IMAGE.
As for my directory it looks like
src
mainClasses (package)
mainFrame.java
runClass.java
resources (package)
kingdomraiderlogo.png
playButton.png (image wanted at the momment.)
ResourcesManager.java
I’ll +1
Do it like this:
Edit:
In Java you have two ways of accessing files. One way is via the path that uses forward slashes, which is usually used in the
getResource()method of the classClass. The first slash referc to the src folder of your project.Second way is a
pathnameand is used in the constructor of the classFile, for example. On windows, this then uses double backslashes, because a backslash has to be escaped. Problem is the platform independency, which is easily regained by usingFile.Separatorin the pathname String.