kuchuz

PBO-C 7 : ImagePanel

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