Reputation: 85
I want to put an image label at certain coordinates on the screen.
I used the code below, but it doesn't work.
Also, when adding the commented line to the code, it gives an error
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class EmiloLadderSnack
{
public static void main(String args[])
{
Graphics g;
JFrame frame=new JFrame("EmiloLadderSnack");
JPanel panel=new JPanel();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
frame.setVisible(true);
frame.add(panel);
BufferedImage img=null;
try
{
img=ImageIO.read(new File("/media/01CCE00FA6888D80/Achieve/Eclipse/EmiloLadderSnack/src/photo.jpg"));
}
catch(IOException e)
{
//g.drawString(e.toString(), 0, 0);
}
ImageIcon icon=new ImageIcon(img);
JLabel lbl=new JLabel(icon);
lbl.setLocation(50, 50);
panel.add(lbl);
lbl.setLocation(50, 50);
}
}
Please, help me fix this problem
Upvotes: 0
Views: 1886
Reputation: 501
setVisible(true);
should write at the end of, adding all components to Frame.
or else write the
panel.revalidate();
after adding the components to panel.
Your code is here:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class EmiloLadderSnack
{
public static void main(String args[])
{
Graphics g;
JFrame frame=new JFrame("EmiloLadderSnack");
JPanel panel=new JPanel();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
frame.add(panel);
BufferedImage img=null;
try
{
img=ImageIO.read(new File("/media/01CCE00FA6888D80/Achieve/Eclipse/EmiloLadderSnack/src/photo.jpg"));
}
catch(IOException e)
{
//g.drawString(e.toString(), 0, 0);
}
ImageIcon icon=new ImageIcon(img);
JLabel lbl=new JLabel(icon);
lbl.setLocation(50, 50);
panel.add(lbl);
lbl.setLocation(50, 50);
frame.setVisible(true);//put it here
}
}
Upvotes: 0
Reputation: 21223
You should first add components to the frame and then call pack()
, setSize()
or setVisible()
. In your case, you're adding ImageIcon
to the panel after the frame is already visible. Adding panel.revalidate();
should solve the immediate problem. However, it is best if you go through How to Make Frames tutorial first.
As for the second question, Graphics
object is not initialized. Moreover, this is not how Graphics
should be used. If you want to draw a string on the component's surface, override paintComponent() and use the provided Graphics
object. See Lesson: Performing Custom Painting for more details. Otherwise, if the intent is to simply notify user about the error, you could use JOptionPane. The relevant tutorial is How to Make Dialogs.
Also, using absolute layout can be very difficult. Check out A Visual Guide to Layout Managers to understand different layout managers and their basic use.
Upvotes: 1