Adding few images in Java JFrame - java

I'm making 2D game. I have background and character images. Both of them are .gif files. What layout or something do I need to use to set background behind the character? I tried some ways but either they're in a row or nothing happens.
I am adding pictures like that:
URL url = Main.class.getResource("images/main.gif");
ImageIcon imageIcon = new ImageIcon(url);
JLabel background = new JLabel(imageIcon);

Code for Background :
first of all copy your background image and paste in src of code
than set layout to borderlayout like this:
setLayout(new BorderLayout());
now add this code:
setContentPane(new JLabel new ImageIcon(getClass().getResource("image.jpg"))));
Note: add your image name here "image.jpg"
now set layout to flow layout like this
setLayout(new FlowLayout());
and write your code here

Related

Creating an Image Icon from a local file for eclipse

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).

Inserting an image as a label in java

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 :)

Simple Java GUI, cards not appearing

import javax.swing.*;
public class SlideShow {
JFrame slide = new JFrame("Slide Show");
public SlideShow(){
slide.setSize(300,400);
slide.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
slide.setVisible(true);
slide.setLocationRelativeTo(null);
JPanel panel = new JPanel();
JLabel label = new JLabel(new ImageIcon("Images/picture1"));
panel.add(label);
slide.add(panel);
}
public static void main(String[] args){
SlideShow slide = new SlideShow();
}
}
I have to create a simple Java GUI that displays some cards. First, I just wanted to test it by displaying one card. For some reason I can't seem to figure out why nothing is being displayed.
You haven't actually used a proper file name "Images/picture1". Should be something like "Images/picture1.png" with the file format
Also image files, generally should be read from the class path, if you plan on having them embedded to the program. To do so, you will first need to put the file in the class path. With most IDE build configurations it's as simple as placing the image in the src. So
ProjectRoot
src
images
picture1.png
Then you would read it like
new ImageIcon(getClass().getResource("/images/picture1.png"));
A better approach would be to use ImageIO.read(). If the file path is incorrect, it will throw an exception, so you know where you're going wrong
Image image = ImageIO.read(getClass().getResource("/images/picture1.png"));
ImageIcon icon = new ImageIcon(image);
You will need to put it in the try/catch block
Also do what codeNinja said about the setVisible() after adding component. Also preferably pack() the frame, instead of setSize()
You need to set the Frame visible after you add all necessary components to it. Move slide.setVisible(true); Down to the bottom of the constructor like this:
...
slide.add(panel);
slide.setVisible(true);
Alternatively you can add slide.revalidate(); at the bottom of your constructor.

Using JLabel for a background image

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.

Jpanel not showing up in safari

I have an applet I have written that has a JLabel (containing an ImageIcon) and a custom ImagePanel inside a JPanel. For some reason the JLabel NEVER shows up in safari and firefox on mac os on first run/load but on other OSes (windows,linux) it appears fine. Now in the same applet there's a button that flips the image to another image. On safari/firefox on mac os, when the button is clicked, the second image shows, the when clicked again, the first image now appears!! Any idea what could be causing this issue? Even on safari for windows the applet works fine.. i.e. first image loads and appears.
UI code
public void createUI(){
mainpanel = new JPanel();
mainpanel.setMaximumSize(new Dimension(154, 212));
mainpanel.setMinimumSize(new Dimension(154, 212));
mainpanel.setName("mainPanel");
mainpanel.setLayout(new BorderLayout());
lcdpanel = new ImagePanel(bgLcdImage);
lcdpanel.setBounds(22, 22, 110, 28);
bgImage = Toolkit.getDefaultToolkit().createImage(bytes);//BufferedImage
label = new JLabel(new ImageIcon(bgImage));
mainpanel.add(lcdpanel);
mainpanel.add(label);
mainpanel.invalidate();
getContentPane().add(mainpanel);
repaint();
}
Button click code
private void flipImage()
{
label.setIcon(new ImageIcon(backImg));
label.repaint();
lcdpanel.setVisible(false);
lcdpanel.repaint();
mainpanel.repaint();
this.repaint();
}
Any help would be appreciated.Thanks
I even made the jlabel as a imagepanel, set the layout of jpanel to null
Setting the layout to null is the worst thing you can do. That will generally cause more problems than solve a problem.
The issue is not where i want the jlabel to be shown but WHY its not showing on Mac OS X firefox/safari browsers when it shows on windows/linux firefox/safari browsers.
How do we know when only a few lines of code are posted? Post a proper SSCCE when you have a problem.
On safari/firefox on mac os, when the button is clicked, the second image shows, the when clicked again, the first image now appears!!
The general format when adding/removing components on a visible GUI is to do:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
You never need to invoke repaint when you change the property of a component. Swing is smart enough to do the repaint for you.
label.setIcon(new ImageIcon(backImg));
//label.repaint();

Categories