gpt4 book ai didi

java - 如何从 JFileChooser 中放入图像?

转载 作者:行者123 更新时间:2023-11-29 07:30:17 26 4
gpt4 key购买 nike

我在使用 Graphics 将图像放入 jPanel 时遇到问题。我有下一个代码:

    JFileChooser navegador = new JFileChooser();
if (navegador.showOpenDialog(null) == 0) {
try {
BufferedImage imagenAbrir = ImageIO.read(navegador.getSelectedFile());
lienzo.paintComponents(imagenAbrir.getGraphics());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Ocurriò un error al guardar la imàgen");
}
}

代码是按钮的一部分,用户可以从任何路径选择图像,但它不会在面板中显示任何内容。可能是什么问题?

最佳答案

我认为你必须用 BufferedImage 对象而不是 Graphics 对象来改变你的方法

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser navegador = new JFileChooser();
if (navegador.showOpenDialog(null) == 0) {
try {
BufferedImage imagenAbrir = ImageIO.read(navegador.getSelectedFile());
//drawPan.paintComponents(imagenAbrir.getGraphics());
drawPan.drawImage(imagenAbrir);
} catch (IOException ie) {
JOptionPane.showMessageDialog(null, "Ocurriò un error al guardar la imàgen");
}
}
}

然后,如果要绘制图像,请自行创建自定义的 JPanel 类。每次在代码中调用 repaint 方法时,都会自动调用 JComponent 的 paintCompoent 方法。

final class ImagePane extends JPanel {

private static final long serialVersionUID = 1L;
private BufferedImage myImage;

public ImagePane(final BufferedImage myImage) {
this.myImage = myImage;
}

@Override
public Dimension getPreferredSize() {
return new Dimension(240, 220);
}

public void drawImage(BufferedImage img)
{
this.myImage = img;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (myImage != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - myImage.getWidth()) / 2;
int y = (getHeight() - myImage.getHeight()) / 2;
g2d.drawImage(myImage, x, y, this);
g2d.dispose();
}
}

}

这里是完整的源代码。

public class JFileChooserTest {

static Runnable doRun = new Runnable()
{
final class ChooseAction implements ActionListener
{
final ImagePane drawPan;

ChooseAction(final ImagePane drawPan)
{
this.drawPan = drawPan;
}


@Override
public void actionPerformed(ActionEvent e) {
JFileChooser navegador = new JFileChooser();
if (navegador.showOpenDialog(null) == 0) {
try {
BufferedImage imagenAbrir = ImageIO.read(navegador.getSelectedFile());
//drawPan.paintComponents(imagenAbrir.getGraphics());
drawPan.drawImage(imagenAbrir);
} catch (IOException ie) {
JOptionPane.showMessageDialog(null, "Ocurriò un error al guardar la imàgen");
}
}
}

}

final class ImagePane extends JPanel {

private static final long serialVersionUID = 1L;
private BufferedImage myImage;

public ImagePane(final BufferedImage myImage) {
this.myImage = myImage;
}

@Override
public Dimension getPreferredSize() {
return new Dimension(240, 220);
}

public void drawImage(BufferedImage img)
{
this.myImage = img;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (myImage != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - myImage.getWidth()) / 2;
int y = (getHeight() - myImage.getHeight()) / 2;
g2d.drawImage(myImage, x, y, this);
g2d.dispose();
}
}

}

@Override
public void run() {
final JFrame frame = new JFrame();
//frame.setSize(new Dimension(300,400));

JPanel lienzo = new JPanel();
lienzo.setLayout(new BorderLayout());

ImagePane drawPan = new ImagePane(null);
JButton drawMe = new JButton("draw me");
lienzo.add(drawMe, BorderLayout.NORTH);
lienzo.add(drawPan, BorderLayout.CENTER);

frame.add(lienzo);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

drawMe.addActionListener(new ChooseAction(drawPan));
}

};

public static void main(String[] args)
{

SwingUtilities.invokeLater(doRun);

}
}

希望对您有所帮助。

Mickey

关于java - 如何从 JFileChooser 中放入图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861991/

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