I have searched the topic in the Google, but it wasn't very clear about what I should do. my question is:
What's wrong with my code? How to revise it.
The answer in many websites always use URL, how can I know the URL of my image?
Here is the code:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TwoPanel {
public static void main(String[] args) {
JFrame frame = new JFrame ("Key test");
MyDrawPanel1 dp1 = new MyDrawPanel1();
//MyDrawPanel2 dp2 = new MyDrawPanel2();
//MyDrawPanel3 dp3 = new MyDrawPanel3();
//MyDrawPanel4 dp4 = new MyDrawPanel4();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
JPanel p = new JPanel ();
p.setLayout(new BorderLayout());
p.add (dp1);
//p.add (dp2);
//p.add(dp3);
//p.add(dp4);
frame.getContentPane().add(p);
frame.pack();
}
}
then the drawpanel:
public class MyDrawPanel1 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
Image image = new ImageIcon("Koala.jpg").getImage();/*something new: if i use the
path to the disk name(D:/Java/workspace/firstJava/src/com/founder/panel/Koala.jpg'),
it can work.*/
public void paintComponent (Graphics g) {
super.paintComponent(g);
//Graphics2D g2 = (Graphics2D) g;
g.drawImage(image, 3, 4, null);
}
public Dimension getPreferredSize() {
if (image != null) {
return new Dimension(image.getWidth(null), image.getHeight(null));
}
return super.getPreferredSize(); // default
}
}
And this panel can work, although i cann't understand why other doesn't work
package com.founder.panel;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
public class MyDrawPanel4 extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String URL_PATH = "file:///D:/Java/workspace/firstJava/src/com/founder/panel/" +
"Koala.jpg";
BufferedImage image = null;
public MyDrawPanel4() {
// TODO Auto-generated constructor stub
setBackground(Color.white);
try {
image = ImageIO.read(new URL(URL_PATH));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
if (image != null) {
return new Dimension(image.getWidth(), image.getHeight());
}
return super.getPreferredSize(); // default
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, null);
}
}
}
My guess is that the image is not loaded, use the path of the file as an argument to ImageIO.read(new File("Koala.jpg"))
. Using ImageIO
:
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 TwoPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showMainWindow();
}
});
}
public static void showMainWindow() {
JFrame frame = new JFrame ("Key test");
MyDrawPanel1 dp1 = new MyDrawPanel1();
//MyDrawPanel2 dp2 = new MyDrawPanel2();
//MyDrawPanel3 dp3 = new MyDrawPanel3();
//MyDrawPanel4 dp4 = new MyDrawPanel4();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel ();
p.setLayout(new BorderLayout());
p.add (dp1);
//p.add (dp2);
//p.add(dp3);
//p.add(dp4);
frame.getContentPane().add(p);
frame.setLocationRelativeTo(null); // center frame
frame.setMinimumSize(new Dimension(300,400)); // make the frame at least 300x400
frame.pack();
frame.setVisible(true);
}
}
The MyDrawPanel1
:
class MyDrawPanel1 extends JPanel {
private static final long serialVersionUID = 1L;
//Image image = new ImageIcon("Koala.jpg").getImage();
BufferedImage image;
MyDrawPanel1() {
super();
try {
this.image = ImageIO.read(getClass().getResource("com/founder/pane/Koala.jpg"))
} catch (IOException e) {
System.err.println("Koala.jpg does not exist!");
e.printStackTrace();
}
}
@Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.drawImage(image, 3, 4, null);
}
}