I am using a custom subclass of JPanel to offer me more control over the display of some images. The code to it is below.
However, in Netbeans, in design mode, I would like to be able to see the image that I am working with, instead of simply looking at the outline of the object.
There is an image attribute, but the only way I can currently set the image is to inject custom code into the image attribute.
However, I am unsure what code I need to include in order to view the image, or if there is a simpler way of doing it.
import javax.swing.*;
import java.awt.*;
public class ImagePanel extends JPanel {
private Image img;
public void setImage(String img) {
this.img = new ImageIcon(img).getImage();//setImage(new ImageIcon(img).getImage());
}
public void setImage(Image img) {
int width = this.getWidth();
int height = (int) (((double) img.getHeight(null) / img.getWidth(null)) * width);
this.img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
alt text http://img691.imageshack.us/img691/2128/tempscreen.png
Change:
To:
Rebuild, and set the ImageName property in the designer. In the original code, the designer thinks that the bean property Image is of type java.awt.Image, rather than String. It does not know how to specify a java.awt.Image at design time to your bean. However it can easily pass Strings to your bean, you just need to give it an unambiguous String bean property (ImageName).