Sunday, June 23, 2013

Adding an Image as background in Netbeans


Adding an Image as background is little different as no function is available for that.
So what you need is Custom Code.You can add an Image as background
using net beans while using features of Net beans
So here is my Code
Note
1)    Image in background will be visible only at run time not design time
2)    I have Skipped some Part of Code So that you could Understand the Code easily
Add A New Frame to you Application make Changes according to given Code

File :Main Frame.class


import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

class backImage extends JComponent {

Image i;

public backImage(Image i) {
this.i = i;

}

@Override
public void paintComponent(Graphics g) {
g.drawImage(i, 0, 0, null);
}
}

public class MainFrame extends javax.swing.JFrame {
  
    public MainFrame() throws IOException {
          BufferedImage bf = ImageIO.read(new File("E:/bohemia.jpg"));
this.setContentPane(new backImage(bf));
        initComponents();
    }

    public static void main(String args[]) throws IOException {
    
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new MainFrame().setVisible(true);
                } catch (IOException ex) {
                    System.out.print(e.getMessage());
                }
               
            }
        });
    }
                  
}

Result:
 














No comments:

Post a Comment