Advertisement
lamaulfarid

ImagePanel

Dec 9th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.image.*;
  5.  
  6. /**
  7.  * An ImagePanel is a Swing component that can display an OFImage.
  8.  *
  9.  * @author Ahmad Lamaul Farid
  10.  * @version 9 Desember 2020
  11.  */
  12.  
  13. public class ImagePanel extends JComponent
  14. {
  15.     private int width, height;
  16.     private OFImage panelImage;
  17.  
  18.     public ImagePanel()
  19.     {
  20.         width = 360;
  21.         height = 240;
  22.         panelImage = null;
  23.     }
  24.  
  25.     public void setImage(OFImage image)
  26.     {
  27.         if (image != null)
  28.         {
  29.             width = image.getWidth();
  30.             height = image.getHeight();
  31.             panelImage = image;
  32.             repaint();
  33.         }
  34.     }
  35.  
  36.     public void clearImage()
  37.     {
  38.         Graphics imageGraphics = panelImage.getGraphics();
  39.         imageGraphics.setColor(Color.LIGHT_GRAY);
  40.         imageGraphics.fillRect(0, 0, width, height);
  41.         repaint();
  42.     }
  43.  
  44.     public Dimension getPreferredSize()
  45.     {
  46.         return new Dimension(width, height);
  47.     }
  48.  
  49.     public void paintComponent(Graphics g)
  50.     {
  51.         Dimension size = getSize();
  52.         g.clearRect(0, 0, size.width, size.height);
  53.         if (panelImage != null)
  54.         {
  55.             g.drawImage(panelImage, 0, 0, null);
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement