java虫子碉堡作战_一个碉堡的swing类

今天遇到一个掉了一笔的swing类。可以完美的解决JFrame下设置背景的问题。任意size。太掉了。特记于此

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值