java game picture not display - java

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.

Related

Background image not viisible in JFrame [duplicate]

Having a issue trying to display my logo. The picture is saved in the same folder as main.java
ImageIcon im = new ImageIcon("banner.png");
JLabel bam = new JLabel(im);
grid.add(bam);
Is there a problem in my syntax?
There are any number of possible issues, but the likely one is, the location of the image is not within the same context as where the application is being executed from.
Let's say, main.java lives in some directory (lets just say "path/to/class" for argument sake), then when you execute you main.java, the path to the images would become something like /path/to/class, meaning you should be using using something like...
ImageIcon im = new ImageIcon("path/to/class/banner.png");
This also assumes that the image hasn't being Jar'ed yet, as ImageIcon(String) expects a path to a file on the file system.
If the program has being Jar'ed then you won't be able use ImageIcon(String), as the banner.png is no longer a file, but a resource, then you would need to use something like...
ImageIcon im = new ImageIcon(getClass().getResource("/path/to/class/banner.png"));
Where /path/to/class is the package where main.java lives.
In either case, I would recommend that you use ImageIO.read instead, as this will actually throw an IOException when something goes wrong, where ImageIcon tends to fail silently...
Take a look at Reading/Loading an Image for more details
Try ImageIcon im = new ImageIcon(new File("banner.png")); If you're using eclipse, put it in the root folder, not the src folder. The root folder has the src folder and the bin folder.
If you create a folder called images (or whatever name you prefer) in your source directory, and then place the banner.png file in that folder you can then use the following code.
ImageIcon im = new ImageIcon(this.getClass().getResource("/images/banner.png"));
I know this is an old post, but I had the same issue on my project and fixed it by giving the full path in the ImageIcon definition, instead of just the path from the project. I.e.
ImageIcon im = new ImageIcon("C:/your/image/file/path/banner.png);
It solved the issue for me, but I do recognise that this is not a valid long-term solution, since it will only work on the developer's machine and I'm not sure if portability would be an issue.
Hope I have helped somebody.
There's a lot of things that could be going on here and it's hard to tell without more code. Based on the short snippet above (and assuming everything else is correct) you should check the image load status of the ImageIcon using the getImageLoadStatus() method. If it returns MediaTracker.COMPLETE then you're good to go and the error is somewhere outside this code. If it's anything but COMPLETE then you are having some sort of issue in loading the image. Maybe it's not in the path or classpath?

Icon is added to JavaFx stage, but doesn't show up

I'm fairly new to graphical libraries, in this case, JavaFX, and I wanted to add an icon to my project. So I googled and found two different methods:
The first one was pretty straightforward:
stage.getIcons().add(new Image("300px-Wikibooks-logo.png"));
That just put out an error message, that the URL isn't valid.
The second method is very similar:
stage.getIcons().add(new Image(getClass().getResourceAsStream("300px-Wikibooks-logo.png")));
This one doesn't fail. It compiles and runs just fine, but the icon isn't showing up. I even tried every single location in my program and I used every "Copy Path" option. I also read that there should be a / in front of the pathname, but I didn't work with it either.
For better visualization, here is my file tree:
Aufgabenblatt5
---.idea
---src
-----main
-------java
---------de.ab5.abgabe5
-----------(here are all my classes)
-------resources
---------de.ab5.abgabe5
-----------(here was the image file while it worked)
----target
Maybe someone can help.

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()?

App loses Images when built

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

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

Categories