Reputation: 615
I have this class which should draw an image.
package ro.adlabs.imnuriAZSMR.UIClases;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class JImage extends JPanel {
private BufferedImage image;
private int height;
private int width;
public JImage(String imagePath,int height,int width) {
try {
image = ImageIO.read(getClass().getResourceAsStream(imagePath));
} catch (IOException ex) {
ex.printStackTrace();
}
this.width = width;
this.height = height;
}
public JImage(String imagePath,int size){
new JImage(imagePath,size,size);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, width, height, this);
}
}
And this Class which show an About Dialog:
package ro.adlabs.imnuriAZSMR.UIClases;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AboutDialog extends JDialog {
public AboutDialog() {
setTitle("About");
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JLabel name = new JLabel("<html><div style='text-align: center;'>Aceasta aplicatie e dezvoltata sub Termenii si Conditiile ADLabs.</div></html>");
JLabel copyright = new JLabel("© ADLabs - www.adlabs.ro");
name.setAlignmentX(0.5f);
copyright.setAlignmentX(0.5f);
add(name);
add(new JImage("../ico/appicon_200x200.png",50));
add(copyright);
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
dispose();
}
});
close.setAlignmentX(0.5f);
add(close);
setModalityType(ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 200);
}
}
I add to the dialog the Image which is the app logo. First time when I created the JImage class it worked, it drawn the picture. Then I added the method:
setSize(width+20,height+20);
to the Jpanel in the JImage class and when I ran the program again it didn't draw the image. Then anything I did it didn't solve this wierd bug. Anyone has any ideea? What am I doing wrong?
Upvotes: 0
Views: 121
Reputation: 109547
You are using a resource path, not a file system path. Such a path may not contain ..
and is either relative to the package directory of the class or absolute.
new JImage("../ico/appicon_200x200.png", 50)
should become something like:
new JImage("/ro/adlabs/imnuriAZSMR/ico/appicon_200x200.png", 50)
Also:
public JImage(String imagePath, int size){
new JImage(imagePath, size, size);
}
should be
public JImage(String imagePath, int size){
this(imagePath, size, size);
}
Upvotes: 2
Reputation: 324098
Graphics doesn't draw image in java
You are using a BoxLayout
. A BoxLayout
will use the preferred size information of the panel when doing the layout. Your preferred size is (0, 0) so there is nothing to paint.
You need to override the getPreferredSize()
method of the panel when you do custom painting to return the size of your component so layout managers can do their job.
However, as already mention there is no need to create a custom class as you can just use a JLabel
to display an image. The only time you do custom painting is when you need to somehow modify the image when it is painted.
Upvotes: 2