Intellij Java - ImageIcon window runs but no image displayed - java

I am having an issue with getting my image to display within the GUI window. The GUI initiates fine but no image is displayed window displays no image. I have tired various methods with no luck and I'm not sure where I'm going wrong. Any guidance would be greatly appreciated
package banksystem;
import javax.swing.*;
import java.awt.*;
public class Login extends JFrame {
Login(){
setTitle("Cash Machine");
ImageIcon img1 = new ImageIcon(ClassLoader.getSystemResource("bank-icon.jpg"));
Image img2 = img1.getImage().getScaledInstance(100,100, Image.SCALE_DEFAULT);
ImageIcon img3 = new ImageIcon(img2);
JLabel label = new JLabel(img3);
add(label);
setSize(800, 480); //Sets frame size
setVisible(true); //Frame will display
setLocation(350, 200); //Sets frame location
}
public static void main(String[] args) {
new Login();
}
}
folder setup within intellij

ImageIcon img1 = new ImageIcon(ClassLoader.getSystemResource("/images/bank-icon.jpg"));
Also, move resources directory outside of the sources root.

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();
}
}

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

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"));

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

how to make image resizeable

i try to make imageviewer, the code is below
import javax.swing.*;
import java.awt.event.*;
import java.IO.*;
public class javaImageViewer extends JFrame{
public javaImageViewer(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(200,100);
JButton openButton = new JButton("Open Images");
getContentPane().add(openButton);
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser(".");
int status = chooser.showOpenDialog(javaImageViewer.this);
if(status == JFileChooser.APPROVE_OPTION){
try{
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(chooser.getSelectedFile().toURL()));
frame.add(label);
frame.setSize(500,500);
frame.setVisible(true);
}
catch(Exception e2){
System.out.println("Error"+e2);
}
}
}
});
}
public static void main(String [] args){
javaImageViewer tim = new javaImageViewer();
tim.setVisible(true);
}
}
but when i open image from camera, it always showing over the frame size
i dont know how to make the image follow my frame size ?
in order to place your image with the Full Size, you can try to make your own JPanel, override the paintComponent method, and inside this method use g.DrawImage ,
other solution and maybe easier is set the JPanel dimesion with the same dimesion of you Image and Add this JPanel to a JScrollPane , in this way is going to show a scrollbars to navigate
depends of reall size in pixels
1) put Image / BufferedImage as Icon/ImageIcon to the JLabel, then image will be resiziable up to real size in pixels
2) resize Image by usage of Image#getScaledInstance(int width, int height, int hints)

Categories

Resources