【问题标题】:How can i re-size the width and height of a png file with custom defined value?如何使用自定义值重新调整 png 文件的宽度和高度?
【发布时间】:2014-08-01 21:51:02
【问题描述】:

我正在加载一个 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;
  }

【问题讨论】:

标签: java swing


【解决方案1】:

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

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);
    }
  }

}

【讨论】:

  • 如果我使用 JPanel 对象,我可以使用 JButton 的方法吗?例如:button.setCursor(cursor); button.setIcon(iconDefault); button.setRolloverIcon(iconRollover); button.setPressedIcon(iconPressed);
  • "Swing 程序应该覆盖paintComponent(),而不是覆盖paint()。"—Painting in AWT and Swing: The Paint Methods
  • @YumYumYum 你是什么意思?当然,JPanel 不会拥有与JButton 相同的方法,尤其是那些。除了调整图像大小之外,您还试图做什么
  • @DavidKroukamp:使用 JButton 调整图像大小 + 光标 + 按下 + 翻转方法。就像垃圾神提到的那样:stackoverflow.com/a/6916719/285594
【解决方案2】:
/** 
 * @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;
}

【讨论】:

    【解决方案3】:

    作品

      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-09-13
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2015-07-16
      • 2019-04-10
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多