Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //thanks to
- //http://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel
- //and a little edition
- //PicPanel.java
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JPanel;
- /**
- *
- * @author user
- */
- public class PicPanel extends JPanel
- {
- private BufferedImage image;
- private int jw, jh;
- public PicPanel(String fname, int image_width, int image_height,
- int jpanel_width, int jpanel_height){
- setBounds(0,0,image_width,image_height);
- jw = jpanel_width;
- jh = jpanel_height;
- try
- {
- image = ImageIO.read(new File(fname));
- }
- catch (IOException ioe)
- {
- System.out.println(ioe.toString());
- System.out.println("Could not read in the pic");
- }
- }
- @Override
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- g.drawImage(image,0,0,jw,jh,this);
- }
- }
- //anywhere on code
- PicPanel mainPanel = new PicPanel("disk2.png", 256, 256, 180, 180); //image , image width, image height, jpanel width, jpanel height
- add(mainPanel);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement