程式碼示例:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class ImgPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Image imgBack;
private boolean backResize;
/**
* 構造方法
* @param arg0 背景圖片 Image 類
* @param arg1 背景圖片是否根據Panel大小自行縮放
*/
public ImgPanel(Image arg0, boolean arg1) {
imgBack = arg0;
backResize = arg1;
}
protected void paintComponent(Graphics g) {
if (backResize) {
g.drawImage(imgBack, 0, 0, getWidth(), getHeight(), null);
} else {
g.drawImage(imgBack, (getWidth()-imgBack.getWidth(null))/2, (getHeight()-imgBack.getHeight(null))/2, null);
程式碼示例:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class ImgPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Image imgBack;
private boolean backResize;
/**
* 構造方法
* @param arg0 背景圖片 Image 類
* @param arg1 背景圖片是否根據Panel大小自行縮放
*/
public ImgPanel(Image arg0, boolean arg1) {
imgBack = arg0;
backResize = arg1;
}
protected void paintComponent(Graphics g) {
if (backResize) {
g.drawImage(imgBack, 0, 0, getWidth(), getHeight(), null);
} else {
g.drawImage(imgBack, (getWidth()-imgBack.getWidth(null))/2, (getHeight()-imgBack.getHeight(null))/2, null);
}
}
}