zoukankan      html  css  js  c++  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);
        }
    }
    
  • 相关阅读:
    超级英雄Hero
    2019 CSP-J 游记(CQ LNBS考场 的退役之战)
    一文梳理Web存储,从cookie,WebStorage到IndexedDB
    一文梳理同源策略与跨域技术
    一文梳理JavaScript中的this
    一文梳理JavaScript 事件循环(Event Loop)
    简单梳理JavaScript垃圾回收机制
    一文梳理JS事件
    src与href
    深入理解CSS定位—浮动模型
  • 原文地址:https://www.cnblogs.com/elnino/p/4816155.html
Copyright © 2011-2022 走看看