gpt4 book ai didi

java - 如何使用自定义值重新调整 png 文件的宽度和高度?

转载 作者:行者123 更新时间:2023-11-30 04:44:01 25 4
gpt4 key购买 nike

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

最佳答案

为什么不在您的 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);
}
}

}

关于java - 如何使用自定义值重新调整 png 文件的宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11528564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com