trizehn

ImagePanel

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