document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * An ImagePanel is a Swing component that can display an OFImage.
  3.  * It is constructed as a subclass of JComponent with the added functionality
  4.  * of setting an OFImage that will be displayed on the surface of this
  5.  * component.
  6.  *
  7.  * @author David Ralphwaldo Martuaraja
  8.  * @version 7 Desember 2020
  9.  */
  10.  
  11. import java.awt.*;
  12. import javax.swing.*;
  13. import java.awt.image.*;
  14.  
  15. public class ImagePanel extends JComponent
  16. {
  17.     private int width, height;
  18.     private OFImage panelImage;
  19.    
  20.     public ImagePanel()
  21.     {
  22.         width = 360;
  23.         height = 240;
  24.         panelImage = null;
  25.     }
  26.    
  27.     public void setImage(OFImage image)
  28.     {
  29.         if (image != null)
  30.         {
  31.             width = image.getWidth();
  32.             height = image.getHeight();
  33.             panelImage = image;
  34.             repaint();
  35.         }
  36.     }
  37.  
  38.     public void clearImage()
  39.     {
  40.         Graphics imageGraphics = panelImage.getGraphics();
  41.         imageGraphics.setColor(Color.LIGHT_GRAY);
  42.         imageGraphics.fillRect(0, 0, width, height);
  43.         repaint();
  44.     }
  45.    
  46.     public Dimension getPreferredSize()
  47.     {
  48.         return new Dimension(width, height);
  49.     }
  50.    
  51.     public void paintComponent(Graphics g)
  52.     {
  53.         Dimension size = getSize();
  54.         g.clearRect(0, 0, size.width, size.height);
  55.        
  56.         if (panelImage != null) g.drawImage(panelImage, 0, 0, null);
  57.     }
  58. }
  59.  
');