1

我正在加载一个 png 格式的按钮图片。但是格式太大了。如何将其宽度和高度调整到定义的范围,但避免使用图像大小。

前:

 public JButton createButton(String name, String toolTip) {
    Image a = null;
    try {
      a = ImageIO.read((InputStream) Test.class.getResourceAsStream("/image/menu/" + name + ".png"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    ImageIcon iconRollover = new ImageIcon(a);        
    int w = iconRollover.getIconWidth();
    int h = iconRollover.getIconHeight();

    // get the cursor for this button
    Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

    // make translucent default image
    //Image image = screen.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics2D g = (Graphics2D) a.getGraphics();
    Composite alpha = AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, .5f);
    g.setComposite(alpha);
    g.drawImage(iconRollover.getImage(), 0, 0, null);
    g.dispose();

    ImageIcon iconDefault = new ImageIcon(a);
    ImageIcon iconPressed = new ImageIcon(a);

    // create the button
    JButton button = new JButton();
    button.setIgnoreRepaint(true);
    button.setFocusable(false);
    button.setToolTipText(toolTip);
    button.setBorder(null);
    button.setContentAreaFilled(false);
    button.setCursor(cursor);
    button.setIcon(iconDefault);
    button.setRolloverIcon(iconRollover);
    button.setPressedIcon(iconPressed);
    return button;
  }

后:

跟进:

  public JButton createButton(String name, String toolTip) {

    // Create image
    BufferedImage a = null;
    try {
      a = ImageIO.read((InputStream) P2P.class.getResourceAsStream("/image/menu/" + name + ".png"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    BufferedImage bi = new BufferedImage(70, 70, BufferedImage.TRANSLUCENT);

    Graphics2D g = (Graphics2D) bi.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(a, 0, 0, 70, 70, null);
    g.dispose();   
    // get the cursor for this button
    Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    ImageIcon iconRollover = new ImageIcon(bi);
    ImageIcon iconDefault = new ImageIcon(bi);
    ImageIcon iconPressed = new ImageIcon(bi);

    // create the button
    JButton button = new JButton();
    //button.addActionListener(this);
    button.setIgnoreRepaint(true);
    button.setFocusable(false);
    button.setToolTipText(toolTip);
    button.setBorder(null);
    button.setContentAreaFilled(false);
    button.setCursor(cursor);
    button.setIcon(iconDefault);
    button.setRolloverIcon(iconRollover);
    button.setPressedIcon(iconPressed);
    return button;
  }
4

3 回答 3

1

为什么不在getScaledInstance()您的 Image 实例上使用该方法,如下所示:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

static int width=300;//change this to your wanted size
static int height =500;

public class Main extends JFrame implements ActionListener {
  Image img;

  JButton getPictureButton  = new JButton("Get Picture");

  public static void main(String[] args) {
    new Main();
  }

  public Main() {
    this.setSize(600, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel picPanel = new PicturePanel();
    this.add(picPanel, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    getPictureButton.addActionListener(this);
    buttonPanel.add(getPictureButton);
    this.add(buttonPanel, BorderLayout.SOUTH);

    this.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    String file = "a.png";
    if (file != null) {
      Toolkit kit = Toolkit.getDefaultToolkit();
      img = kit.getImage(file);

      img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);//scale the image to wanted size

      this.repaint();
    }
  }
  class PicturePanel extends JPanel {
    public void paintComponent(Graphics g) {
      g.drawImage(img, 0, 0, this);
    }
  }

}
于 2012-07-17T18:28:10.457 回答
1
/** 
 * @param img image (Image or ImageIcon)
 * @param w the width of the returned image 
 * @param h the height of the returned image 
 * @param imgW the width of the graphics in the returned image 
 * @param imgH the height of the graphics in the returned image 
 * @return ImageIcon
*/
public static ImageIcon resizeImage(Object img, Integer w, Integer h, Integer imgW, Integer imgH) {
    Image image = objectToImage(img);
    if(w == null)
        w=image.getWidth(null);
    if(h == null)
        h=image.getHeight(null);
    if(imgW == null) 
        imgW=w;
    else if(imgH == null)
        imgH=imgW;
    if(imgH == null)
        imgH=h;

    BufferedImage resizedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = resizedImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.drawImage(toBufferedImage(image), (w-imgW)/2, (h-imgH)/2, imgW, imgH, null);
    g2d.dispose();
    return new ImageIcon(resizedImage);
}

public static Image objectToImage(Object img) {
    if(img instanceof ImageIcon)
        return ((ImageIcon)img).getImage();
    else if(img instanceof Image)
        return (Image)img;
    else
        throw new ClassCastException();
}

public static BufferedImage toBufferedImage(Image img) { 
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();
    return bi;
}
于 2014-08-01T00:39:13.867 回答
0

作品

  public JButton createButton(String name, String toolTip) {

    // Create image
    BufferedImage a = null;
    try {
      a = ImageIO.read((InputStream) P2P.class.getResourceAsStream("/image/menu/" + name + ".png"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    BufferedImage bi = new BufferedImage(70, 70, BufferedImage.TRANSLUCENT);

    Graphics2D g = (Graphics2D) bi.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(a, 0, 0, 70, 70, null);
    g.dispose();   
    // get the cursor for this button
    Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    ImageIcon iconRollover = new ImageIcon(bi);
    ImageIcon iconDefault = new ImageIcon(bi);
    ImageIcon iconPressed = new ImageIcon(bi);

    // create the button
    JButton button = new JButton();
    //button.addActionListener(this);
    button.setIgnoreRepaint(true);
    button.setFocusable(false);
    button.setToolTipText(toolTip);
    button.setBorder(null);
    button.setContentAreaFilled(false);
    button.setCursor(cursor);
    button.setIcon(iconDefault);
    button.setRolloverIcon(iconRollover);
    button.setPressedIcon(iconPressed);
    return button;
  }
于 2012-07-18T12:29:29.993 回答