igede

Untitled

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