Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. import java.awt.image.*;  
  4.    
  5.  public class ImagePanel extends JComponent  
  6.  {  
  7.    private int width, height;  
  8.    
  9.    private OF panelImage;  
  10.    /**  
  11.     * Create a new, empty ImagePanel.  
  12.     */  
  13.    public ImagePanel()  
  14.    {  
  15.      width = 360;  
  16.      height = 240;  
  17.      panelImage = null;  
  18.    }  
  19.    /**  
  20.     * Set the image that this panel should show.  
  21.     *  
  22.     * @param image The image to be displayed.  
  23.     */  
  24.    public void setImage(OF image)  
  25.    {  
  26.      if(image != null) {  
  27.        width = image.getWidth();  
  28.        height = image.getHeight();  
  29.        panelImage = image;  
  30.        repaint();  
  31.      }  
  32.    }  
  33.    /**  
  34.     * Clear the image on this panel.  
  35.     */  
  36.    public void clearImage()  
  37.    {  
  38.      Graphics imageGraphics = panelImage.getGraphics();  
  39.      imageGraphics.setColor(Color.LIGHT_GRAY);  
  40.      imageGraphics.fillRect(0, 0, width, height);  
  41.      repaint();  
  42.    }  
  43.    
  44.    /**  
  45.     * Tell the layout manager how big we would like to be.  
  46.     * (This method gets called by layout managers for placing  
  47.     * the components.)  
  48.     *  
  49.     * @return The preferred dimension for this component.  
  50.     */  
  51.    public Dimension getPreferredSize()  
  52.    {  
  53.      return new Dimension(width, height);  
  54.    }  
  55.    /**  
  56.     * This component needs to be redisplayed. Copy the internal image  
  57.     * to screen. (This method gets called by the Swing screen painter  
  58.     * every time it want this component displayed.)  
  59.     *  
  60.     * @param g The graphics context that can be used to draw on this component.  
  61.     */  
  62.    public void paintComponent(Graphics g)  
  63.    {  
  64.      Dimension size = getSize();  
  65.      g.clearRect(0, 0, size.width, size.height);  
  66.      if(panelImage != null) {  
  67.        g.drawImage(panelImage, 0, 0, null);  
  68.      }  
  69.    }  
  70.  }