易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Java Swing的背景图片按比例缩放
Java Swing的背景图片按比例缩放
import
java.awt.*;
import
java.awt.image.BufferedImage;
import
javax.swing.Icon;
import
javax.swing.ImageIcon;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
public
class
ScaleIcon
implements
Icon {
private
BufferedImage i =
null
;
private
Icon icon =
null
;
public
ScaleIcon(Icon icon) {
this
.icon = icon;
}
@Override
public
int
getIconHeight() {
return
icon.getIconHeight();
}
@Override
public
int
getIconWidth() {
return
icon.getIconWidth();
}
public
void
paintIcon(Component c, Graphics g,
int
x,
int
y) {
float
wid = c.getWidth();
float
hei = c.getHeight();
int
iconWid = icon.getIconWidth();
int
iconHei = icon.getIconHeight();
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.scale(wid / iconWid, hei / iconHei);
icon.paintIcon(c, g2d,
0
,
0
);
}
public
static
void
main(String[] args) {
ScaleIcon icon =
new
ScaleIcon(
new
ImageIcon(ClassLoader.getSystemResource(
"img/main.jpg"
)));
JLabel label =
new
JLabel(icon);
JFrame frame =
new
JFrame();
frame.getContentPane().add(label, BorderLayout.CENTER);
// frame.getContentPane().add(new JButton("click"),BorderLayout.NORTH);
frame.setSize(
800
,
600
);
frame.setLocationRelativeTo(
null
);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(
true
);
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图