JAVA-swing中的图像

Icon

接口类,有以下方法

int getIconHeight();
int getIconWidth();
void paintIcon(Component c, Graphics g, int x, int y);

ImageIcon

构造方法

ImageIcon(byte[] imageData)
ImageIcon(Image image)
ImageIcon(String filename)
ImageIcon(URL location) 

实现Icon的从图像绘制图标。
使用MediaTracker预先从URL,文件名或字节数组中获取创建的图像,实现图像的加载。

Image

是所有图像类的超类,必须以平台特定的方式获取图像。
获得图像

	imageicon.getImage();

参考网址

各个Image的区别

ImageIcon可以通过Image转化过来
Image一般尺寸过大,不适用于所有界面。
Image一般尺寸较大,不适合用作icon(大图片用作icon时只显示图片的一部分),需要经过处理:
Icon是接口,一般不直接用。
ImageIcon用途最广泛。
Image用来绘制图片,ImageIcon用来添加到各类组件中,直接显示要转化为Image。

paintIcon

Icon会自动调用 public void paintIcon(Component c, Graphics g, int x, int y) 方法

图像等比例缩放显示在Label内

package aaa;

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);
        /*图像被隐式地定义为在整数坐标位置上提供颜色样本.当图像没有缩放到目标而垂直呈现时,
		     其图像像素映射到其设备像素的选择是显而易见的,且图像中整数坐标位置的样本一个对一个地被转换到设备像素网格上的相应整数位置。
		     当图像在缩放、旋转或其他转换坐标系中呈现时,图像后面设备像素坐标的映射关系可能引起问题:所提供的图像样本整数位置之间的连续坐标处使用什么颜色样本。
		      插值算法定义了一些函数,它们根据整数坐标周围的颜色样本为图像中的任何连续坐标提供颜色样本。
		      此提示允许的值有
			value_interpolation_nearest_neighbor
			value_interpolation_bilinear
			value_interpolation_bicubic
        */
        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/11.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(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

图片的缩放

	ImageIcon imgicon=new ImageIcon(ClassLoader.getSystemResource("img/11.jpg"));
	//通过类加载器获取图像
	Image img=imgicon.getImage();
	//通过ImageIcon获取Image
	img=img.getScaledInstance(50, 50, img.SCALE_AREA_AVERAGING );
	//前两个参数分别为长和宽,第三个参数为缩放算法,缩放算法有多个,可以查查资料,初学者不用太关心这个。
	ImageIcon im=new ImageIcon(img);



	//简洁点缩行后
	Image img=(new ImageIcon(ClassLoader.getSystemResource("img/11.jpg"))).getImage().getScaledInstance(50,50,Image.SCALE_SMOOTH);

关于Graphics2D

scale
public abstract void scale(double sx,double sy)
使用缩放变换连接当前的Graphics2D Transform后续渲染根据相对于先前缩放比例的指定缩放因子进行调整大小。 这等同于调用transform(S) ,其中S是一个AffineTransform由以下矩阵表示:
[ sx 0 0 ]
[ 0 sy 0 ]
[ 0 0 1 ] 参数
sx - 后续渲染操作中的X坐标相对于以前的渲染操作相乘的量。
sy - 后续渲染操作中的Y坐标相对于以前的渲染操作相乘的量。

猜你喜欢

转载自blog.csdn.net/weixin_43353639/article/details/108491652