The following code produces error:
ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(10,10,Image.SCALE_DEFAULT);
mainPanel.add(scaleImage);
The error is cannot find method add(Image).
Why it is giving this error?
You can't do it like that. JPanel does not accept Image as parameter (that's what the error tells you).
You have two options:
draw the image in the panel instead. The solution uses the paintComponent(..) method.
use a JLabel and an ImageIcon
The answers to this question will show you how to do it either way.
ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(70,70,Image.SCALE_DEFAULT);
ImageIcon ii=new ImageIcon(scaleImage);
JLabel pic=new JLabel(ii);
mainP.add(pic); // now you can add
Related
So I am trying to add an image to a JLabel object which is added to a JPanel called "topPanel" which is part of a JFrame called
"primaryWindow". I have already declared the "topPanel" and "primaryWindow". I found on other forms that you have to create a separate source folder and add the image file in that folder in order to access it and I did so.
However, when I execute the following, the image does not appear to be on the Label. I know that it has nothing to do with adding the JLabel to the panel properly because when I enter a String into the JLabel constructor, the String appears on the panel. An image however does not appear. Am I properly adding the image? I am using a mac if this helps.
private JLabel image = new JLabel();
image.setIcon(new ImageIcon("Check.png"));
topPanel.add(image);
primaryWindow.add(topPanel, BorderLayout.NORTH);
You can also use BufferedImage
BufferedImage myPicture = ImageIO.read(new File("C:\\xx\\xxx\\Check.png.jpg"));
Image scaled = myPicture.getScaledInstance(100,70,Image.SCALE_SMOOTH);
image = new JLabel(new ImageIcon(scaled));
topPanel.add(image);
primaryWindow.add(topPanel, BorderLayout.NORTH);
Note that "C:\\xx\\xxx\\Check.png.jpg" is the path where you save Check.png.
Hope this helped.
The ImageIcon that you passed into the setIcon method of the JLabel could be null. Have you tried to check if it is null before calling setIcon? If you create a "res" resource folder in the root directory of your project, you could try the following:
image.setIcon(new ImageIcon(ImageIO.read(new File("res/Check.png"))));
Additionally, if you are using Eclipse, you should try and refresh the project directory by right clicking and pressing refresh; sometimes Eclipse doesn't register files added.
Lastly, try setting the background of the JPanel to a certain color to see if it is displaying it and make sure its width and height are not 0 (it is possible that the layout you are using changed its size).
This question already has answers here:
Java: how to add image to Jlabel?
(5 answers)
Closed 7 years ago.
How to add a picture to JLabelin Java?
You can use ImageIcon
JLabel l = new JLabel(new ImageIcon("path-to-file"));
Try this code:
ImageIcon imageIcon = new ImageIcon("yourFilepth");
JLabel label = new JLabel(imageIcon);
For more Info
jLabel1 = new javax.swing.JLabel();
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\admin\\Desktop\\Picture 34029.jpg"));
Here i posted the code that got generated automatically in netbeans.Hope my code helps in this regards keep coding goodluck.
You have to supply to the JLabel an Icon implementation (i.e ImageIcon). You can do it trough the setIcon method, as in your question, or through the JLabel constructor:
Image image=GenerateImage.toImage(true); //this generates an image file
ImageIcon icon = new ImageIcon(image);
JLabel thumb = new JLabel();
thumb.setIcon(icon);
I recommend you to read the Javadoc for JLabel, Icon, and ImageIcon. Also, you can check the How to Use Labels Tutorial, for more information.
Also have a look at this tutorial: Handling Images in a Java GUI Application
label_007= new JLabel("My Label");
In lieu of the above Label, My Label I want to insert an image in the same position. How can I do that? I am just a novice in Java. Please help
ImageIcon icon = new ImageIcon('image.png');
JLabel image = new JLabel();
image.setIcon(icon);
The above code should work.
Make an image icon with an image file
Make a JLabel
Then set your JLabel's Icon
You can use ImageIcon for images:
ImageIcon ICON = new ImageIcon(URLClassLoader.class.getResource(IMAGE_PATH));
JLabel imageLabel = new JLabel();
imageLabel.setIcon(ICON);
URLClassLoader.class.getResource is using for packaging the application in runnable-jar file.
The IMAGE_PATH is relative to your project folders/packages. for example if the image is in com.app.images package, the path will be /com/app/images/image.png.
If you're novice in Java and particularly in Swing, I encourage you to check my swing projects- Minesweeper and Pacman. They are well-documented, I built them when I learned Swing.
Good luck :)
Just a rather simple step but I can't get it done.
A code like this:
label.setIcon(new ImageIcon ("abc.JPG"));
Problem:
ImageIcon cannot be resolved to a type
What's wrong there? Is there any libraries or configuration that I missed out?
When I run the code it just opens an empty window
I also important whatever is necessary
relevant parts of the code:
public class Game extends JFrame implements ActionListener,KeyListener{
private JLabel background;
....
public Game(){
background=new JLabel(new ImageIcon("/graphics/board.gif"));
...
this.add(background);
this.setSize(800,600);
this.setVisible(true);...
I tried adding the JLabel to a JPanel and then add it to the frame but it still shows nothing in the window
Originally the code was:
JLabel background = new JLabel("/graphics/board.gif");
This would not set the image at the path described, Suggest that the following method is used (this could be simplified to just use a different JLabel constructor but steps shown for clarity)
Create and load the image and then set the icon for the Label As follows
ImageIcon icon = new ImageIcon("/graphics/board.gif");
JLabel background = new JLabel();
background.setIcon(icon);
Link to ImageIcon Java Doc
It is important to set in the layout the order in which the elements are displayed , maybe you have something that is displayed over the label..
I'm guessing you have a directory structure something like:
-c:\java
- source (for source and class files)
- graphic (for your images)
background=new JLabel(new ImageIcon("/graphics/board.gif"));
Don't specify the leading "/" in the file name. That tells Java to look at the root of the C drive, not at the directory where your class is executing from.
Also, don't use:
this.setSize(800,600);
The image does not stretch to fill the size of the frame. Intead you should be using:
this.pack();
so the frame will be the size of the image.