Advertisement
mnaufaldillah

ImagePanel Tugas 7

Dec 6th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 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 Muhammad Naufaldillah
  11.  * @version 5 December 2020
  12.  */
  13. public class ImagePanel extends JComponent
  14. {
  15.     // The current width and height of this panel
  16.     private int width, height;
  17.    
  18.     // An internal image buffer that is used for painting. For
  19.     // actual display, this image buffer is then copied to screen.
  20.     private OFImage panelImage;
  21.    
  22.     /**
  23.      * Create a new, empty ImagePanel.
  24.      */
  25.     public ImagePanel()
  26.     {
  27.         width = 360;    // arbitrary size for empty panel
  28.         height = 240;
  29.         panelImage = null;
  30.     }
  31.    
  32.     /**
  33.      * Set the image that this panel should show.
  34.      *
  35.      * @param image The image to be displayed.
  36.      */
  37.     public void setImage(OFImage image)
  38.     {
  39.         if(image != null)
  40.         {
  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 getPrefereedSize()
  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.         {
  87.             g.drawImage(panelImage, 0, 0, null);
  88.         }
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement