主题:为什么图像大小没变
tgnian
[专家分:100] 发布于 2007-12-08 12:32:00
File imagefile=new File("paste.png");
try
{
BufferedImage bufferedImage = ImageIO.read(imagefile);
bufferedImage.getScaledInstance(16,16,BufferedImage.SCALE_FAST);
ImageIcon imageicon=new ImageIcon(bufferedImage);
jmuEdit.add(jmuiPaste=new JMenuItem("Paste",imageicon));
} catch (IOException e)
{
e.printStackTrace();
}
为什么图像大小没变
回复列表 (共2个回复)
沙发
SwingDesigner [专家分:180] 发布于 2007-12-10 11:01:00
稍微改动一下就可以显示所要大小的图片了。
import java.io.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
public class Test {
public Test() {}
public static void main(String[] args) {
JFrame frame = new JFrame();
Container cont = frame.getContentPane();
JMenu jmuEdit = new JMenu("edit");
JMenuItem jmuiPaste;
File imagefile = new File("f:/menu.gif");
try {
BufferedImage bufferedImage = ImageIO.read(imagefile);
//此处的参数应为BufferedImage.SCALE_SMOOTH
Image image = new ImageIcon(bufferedImage).getImage().
getScaledInstanc(16,16,BufferedImage.SCALE_SMOOTH);
ImageIcon imageicon = new ImageIcon(image);
jmuEdit.add(jmuiPaste = new JMenuItem("Paste", imageicon));
} catch (IOException e) {}
JMenuBar mbar = new JMenuBar();
mbar.add(jmuEdit);
frame.setJMenuBar(mbar);
frame.setBounds(400, 400, 400, 300);
frame.setVisible(true);
}
}
板凳
tgnian [专家分:100] 发布于 2007-12-16 22:30:00
try
{
File imagefile=new File("paste.png");
BufferedImage bufferedImage = ImageIO.read(imagefile);
// bufferedImage.getScaledInstance(16,16,BufferedImage.SCALE_FAST);
Image changeimage = new ImageIcon(bufferedImage).getImage().
getScaledInstance(16,16,BufferedImage.SCALE_FAST);
ImageIcon imageicon=new ImageIcon(changeimage);
jmuEdit.add(jmuiPaste=new JMenuItem("粘贴(P)",imageicon));
} catch (IOException e)
{
e.printStackTrace();
}
sorry,我试过用BufferedImage.SCALE_FAST也行,我认为可能是ImageIcon imageicon=new ImageIcon(changeimage);这句我原来的参数类型是BufferedImage 或bufferedImage.getScaledInstance(16,16,BufferedImage.SCALE_FAST);没达到改变大小的效果 ,我也不懂,希望大家赐教,说说自己的看法
我来回复