App loses Images when built - java

For a project I am working on, I have made a Bingo Card. The basic functionality is that a Card is randomly generated and displayed in STD out as well as in a Swing GUI I made by hand. This application has multiple java files within.
BingoFinal.java - The main file. This is what's run when the program is run.
Bingo_Card.java - creates the Bingo card, prints it to STD out, and checks for bingo
BingoBG.java - Draws the background of the GUI with 2D graphics
DrawBingoCard.java - Calls BingoBG and also creates 25 labels with the board values. When Bingo_Card finds a matching number(inputted by STD IN), it no longer prints the number, and prints Chip.png(which is in the same package folder as the java files), a bingo chip image, making it look covered.
This works flawlessly when I run it through NetBeans, but when I clean and build it, then run the jar in terminal, everything works except for displaying the bingo chip image. Does anybody know why that would happen?
EDIT: Here is how I load the Image
ImageIcon chip; //declare ImageIcon
chip = new ImageIcon("chip75.png"); //define it as chip75.png. It is stored in package folder
JLabel B1Chip; //declare empty Label
B1Chip = new JLabel(chip); //define the Label with just the ImageIcon
B1Chip.setBounds(22, 112, chip.getIconWidth(), chip.getIconHeight()); //place at (22,112)
frame.add(B1Chip, null); //Add to frame

You should be accessing the images, by using :
ImageIcon chip = new ImageIcon(ClassName.class.getResource("/chip75.png"))
More information can be found on the info page of tag: embedded-resource A detailed answer regarding adding images to the NetBeans Project is mentioned on the info-page link.
More importantly, it would be wise, that you use ImageIO.read, since atleast it will let you know, if something goes wrong, by throwing IOException
Throws:
IllegalArgumentException - if input is null.
IOException - if an error occurs during reading.
ImageIcon on the other hand will hide the exception if any :-)

copy the image files into jar or give absolute path of the image files

Related

Why do i get a NullPointerException when loading in ImageViews

I am currently making a poker game in java. The model works just fine. I do however also have to make a proper view for the game. I have to use javaFX scenebuilider as well as a graphic view class with actual code. In order to do this I made a superview to include both. When i am trying to run my application I get a white window and a nullpointerexception (for the code I included).
I do not get what I am doing wrong, since no one ever explained to me the basics of Imageviews and loading images in.
I made a package "res" where I embedded all the pics that represent my cards in the deck. Also note that k.toString() automatically generates the proper name of the file including the ".png" part. I printed it out and I am sure it works!
I use the for-loop to go through my list of cards that make the deck. My cards are generated in the Class Card. So basically I am going through an ArrayList of cards.
public void initializeDeck(){
for(Card c:model.getRound().getDeck().getList()){
int index = model.getRound().getDeck().getList().indexOf(c);
imv = new ImageView(new Image(getClass().getResourceAsStream("/res/"+k.toString()),95, 141, false, true));
deckView.add(index, imv);
}
getChildren().addAll(stapelView);
}

JButton hover effect delay in Java

I want to know about these two questions.
1. I am using NetBeans and I have created a project, I already have saved all the images in "src" folder, but when I put just image name like (image.png) then no image will displayed on button. I have attached working code as well as no working code below -
Not Working Code:
private void loginMouseEntered(java.awt.event.MouseEvent evt) {
login.setIcon(new ImageIcon("login_button_hover.png"));
}
Working Code
private void loginMouseEntered(java.awt.event.MouseEvent evt) {
login.setIcon(new ImageIcon("C:\\Users\\Zeeshan\\Documents\\NetBeansProjects\\Attandence Software\\src\\images\\login_button_hover.png"));
}
2. I want to give delay to the "Hover" image.
For example: If I take my mouse pointer to the Button/Image, the Hover image will display slowly and take 1 sec to display completely.
Read the javadoc of the constructor:
public ImageIcon(String filename)
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path.
(emphasis mine)
It doesn't expect a classpath resource path as argument, but a file path (i.e. the path of a file on the file system, not the path of a resource of your app).
If you want to load a resource using the ClassLoader, then use
new ImageIcon(MyClass.class.getResource("/login_button_hover.png"));
Also, you'd better load this ImageIcon once and reuse it, instead of reconstructing it every time the mouse enters the button.
Regarding your second point, you could start a swing Timer every time the mouse enters the button, and have the timer change the icon once the second is elapsed. But you'll have to deal with multiple enters per second, etc. What's wrong with setRolloverIcon()?

