import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
public class ImagePanel extends JComponent
{
private int width;
private int height;
private OFImage panelImage;
/**
* Constructor for objects of class ImagePanel
*/
public ImagePanel()
{
width = 160;
height = 240;
panelImage = null;
}
public void setImage(OFImage image)
{
if(image != null)
{
width = image.getWidth();
height = image.getHeight();
panelImage = image;
repaint();
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void clearImage()
{
Graphics imageGraphics = panelImage.getGraphics();
imageGraphics.setColor(Color.LIGHT_GRAY);
imageGraphics.fillRect(0, 0, width, height);
repaint();
}
public Dimension getPreferedSize()
{
return new Dimension(width, height);
}
public void paintComponent(Graphics g)
{
Dimension size = getSize();
g.clearRect(0, 0, size.width, size.height);
if(panelImage != null)
{
g.drawImage(panelImage, 0, 0, null);
}
}
}