Aide pour charger une image avec Java.
patrickjunior01
Posted messages
3
Status
Member
-
patrickjunior01 Posted messages 3 Status Member -
patrickjunior01 Posted messages 3 Status Member -
Hello,
I am really desperate. In fact, it has been more than a week that I have been trying to load an image into a panel but to no avail. I have already tried all the possible codes on the internet but nothing works. And the weirdest thing is that the program does not return any exceptions. I am using NetBeans 7.1.2. Please, does anyone have a solution to my problem? Thank you in advance.
Best regards,
Configuration: Windows XP / Chrome 21.0.1180.89
I am really desperate. In fact, it has been more than a week that I have been trying to load an image into a panel but to no avail. I have already tried all the possible codes on the internet but nothing works. And the weirdest thing is that the program does not return any exceptions. I am using NetBeans 7.1.2. Please, does anyone have a solution to my problem? Thank you in advance.
Best regards,
Configuration: Windows XP / Chrome 21.0.1180.89
1 answer
"I have already tried all the possible codes on the internet but nothing works."
The internet is really a mess, or the truth is out there...
The internet is really a mess, or the truth is out there...
import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class Test1 { public static void main(String[] args) { JFrame frame = new JFrame(); frame.add(new JLabel(new ImageIcon("C:/test.png"))); frame.pack(); frame.setVisible(true); } } import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; class ImagePanel extends JPanel { private static final long serialVersionUID = 1; private final BufferedImage img; ImagePanel(String fileName) throws IOException { img = ImageIO.read(new File(fileName)); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img,0,0,null); } } public class Test2 { public static void main(String...args) throws IOException { JFrame frame = new JFrame(); frame.add(new ImagePanel("C:/test.png")); frame.setSize(300,300); frame.setVisible(true); } }The trust does not exclude control.
patrickjunior01
Posted messages
3
Status
Member
Thank you KX. But will I be able to read files with the JPEG, JPG extension or others with this code?
KX
Posted messages
19031
Status
Moderator
3 020
GIF, PNG, JPEG, BMP, and WBMP
patrickjunior01
Posted messages
3
Status
Member
Thank you very much.