Include image inside a Java code

I'm using Dr. Java as a compiler for Java. I am trying to include an image inside my code. I'm building my first GUI program.
This is the code I used:
import java.awt.*;
import javax.swing.*;
public class Test{
public static void main(String[] args){
JFrame frame = new JFrame("Test Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel primary = new JPanel();
primary.setBackground(color.green);
primary.setPreferredSize(new Dimension(100,100));
JLabel label = new JLabel("Tesing the program");
primary.add(label);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
Lets say there's a file on my desktop called image.gif and I want to include this image in my code.
Can I store images as variables to use them in my code?
Where is the image usually stored in a java program and how can I refer to it?
Is an image a form of object?
What is the code to include the image I have in the program I am writing on Dr. Java?
Can I store images as variables to use them in my code?
yes you can. by using this command: ImageIcon image = new ImageIcon("C:\image.gif");
(instead of C:\image.gif you must write your image location)
Where is the image usually stored in a java program and how can I
refer to it?
imageicon helps you to show your image and put it where you want.
Is an image a form of object?
everything in java is an object, except primitive types.
What is the code to include the image I have in the program I am writing on Dr. Java?
first you must place your image somewhere you wanna refer to. and then define it as I told. and use how you want according the references.
Im building my first GUI program.
Start by reading the Swing tutorial on Creating a GUI With Swing for all the basics and lots of working examples.
I want to include this image in my code.
You can start with the section on How to Use Icons.
Can I store images as variables to use them in my code?
Yes, images are usually stored as variables.
Where is the image usually stored in a java program and how can I refer to it?
Images, basically are termed as application resources, that are used to provide some information, apart from text displayed on the GUI
You can have a look at working with Images and this simple example
As you deploy an application, Images used in the application are often embedded into the JAR file, along with the rest of the application. This answer, might can give you more information, as to how to add a resource to the project
Or you could serialize a java.awt.image.BufferedImage under a class name and store it with the binaries in a resources folder, but why not simply properly structure your applications folders.
java.awt.Toolkit obtained from the swing or awt container is used to acquire images
the java.awt.Graphics object is used paint images onto the container and is also obtained through a method on the gui container you wish to paint on.
Another more complex imaging file manipulation system is in core java api
called javax.imageio

combine multiple windows to a single window

I am developing an application which have the following windows
If the information entered in the windows are correct than only the user will be prompted with the windows in the above sequence. Now the customer has demanded me with this user interface.
Now I have to add all these windows in the last window format, with the specification's as the user will be allowed in the 2nd portion of the last image if the first information entered is correct.And the user when launches this app see the last image and can change the values as any time in the respective portions of the last window.
I have coded it in Swing Java.I am new to Java. I am working in Netbeans 7.1.2 I have three files as
1)Login.java
-containing my LoginDemo class which have main and form object of extended Jframe class
-Login class extending J frame and implementing action listener(this class creates an J frame of next file Enter the information.
2)Algorithm.java
creates new J frame object of next file if information is correct.
3)TravellingSalesmanProblem.java
gives the output as shown in Optimal Travel Route window.
I am accessing the information using REST call to a website.
So can anyone help me in this?
This will depend on how you structured you code. If you simply placed components directly onto the the windows/frames themselves, then you might be in for some work.
Alternatively, if you used panels, which you then placed onto windows, this might save you some time.
Anyway. Assuming you only have windows.
for each window do
myLastWindow.add(window.getContentPane());
This is pretty simple, but you'll also need to know the layout you want. I'd suggest something like GridLayout or VerticalLayout from the SwingX project.

java game picture not display

I make 2D game in AWT and I had all files in one package. Now I divided files into some packages. Images, which I called with:
ImageIcon ii=new ImageIcon(this.getClass().getResource(image));
img=ii.getImage();
now I call with:
ImageIcon ii=new ImageIcon("cz/ryska/awtgame/images/"+image);
img=ii.getImage();
This code is in class in package cz.ryska.awtgame.basic
But when I start game, displays game scene but not display images. Scene is empty. But any Java error is not induced. Images are probably found but not display. What is problem?
..Painting was functional before I change package structure.
You did more than change the package structure. The change is that the first is loading an image by URL, while the 2nd is loading an image from a File (where the String represents the path). An URL can be used with an embedded resource, while a File cannot.
See the info. page on embedded-resource for further info.

Categories