gpt4 book ai didi

java - 如何使用 getScaledInstance 缩放图像?

转载 作者:行者123 更新时间:2023-11-29 05:38:59 25 4
gpt4 key购买 nike

我正在尝试使用鼠标运动监听器缩放图像,但它不起作用。所以现在我正在使用 2 个 JTextfields 手动执行此操作。我从 Jtextfields 获取值,然后将该值传递给 getScaledInstance() 方法。但它不起作用。

我的代码:

        final JTextField jj = new JTextField();
jj.setColumns(5);
buttonPane.add(jj);
JButton btn = new JButton("Resize");
final JTextField jj1 = new JTextField();
jj1.setColumns(5);
buttonPane.add(jj1);
buttonPane.add(btn);
btn.addActionListener(new ActionListener() {


@Override
public void actionPerformed(ActionEvent arg0)
{
int x= Integer.parseInt(jj.getText());
int y=Integer.parseInt(jj1.getText());

BufferedImage b = a;
b.getScaledInstance(x, y,BufferedImage.TYPE_INT_ARGB);
label.setIcon(new ImageIcon(b));
}
});

这里的 a 是从栈中弹出的 BufferedImage 实例。labelJLabel

最佳答案

您需要将返回引用分配给某物...

Image scaled = b.getScaledInstance(x, y,BufferedImage.TYPE_INT_ARGB);
label.setIcon(new ImageIcon(scaled));

您可能还想通读 The Perils of Image.getScaledInstance()

鼠标滚轮缩放更新

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MouseScaleTest {

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

public MouseScaleTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private BufferedImage img;

private float scale = 1f;
private float scaleDelta = 0.05f;

public TestPane() {

try {
img = ImageIO.read(new File("/path/to/your/image"));
} catch (IOException ex) {
ex.printStackTrace();
}

addMouseWheelListener(new MouseWheelListener() {

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int rotation = e.getWheelRotation();
if (rotation < 0) {
scale -= scaleDelta;
} else {
scale += scaleDelta;
}
if (scale < 0) {
scale = 0;
} else if (scale > 1) {
scale = 1;
}
repaint();
}
});
}

@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
Graphics2D g2d = (Graphics2D) g.create();

int x = (int)((getWidth() - (img.getWidth() * scale)) / 2);
int y = (int)(getHeight() - (img.getHeight() * scale)) / 2;

AffineTransform at = new AffineTransform();
at.translate(x, y);
at.scale(scale, scale);

g2d.setTransform(at);
g2d.drawImage(img, 0, 0, this);

g2d.dispose();
}
}
}
}

花时间通读How to write a Mouse ListenerHow to write a Mouse-Wheel Listener

关于java - 如何使用 getScaledInstance 缩放图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18396302/

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