Advertisement
reyhanzo

Untitled

Dec 15th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1.  import java.awt.*;  
  2.  import javax.swing.*;  
  3.  import java.awt.image.*;  
  4.  /**  
  5.   * An ImagePanel is a Swing component that can display an OFImage.  
  6.   * It is constructed as a subclass of JComponent with the added functionality  
  7.   * of setting an OFImage that will be displayed on the surface of this  
  8.   * component.  
  9.   *  
  10.   * @author Michael Kolling and David J. Barnes  
  11.   * @version 1.0  
  12.   */  
  13.  public class ImagePanel extends JComponent  
  14.  {  
  15.    // The current width and height of this panel  
  16.    private int width, height;  
  17.    // An internal image buffer that is used for painting. For  
  18.    // actual display, this image buffer is then copied to screen.  
  19.    private OFImage panelImage;  
  20.    /**  
  21.     * Create a new, empty ImagePanel.  
  22.     */  
  23.    public ImagePanel()  
  24.    {  
  25.      width = 360;  // arbitrary size for empty panel  
  26.      height = 240;  
  27.      panelImage = null;  
  28.    }  
  29.    /**  
  30.     * Set the image that this panel should show.  
  31.     *  
  32.     * @param image The image to be displayed.  
  33.     */  
  34.    public void setImage(OFImage image)  
  35.    {  
  36.      if(image != null) {  
  37.        width = image.getWidth();  
  38.        height = image.getHeight();  
  39.        panelImage = image;  
  40.        repaint();  
  41.      }  
  42.    }  
  43.    /**  
  44.     * Clear the image on this panel.  
  45.     */  
  46.    public void clearImage()  
  47.    {  
  48.      Graphics imageGraphics = panelImage.getGraphics();  
  49.      imageGraphics.setColor(Color.LIGHT_GRAY);  
  50.      imageGraphics.fillRect(0, 0, width, height);  
  51.      repaint();  
  52.    }  
  53.    // The following methods are redefinitions of methods  
  54.    // inherited from superclasses.  
  55.    /**  
  56.     * Tell the layout manager how big we would like to be.  
  57.     * (This method gets called by layout managers for placing  
  58.     * the components.)  
  59.     *  
  60.     * @return The preferred dimension for this component.  
  61.     */  
  62.    public Dimension getPreferredSize()  
  63.    {  
  64.      return new Dimension(width, height);  
  65.    }  
  66.    /**  
  67.     * This component needs to be redisplayed. Copy the internal image  
  68.     * to screen. (This method gets called by the Swing screen painter  
  69.     * every time it want this component displayed.)  
  70.     *  
  71.     * @param g The graphics context that can be used to draw on this component.  
  72.     */  
  73.    public void paintComponent(Graphics g)  
  74.    {  
  75.      Dimension size = getSize();  
  76.      g.clearRect(0, 0, size.width, size.height);  
  77.      if(panelImage != null) {  
  78.        g.drawImage(panelImage, 0, 0, null);  
  79.      }  
  80.    }  
  81.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement