如何设置Icon的图片大小

Pol 2007-11-13 04:31:17
通过下面的代码可以显示一张图片
Icon logoImage=new ImageIcon(".\\photo\\cb2.jpg");
lblPhoto=new JLabel(logoImage);
不过显示的图片是原来图片的大小,我想在程序中改变图片的大小,
Icon , ImageIcon都没有改变大小的方法,
应该做?
...全文
2401 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wubo520 2008-05-15
  • 打赏
  • 举报
回复
MM发的贴就是 不一样
Pol 2007-11-15
  • 打赏
  • 举报
回复
不过什么意思都不懂..
Pol 2007-11-15
  • 打赏
  • 举报
回复
..chenweionline您真的好厉害了,,,这可算弄出来了,呵呵,好高兴.
这是我编译过的程序代码:
import java.awt.*;
import javax.swing.*;
import java.io.IOException;
import java.io.File;
import javax.imageio.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
public class phDemo extends JFrame{
JPanel panel;
JLabel lblPhotoDemo;

public phDemo() {
super("photo demo");
panel=new JPanel();
lblPhotoDemo=new JLabel();
try{
File file=new File( "E:\\练习题\\学生情况管理系统\\java\\photo\\cb1.jpg ");
BufferedImage image = ImageIO.read(file);

int width = 100;
int height = 100;

double scaleWidth = (double) width / (double) image.getWidth();
double scaleHeight = (double) height / (double) image.getHeight();

BufferedImage dstImage = new BufferedImage(width, height, image.getType());

AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(scaleWidth , scaleHeight);
AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform,2);//
affineTransformOp.filter(image, dstImage);

Icon logoImage1 = new ImageIcon(dstImage);
lblPhotoDemo=new JLabel(logoImage1);

}catch(IOException e){
System.out.println(e.getMessage());
}
panel.add(lblPhotoDemo);
this.add(panel);
this.setSize(600,500);
this.show();
}
public static void main(String[] args){
new phDemo();
}
}
liujun999999 2007-11-14
  • 打赏
  • 举报
回复
其实缩放完全可以不用AffineTransform
Graphics类中有一个drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) 可以实现缩放的功能,可以把图片平铺在一个Buffer上面
chenweionline 2007-11-14
  • 打赏
  • 举报
回复
上面2行写错了,width和height直接赋值100
int width = 100;
int height = 100;
chenweionline 2007-11-14
  • 打赏
  • 举报
回复
随便写下的,也没太仔细看,ImageIO.read()的确没有String类型参数,你为图片新建一个File就可以了。
ImageIO.read(new File("xxx.jpg"));

你需要 100*100 大小的缩略图,可以做一个换算。

int width = 100(int)(image .getWidth() * scale );
int height = 100(int)(image .getHeight() * scale );

double scaleWidth = (double) width / (double) image.getWidth();
double scaleHeight = (double) height / (double) image.getHeight();

BufferedImage dstImage = new BufferedImage(width, height, image.getType());

AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(scaleWidth , scaleHeight );
affineTransformOp.filter(image, dstImage);
Pol 2007-11-13
  • 打赏
  • 举报
回复
还是有点问题, BufferedImage image = ImageIO.read(".\\photo\\cb2.jpg");
ImageIO.read好象没有String类型.
您看下错误说明嘛:

E:\练习题\学生情况管理系统\java\photoDemo.java:29: 找不到符号
符号: 方法 read(java.lang.String)
位置: 类 javax.imageio.ImageIO
BufferedImage image = ImageIO.read(".\\photo\\cb2.jpg");
^
注意: E:\练习题\学生情况管理系统\java\photoDemo.java 使用或覆盖了已过时的 API。
注意: 要了解详细信息,请使用 -Xlint:deprecation 重新编译。

还有,我发现这样也不能弄成100*100固定大小是不是?,,很麻烦的,不行就不管它算了,
大不了就把空间留大点让它显示了.
Pol 2007-11-13
  • 打赏
  • 举报
回复
``您好很厉害哟,学习中...
chenweionline 2007-11-13
  • 打赏
  • 举报
回复
AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, hints);

hints 是绘制提示,这里可以替换成 null
chenweionline 2007-11-13
  • 打赏
  • 举报
回复
随便写了点,不知道组件有没提供更简单的方法。


// 1 读图
BufferedImage image = ImageIO.read("xxx.jpg");

// 2 图像长宽缩小一半

double scale = 0.5d;

int width = (int)(image .getWidth() * scale );
int height = (int)(image .getHeight() * scale );
BufferedImage dstImage = new BufferedImage(width, height, srcImage.getType());

AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(scale , scale);

AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, hints);

affineTransformOp.filter(image, dstImage);

// 3 实例化 ImageIcon
Icon icon = new ImageIcon(dstImage);
Pol 2007-11-13
  • 打赏
  • 举报
回复
不会做,您说的两种方法,我查帮助弄的头晕都不知道怎么做呀,,
您能不能给我说详细些或者举个例子嘛..谢谢了.
Pol 2007-11-13
  • 打赏
  • 举报
回复
```不懂呀,,我试试看了.
chenweionline 2007-11-13
  • 打赏
  • 举报
回复
可以试试覆写 ImageIcon 的 paintIcon(Component c, Graphics g, int x, int y) 方法,设置 Graphics 的 scale。

或者使用 ImageIO.read() 读取图片,然后缩放图像,再将缩放后的图像做为参数实例化 ImageIcon 。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