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);
}
}