Guest User

Untitled

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