Why is my Image Icon not showing up in my JFrame? - java

I am trying to display an image icon PNG, I have properly referred to the file called "logo.png" inside the project folder and set the icon as seen below. I'm not sure where I went wrong as I followed this tutorial starting around minute 14 (https://www.youtube.com/watch?v=Kmgo00avvEw&t=298s), I have made sure the code was correct but I'm still unable to get the image to appear beside the text when I run the code in Eclipse.
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class mainClass {
public static void main(String[] args) {
// JFrame is a GUI frame to add components to
//JLabel a GUI display area for a string of texts and image.
ImageIcon image = new ImageIcon("logo.png");
JLabel label = new JLabel(); //creates JLabel
label.setText("Welcome to Magic Shape"); //sets text for label
label.setIcon(image);
JFrame frame = new JFrame(); //creates JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close operations when...
frame.setSize(500, 500); //sets size of JFrame (can resize)
frame.setTitle("Magic Shape"); //title of Program
frame.setVisible(true); //makes frame visible
frame.add(label); //adds label
}
}

package be.project.jframe;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws IOException {
// JFrame is a GUI frame to add components to
//JLabel a GUI display area for a string of texts and image.
InputStream stream = Main.class.getResourceAsStream("/be/project/jframe/icons/400.png");
ImageIcon icon = new ImageIcon(ImageIO.read(stream));
JLabel jLabelObject = new JLabel("Welcome to Magic Shape");
jLabelObject.setIcon(icon);
JFrame frame = new JFrame(); //creates JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close operations when...
frame.setSize(800,600);
frame.setTitle("Magic Shape"); //title of Program
frame.setVisible(true); //makes frame visible
frame.add(jLabelObject); //adds label
frame.pack();
}
}

According to your code, Java searches for the file logo.png in the working directory. The working directory is the value returned by System.getProperty("user.dir"). If Java does not find the file, it essentially creates a null icon and that's why you aren't seeing the icon.
Just make sure you put the file in the correct directory.
Refer to the following.
Accessing Resources
Retrieving Resources
How to Use Icons

Make sure the png file is placed at current working directory:
System.out.println(System.getProperty("user.dir"));

Related

How do I import an image in intellij (JAVA)

I'm fairly new to coding and was trying to make a simple GUI with JAVA Swing. I wanted to place an image in a label but for some reason does the image does not show when I run the Code. Are there some requirements I don't know of or did I do something wrong?
package Part2Lables;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
//Label = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("Kakashi.png");
JLabel label = new JLabel(); // create a label
label.setText("This is text in a label"); // set text to the label
label.setIcon(image);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.add(label);
}
}
This is the code I used (I did use imports but they are not visible in the pic.)

setIcon of JLabel is not working, but setIconImage of JFrame is

I am using VS Code and my folder contains one Java file and one image only.
I was trying to set image in JLabel, so first I access the image by:
ImageIcon img = new ImageIcon(getClass().getResource("img_flag.png"));
Then I set this to JLabel but nothing appears on my screen.
JLabel label = new JLabel(img);
To confirm that I am accessing the correct image, I set it to the logo of the window and the logo was showing properly.
setIconImage(img.getImage());
I can't figure out why label can't show the image.
Here is my full code:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Mylable extends JFrame{
Mylable(){
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
ImageIcon img = new ImageIcon(getClass().getResource("img_flag.png"));
setIconImage(img.getImage()); //working
JLabel label = new JLabel(img); //not working
add(label);
}
public static void main(String[] args) {
new Mylable();
}
}

Java - How do I get an image to display?

