Advertisement
Ragnard-Kiseki-

BackgroundImage

Oct 2nd, 2021 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. package finalactivities;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8. import javax.imageio.ImageIO;
  9. import javax.swing.JPanel;
  10.  
  11. public class BackgroundImage extends JPanel {
  12.  
  13.       /**
  14.      *
  15.      */
  16.     private static final long serialVersionUID = 1L;
  17.     private Image backgroundImage;
  18.  
  19.       // Some code to initialize the background image.
  20.       // Here, we use the constructor to load the image. This
  21.       // can vary depending on the use case of the panel.
  22.       public BackgroundImage(String fileName) throws IOException {
  23.         backgroundImage = ImageIO.read(new File(fileName));
  24.       }
  25.  
  26.       public void paintComponent(Graphics g) {
  27.         super.paintComponent(g);
  28.  
  29.         // Draw the background image.
  30.         g.drawImage(backgroundImage, 0, 0, this.getWidth(), this.getHeight(), null);
  31.       }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement