How to create an Image Panel and use it on NetBeans?
I have a class which simply extends JPanel and overrides the paintComponent method as such:
@Override
public void paintComponent(Graphics g)
{
Image img = new ImageIcon("Images/Background1.jpg").getImage();
g.drawImage(img,开发者_运维百科 0, 0, null);
}
I added it to the NetBeans pallet but the background image is not displayed neither on the UI editor nor when I run the program through NetBeans. What am I doing wrong? I would love to have this image panel on the pallet.
If you are drawing the image at location (0, 0) and not scaling the image to fit the window, then there is no reason to do custom painting. Just create an ImageIcon using the image and add the image to a JLabel and add the label to the frame.
The probable problem with doing your custom painting is that you haven't given the component a preferred size so the size of the panel is 0 and there is nothing to paint.
Unless you are doing scaling or other Graphics functions on the image keep it simple and use a JLabel and it will manage the painting and the sizing for you.
See this example from Chee K. Yap:
// MyPanel.java
// -- continuing the example of EmptyFrame1.java
/**
* Our main goal is to add panels to a frame.
* In swing, panels are extensions of JPanels, and the main
* method we need to override is the "paintComponent" method:
*
* class MyPanel extends JPanel {
* // override the paintComponent method
* public void paintComponent(Graphics g) {
* super.paintComponent(g); // calls the default method first!
* g.drawString("Hello Panel!", 75, 100); // our own renderings...
* } //paintComponent
* } //class MyPanel
*
* A JFrame has several layers, but the main one for
* adding components is called "content pane".
* We need to get this pane:
* Container contentPane = frame.getContentPane();
* Then add various components to it. In the present
* example, we add a JPanel:
* contentPane.add( new MyPanel());
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// text panel
class textPanel extends JPanel {
// override the paintComponent method
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello Panel!", 75, 100);
} //paintComponent
} //class textPanel
class MyFrame extends JFrame {
public MyFrame(String s) {
// Frame Parameters
setTitle(s);
setSize(300,200); // default size is 0,0
setLocation(10,200); // default is 0,0 (top left corner)
// Window Listeners
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0); // exit is still needed after dispose()!
} //windowClosing
}); //addWindowLister
// Add Panels
Container contentPane = getContentPane();
contentPane.add(new textPanel());
contentPane.setBackground(Color.pink);
} //constructor MyFrame
} //class MyFrame
public class MyPanel {
public static void main(String[] args) {
JFrame f = new MyFrame("My Hello Panel!");
f.setVisible(true); // equivalent to f.show()!
} //main
} //class MyPanel
/* NOTES:
WindowAdapter() is class that implements WindowListers
with null methods for all the 7 methods of WindowListeners!
It is found in java.awt.event.*.
*/
精彩评论