I have spent a really long time trying to find a way to display an image in a Java program (I'm trying to learn how to make a 2D Java game) an nothing that I've tried works. I'm using Eclipse Mars and the latest of everything else. Here is my code:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame {
public static void main(String[] args0) {
JFrame frame = new JFrame();
ImageIcon image = new ImageIcon("background.bmp");
JLabel imageLabel = new JLabel(image);
frame.add(imageLabel);
frame.setLayout(null);
imageLabel.setLocation(0, 0);
imageLabel.setSize(1000, 750);
imageLabel.setVisible(true);
frame.setVisible(true);
frame.setSize(1000, 750);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Please, just tell me how to correct the code so that the image actually displays. Thank you ahead of time.
(P.S. the "background.bmp" file is in the "default package" if that changes anything)
The image with .bmp suffix can't be displayed in your code. Try to modify
ImageIcon image = new ImageIcon("background.bmp");
to
ImageIcon image = new ImageIcon("background.png");
and change the image name to background.png.
However, if you just want to use background.bmp, you need to modify your code like this and background image will be displayed.
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame {
public static void main(String[] args0) {
try {
JFrame frame = new JFrame();
File imageFile = new File("background.bmp");
Image i = ImageIO.read(imageFile);
ImageIcon image = new ImageIcon(i);
JLabel imageLabel = new JLabel(image);
frame.add(imageLabel);
frame.setLayout(null);
imageLabel.setLocation(0, 0);
imageLabel.setSize(1000, 750);
imageLabel.setVisible(true);
frame.setVisible(true);
frame.setSize(1000, 750);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here is the effect with new code:
Hope it helps.
BTW, I've put the image at the root directory of the project.
Read the section from the Swing tutorial on How to Use Icons. The examples their show you how to load the images as resources. When you use resources the classpath will be searched to find the file.
//frame.setLayout(null);
//imageLabel.setLocation(0, 0);
//imageLabel.setSize(1000, 750);
Don't use a null layout and attempt to set the size/location of the label. Let the layout manager do its job. The label will be the size of the image.
//frame.setSize(1000, 750);
frame.pack();
Don't use frame.setSize(...). Instead you use frame.pack() then the frame will be the size of the image plus the size of the frame borders and title. Let Swing and its layout managers do the work for you.
As a beginer, I found that is easy to see the picture you draw:
Source code
public class CheckCodeTest {
private int width = 100, height = 50;
private BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
#Test
public void drawGraphicsTest() throws IOException {
Graphics graphics = image.createGraphics();
// draw an orange rectangle
graphics.setColor(Color.orange);
graphics.fillRect(0,0,width,height);
// layout the picture right now!
graphics.drawImage(image,0,0,null);
ImageIO.write(image, "png", new File("checkcode.png"));
}
}
Output
It produce a picture file under your projects content.
output-picture
Then you can see what change after adding draw code in small window, it is more convenient than closing an jump-out Frame / Label window:
output-picture-in-editor

Putting everything into a JFrame

I want to know how to put put console output into a JFrame. For example, putting this output into a JFrame:
import static java.lang.System.out;
public class frame{
public static void main(String [] args){
out.println("hello");
}
}
How is it possible?
You need to set up the JFrame first.
JFrame frame = new JFrame("title");
Then, set the properties of the JFrame:
frame.setSize(1280,720); //Sets the program's size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to exit on close
frame.setResizable(true); //Tells the program if resizing is enabled
Then, create a panel to store the components:
JPanel p = new JPanel();
After that, you must add the panel to the JFrame like so:
frame.add(p);
Then, with that done, you can use the components supplied in the swing framework, and add them to the panel. A reference for these components can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html.
To create a component, use the following code:
JLabel label = new JLabel();
Then, use it's build in functions to change it:
label.setText("new text");
Then, once again, to add a component to a panel, use the panel's add() method:
panel.add(label);
Those are just the basics of making a GUI with java. A full tutorial can be viewed here:
http://docs.oracle.com/javase/tutorial/uiswing/
Good Luck!
I can help you with this, but let me please fix some syntax errors you have. When you put the import, an import can't be static (that I know of) and when you want to print out something using "System.out.print" or "System.out.println" you MUST include the "System" part of the line. If you want to add text to a a JFrame use the JLabel to import both just do this bit of code:
import javax.swing.*;
That should import all of your swing elements such as JLabel and JFrame and JPanel, and try this code it will make a window that will have a button and a label. The button doesn't do anything in this code:
import javax.swing.*;
public class main{
public static void main(String[] args)
{
/*
* Creates the frame, makes it visible, and makes
* appear in the center of the screen. While also making it have a close operation
*/
JFrame frame = new JFrame("Button");
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//Creates the panel, and adds it to the frame
JPanel panel = new JPanel();
frame.add(panel);
//Creates the label and adds it to the panel, also sets the text
JLabel label = new JLabel();
label.setText("Welcome" + "\n" + "\n");
panel.add(label);
//Creates the button and adds it to the panel
JButton button1 = new JButton("Button 1");
panel.add(button1);
}
}
1.If you want to use JFrame you have to extend your class to a subclass of JFrame:
public class frame extends JFrame {}
2.a)If you want to put Text in your Frame use JLabel and add it to your frame:
JLabel hello = new JLabel("Hello");
add(hello);
2.b)If you want a console output just call System.out.println() in the constructor
Here is a small example class:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Frame extends JFrame {
public static void main(String args[]) {
new Frame();
}
Frame() {
System.out.println("Hello");
JLabel hello = new JLabel("Hello");
add(hello);
this.setSize(100, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
have a look to the oracle lessons... or any java book!
If that is the case, I don't want to get into GUI just yet. (Learning from a book) Can I convert my current project to a jar file and have it automatically open a command prompt window upon double click?

How to place images on top of each other in java

I have a background image of a road, which I have displayed on a JFrame using an ImageIcon. I want to put a car on top of that background image at a certain (x,y) location.
I tried using another ImageIcon and the background does not appear when I run the program , however the car does.
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Gui extends JFrame {
private ImageIcon northcar = new ImageIcon("src/north.gif");
private ImageIcon usIcon = new ImageIcon("src/trafficLight.jpg");
public Gui() {
add(new JLabel(usIcon)); // background image does not appear after the i added the northcar label
add(new JLabel(northcar)); // this picture can be seen
}
public static void main(String[] args) {
Gui frame = new Gui();
frame.setTitle("TestImageIcon");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 650, 650);
frame.setVisible(true);
}
}
I heard about using a canvas but have no clue. Any ideas ?
Many Thanks
How about using LayeredPane, this might help.
add(new JLabel(usIcon));
add(new JLabel(northcar));
Change the order to:
add(new JLabel(northcar));
add(new JLabel(usIcon));
Read up on Z-Order. Simply the last component added gets painted first.

Categories

Resources