document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.image.*;
  4.  
  5. /**
  6.  * Class ini berisi komponen Swing yang dapat menampilkan OFImage.
  7.  * Dibuat sebagai subclass JComponent untuk fungsionalitas pengaturan OFImage.
  8.  *
  9.  * @author M.Fajri Davyza Chaniago
  10.  * @version 7 Desember 2020
  11.  */
  12.  
  13. public class ImagePanel extends JComponent
  14. {
  15.     // Lebar dan tinggi panel saat ini
  16.     private int width, height;
  17.    
  18.     // Internal image buffer untuk painting, ditampilkan di screen.
  19.     private OFImage panelImage;
  20.    
  21.     public ImagePanel()
  22.     {
  23.         width = 360; // Ukuran panel kosongan bebas
  24.         height = 240;
  25.         panelImage = null;
  26.     }
  27.    
  28.     public void setImage(OFImage image)
  29.     {
  30.         if (image != null)
  31.         {
  32.             width = image.getWidth();
  33.             height = image.getHeight();
  34.             panelImage = image;
  35.             repaint();
  36.         }
  37.     }
  38.    
  39.     public void clearImage()
  40.     {
  41.         Graphics imageGraphics = panelImage.getGraphics();
  42.         imageGraphics.setColor(Color.LIGHT_GRAY);
  43.         imageGraphics.fillRect(0, 0, width, height);
  44.         repaint();
  45.     }
  46.    
  47.     // Method di bawah untuk mendefinisikan ulang method warisan dari superclass.
  48.    
  49.     public Dimension getPreferredSize()
  50.     {
  51.         return new Dimension(width, height);
  52.     }
  53.    
  54.     public void paintComponent(Graphics g)
  55.     {
  56.         Dimension size = getSize();
  57.         g.clearRect(0, 0, size.width, size.height);
  58.        
  59.         if (panelImage != null)
  60.         {
  61.             g.drawImage(panelImage, 0, 0, null);
  62.         }
  63.     }
  64. }
');