RickyS20_

Untitled

Nov 23rd, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1.  
  2. /**
  3.  * An ImagePanel is a Swing component that can display an OFImage.
  4.  * It is constructed as a subclass of JComponent with the added functionality
  5.  * of setting an OFImage that will be displayed on the surface of this
  6.  * component.
  7.  *
  8.  * @author (Ricky S)
  9.  * @version (1.0)
  10.  */
  11.  
  12. import java.awt.*;
  13. import javax.swing.*;
  14. import java.awt.image.*;
  15.  
  16.  
  17. public class ImagePanel extends JComponent
  18. {
  19.     // The current width and height of this panel
  20.     private int width, height;
  21.     // An internal image buffer that is used for painting. For
  22.     // actual display, this image buffer is then copied to screen.
  23.     private OFImage panelImage;
  24.     /**
  25.        * Create a new, empty ImagePanel.
  26.          */
  27.         public ImagePanel()
  28.         {
  29.             width = 360; // arbitrary size for empty panel
  30.             height = 240;
  31.             panelImage = null;
  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. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment