views:

662

answers:

3

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

A: 
new ImageIcon("/path/to/your/image.jpg").getImage();
Bozho
I included the necessary libraries, but I get a run time error...Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be non-zero at java.awt.image.ReplicateScaleFilter.<init>(ReplicateScaleFilter.java:85) at java.awt.image.AreaAveragingScaleFilter.<init>(AreaAveragingScaleFilter.java:60) at java.awt.Image.getScaledInstance(Image.java:154) at Pokertable.ImagePanel.setImage(ImagePanel.java:17)
Allen
this.img = img.getScaledInstance(width, height, image.SCALE_SMOOTH) is the line of code being called that is apparently creating the issue.
Allen
have you imported everything?
Bozho
it looks like it's recognizing everything correctly, now, but the new issue appears to be a matter of initialization
Allen
I'm just trying to get the image to show up in design mode, but i cannot manually edit the code so i have to use the custom code as you can see in the image i included. the answers i've received so far don't work. any ideas?
Allen
why do you need it in design mode? does it work at runtime?
Bozho
i want to be able to see the image in design mode, so that i place things on top of it properly.and yes, i get a run time error, see the comments above
Allen
well, debug it. put a breakpoint and see what and why is 0
Bozho
A: 

Another way to do it is using the toolkit, but of course you're doing an application, not an applet, right?

URL url = new URL(this.getCodeBase(), "/path/to/image/here.gif");
temp = ImageIO.read(url);

I had issues with the ImageIcon stuff too.

piggles
i need something to put into the custom code as a parameter to the call to setImage, as you can see in the image above
Allen
+1  A: 

Change:

public void setImage(String img) {

To:

public void setImageName( String img ) {

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).

GalaJon
I cleaned and did a rebuild, but the ImagePanel object does not have the ImageName property available for modification in design view.The 'image' value still has the custom code from the other guy's suggestion, and there is a 'name' property, set to null, but no ImageName property.
Allen