public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image,0,0,this); }
public class PrintImage extends Frame{ /** * */ private static final long serialVersionUID = 1564025218977273340L; /** * 读取网页图片 */ private ImageIcon img; public void paint(Graphics g){ g.drawImage(img.getImage(), 20, 20, this);//显示图像 } public void processWindowsEvent(WindowEvent e){ //处理windows窗口事件 super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING){ System.exit(0); } } public static void main(String[] args) throws IOException{ String path = "F:\\m_1259201088627.jpg"; PrintImage urlt = new PrintImage(); urlt.img = new ImageIcon(path); urlt.enableEvents(AWTEvent.WINDOW_EVENT_MASK); urlt.setSize(600,600);//设定窗口的大小 urlt.setVisible(true);//Shows or hides this Window depending on the value of parameter } }
/** * <br>Created on 2010-11-11 * @author Alex.Huang */ package ext.test.jws.gui;import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel;/** * <br>Created on 2010-11-11 * @author Alex.Huang */ public class PanelImageTest extends JPanel { private Image image; public PanelImageTest() { super(); setOpaque(true); image = Toolkit.getDefaultToolkit().getImage( "D:/My Document/My Pictures/Wallpaper Images/NOAA/line0053.jpg"); ; } public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.WHITE); if (image != null) { int height = image.getHeight(this); int width = image.getWidth(this); if (height != -1 && height > getHeight()) height = getHeight(); if (width != -1 && width > getWidth()) width = getWidth(); int x = (int) (((double) (getWidth() - width)) / 2.0); int y = (int) (((double) (getHeight() - height)) / 2.0); g.drawImage(image, x, y, width, height, this); } }
此方法文档上有详细描述。
{
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
/**
*
*/
private static final long serialVersionUID = 1564025218977273340L;
/**
* 读取网页图片
*/
private ImageIcon img;
public void paint(Graphics g){
g.drawImage(img.getImage(), 20, 20, this);//显示图像
}
public void processWindowsEvent(WindowEvent e){
//处理windows窗口事件
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
public static void main(String[] args) throws IOException{
String path = "F:\\m_1259201088627.jpg";
PrintImage urlt = new PrintImage();
urlt.img = new ImageIcon(path);
urlt.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
urlt.setSize(600,600);//设定窗口的大小
urlt.setVisible(true);//Shows or hides this Window depending on the value of parameter
}
}
/**
* <br>Created on 2010-11-11
* @author Alex.Huang
*/
package ext.test.jws.gui;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;/**
* <br>Created on 2010-11-11
* @author Alex.Huang
*/
public class PanelImageTest extends JPanel {
private Image image; public PanelImageTest() {
super();
setOpaque(true); image = Toolkit.getDefaultToolkit().getImage( "D:/My Document/My Pictures/Wallpaper Images/NOAA/line0053.jpg"); ;
} public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
if (image != null) {
int height = image.getHeight(this);
int width = image.getWidth(this); if (height != -1 && height > getHeight())
height = getHeight(); if (width != -1 && width > getWidth())
width = getWidth(); int x = (int) (((double) (getWidth() - width)) / 2.0);
int y = (int) (((double) (getHeight() - height)) / 2.0);
g.drawImage(image, x, y, width, height, this);
}
}
public static void main(String[] args) {
JFrame frame=new JFrame();
PanelImageTest panel=new PanelImageTest();
JButton b=new JButton("按钮");
panel.add(b);
frame.add(panel);
frame.setVisible(true);
}}
现写现测试。主要是扩展一个JPanel然后重写paintComponent(Graphics g)方法,里面画图的算法是适应框体大小的,所以只要改图片就可以用了。
import java.awt.Graphics;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class Background extends JFrame{
ImageIcon image;
JLabel imageLabel;
JPanel backgroundPane;
public Background() {
image = new ImageIcon(Background.class.getClassLoader().getResource("images/background.gif"));
backgroundPane = new JPanel();
imageLabel = new JLabel(image);
backgroundPane.add(imageLabel);
this.add(backgroundPane);
this.setBounds(200, 300, 381, 330);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.drawImage(image.getImage(), 0, 20, this);
}
public static void main(String[] args) {
Background bg = new Background();
}
}图片放到项目的根目录下
import java.awt.Graphics;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class Background extends JFrame {
ImageIcon image; public Background() {
image = new ImageIcon(Background.class.getClassLoader().getResource(
"images/background.gif"));
this.setBounds(200, 300, 381, 330);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void paint(Graphics g) {
g.drawImage(image.getImage(), 0, 20, this);
} public static void main(String[] args) {
Background bg = new Background();
}}
应该是这样