zoukankan      html  css  js  c++  java
  • 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);
        }
    }




  • 相关阅读:
    MySQL数据库的创建&删除&选择
    JS实现异步的几种方式
    十种排序算法实例说明总结
    常用的bug管理工具
    Bootstrap+Hbuilder
    从菜鸟的视角看测试!
    安装numpy和matplotlib
    Eclipse在线安装svn
    重新打个招呼
    <USACO09JAN>气象测量/气象牛The Baric Bovineの思路
  • 原文地址:https://www.cnblogs.com/secbook/p/2655164.html
Copyright © 2011-2022 走看看