의 사용을 만들기 배경
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class BackgroundPane extends JPanel {
private BufferedImage img;
private BufferedImage scaled;
public BackgroundPane() {
}
@Override
public Dimension getPreferredSize() {
return img == null ? super.getPreferredSize() : new Dimension(img.getWidth(), img.getHeight());
}
public void setBackground(BufferedImage value) {
if (value != img) {
this.img = value;
repaint();
}
}
@Override
public void invalidate() {
super.invalidate();
if (getWidth() > img.getWidth() || getHeight() > img.getHeight()) {
scaled = getScaledInstanceToFill(img, getSize());
} else {
scaled = img;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (scaled != null) {
int x = (getWidth() - scaled.getWidth())/2;
int y = (getHeight() - scaled.getHeight())/2;
g.drawImage(scaled, x, y, this);
}
}
public static BufferedImage getScaledInstanceToFill(BufferedImage img, Dimension size) {
double scaleFactor = getScaleFactorToFill(img, size);
return getScaledInstance(img, scaleFactor);
}
public static double getScaleFactorToFill(BufferedImage img, Dimension size) {
double dScale = 1;
if (img != null) {
int imageWidth = img.getWidth();
int imageHeight = img.getHeight();
double dScaleWidth = getScaleFactor(imageWidth, size.width);
double dScaleHeight = getScaleFactor(imageHeight, size.height);
dScale = Math.max(dScaleHeight, dScaleWidth);
}
return dScale;
}
public static double getScaleFactor(int iMasterSize, int iTargetSize) {
double dScale = (double) iTargetSize/(double) iMasterSize;
return dScale;
}
public static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) {
return getScaledInstance(img, dScaleFactor, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
}
protected static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor, Object hint, boolean bHighQuality) {
BufferedImage imgScale = img;
int iImageWidth = (int) Math.round(img.getWidth() * dScaleFactor);
int iImageHeight = (int) Math.round(img.getHeight() * dScaleFactor);
if (dScaleFactor <= 1.0d) {
imgScale = getScaledDownInstance(img, iImageWidth, iImageHeight, hint, bHighQuality);
} else {
imgScale = getScaledUpInstance(img, iImageWidth, iImageHeight, hint, bHighQuality);
}
return imgScale;
}
protected static BufferedImage getScaledDownInstance(BufferedImage img,
int targetWidth,
int targetHeight,
Object hint,
boolean higherQuality) {
int type = (img.getTransparency() == Transparency.OPAQUE)
? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage) img;
if (targetHeight > 0 || targetWidth > 0) {
int w, h;
if (higherQuality) {
// Use multi-step technique: start with original size, then
// scale down in multiple passes with drawImage()
// until the target size is reached
w = img.getWidth();
h = img.getHeight();
} else {
// Use one-step technique: scale directly from original
// size to target size with a single drawImage() call
w = targetWidth;
h = targetHeight;
}
do {
if (higherQuality && w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}
if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}
BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
ret = tmp;
} while (w != targetWidth || h != targetHeight);
} else {
ret = new BufferedImage(1, 1, type);
}
return ret;
}
protected static BufferedImage getScaledUpInstance(BufferedImage img,
int targetWidth,
int targetHeight,
Object hint,
boolean higherQuality) {
int type = BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage) img;
int w, h;
if (higherQuality) {
// Use multi-step technique: start with original size, then
// scale down in multiple passes with drawImage()
// until the target size is reached
w = img.getWidth();
h = img.getHeight();
} else {
// Use one-step technique: scale directly from original
// size to target size with a single drawImage() call
w = targetWidth;
h = targetHeight;
}
do {
if (higherQuality && w < targetWidth) {
w *= 2;
if (w > targetWidth) {
w = targetWidth;
}
}
if (higherQuality && h < targetHeight) {
h *= 2;
if (h > targetHeight) {
h = targetHeight;
}
}
BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
ret = tmp;
tmp = null;
} while (w != targetWidth || h != targetHeight);
return ret;
}
}
로 Image
그림의 JPanel
할 수는 말했다와 패널
BufferedImage backgroundImage = ImageIO.read(...);
BackgroundPane backgroundPane = new BackgroundPane();
backgroundPane.setBackground(backgroundImage);
패널, 배경 이미지, 레이블 및 버튼
,
backgroundPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
backgroundPane.add(new JLabel("This is a label"), gbc);
backgroundPane.add(new JButton("This is a button"), gbc);
는 대부분의 구성 요소는 기본적으로 불투명 기억, 그래서 당신은 할 setOpaque(false)
을 사용해야 할 수도 있습니다 그들 당신에 패널을 추가해야합니다, 어떤 점에서
(JLabel
는 예외 중 하나입니다) 투명 이것은 (JFrame
과 같은) 창 기반 클래스입니다. 왜냐하면이 클래스가 표시 할 수있는 유일한 방법이기 때문입니다.
[기본 질문에 대한 두 가지 예제가 있습니다.] (http://stackoverflow.com/questions/22162398/how-to- set-a-background-picture-in-jpanel/22162430 # 22162430) 그런 다음 컨테이너에 표시 할 항목을 추가해야합니다. 'null' 레이아웃에주의 할 것입니다. – MadProgrammer
배경 이미지의 기본 기능을'JLabel' 또는'JPanel'에 캡슐화하여 조심스럽게 답변을 읽어주십시오. - 다음은'JFrame' 내에 표시됩니다. 하나도없는 UI를 표시 할 수 없습니다. 불가능합니다. 그러나 [국경없는 창] (https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html)을 만들면 [this] (http : // stackoverflow)와 같은 작업을 수행 할 수 있습니다. com/questions/11703794/how-to-set-jframe-background-transparent-but-jpanel-or-jlabel-background-opaque/11705029 # 11705029) – MadProgrammer
또는 [this] (http://stackoverflow.com/questions/) 13557261/why-are-graphics-not-j-inframe/13557495 # 13557495) – MadProgrammer