gpt4 book ai didi

java - 如何正确调整大图像的大小?

转载 作者:行者123 更新时间:2023-12-01 18:09:04 24 4
gpt4 key购买 nike

我正在尝试调整尺寸为 100 x 100 的图像,但我不明白我做错了什么。我将代码放在那里来调整它的大小,但它无法正确编译。作业的一部分是按原样拍摄图像并使用代码调整其大小。我以为我可以预先调整它的大小然后将其输入其中,但这是不允许的。请帮忙。

这是代码,这是图像 BlackCat

package catmover;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.Image
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class MainPage extends JFrame implements ActionListener {

private static JFXPanel fxContainer;

private JButton up, down, left, right;

private JLabel catHolder;

private SpringLayout sl;

private JPanel jp;

public MainPage() {
this.jp = new JPanel();
this.sl = new SpringLayout();
}

void init() {
fxContainer = new JFXPanel();
add(fxContainer, BorderLayout.CENTER);

Platform.runLater(this::createScene);
}

private void createScene() {
try {
prepareCatHolder();
prepareButtons();

this.add(jp);
this.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}

}

private void prepareCatHolder() throws IOException {
BufferedImage img = ImageIO.read(new File("Black_Cat.png"));
Image newImage = ("Black_cat.png").getScaledInstance(100, 100, Image.SCALE_DEFAULT);
ImageIcon icon = new ImageIcon(img);
catHolder = new JLabel(icon);

jp.setLayout(sl);
sl.putConstraint(SpringLayout.WEST, catHolder,
100,
SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, catHolder,
100,
SpringLayout.NORTH, jp);

jp.add(catHolder);
}

private void prepareButtons() {
up = new JButton("UP");
up.setPreferredSize(new Dimension(100, 50));
up.addActionListener(this);

down = new JButton("DOWN");
down.setPreferredSize(new Dimension(100, 50));
down.addActionListener(this);

left = new JButton("LEFT");
left.setPreferredSize(new Dimension(100, 50));
left.addActionListener(this);

right = new JButton("RIGHT");
right.setPreferredSize(new Dimension(100, 50));
right.addActionListener(this);

sl.putConstraint(SpringLayout.WEST, up,
this.getWidth() - 300, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, up,
this.getHeight() - 300, SpringLayout.NORTH, jp);

sl.putConstraint(SpringLayout.WEST, left,
this.getWidth() - 450, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, left,
this.getHeight() - 200, SpringLayout.NORTH, jp);

sl.putConstraint(SpringLayout.WEST, right,
this.getWidth() - 150, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, right,
this.getHeight() - 200, SpringLayout.NORTH, jp);

sl.putConstraint(SpringLayout.WEST, down,
this.getWidth() - 300, SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, down,
this.getHeight() - 100, SpringLayout.NORTH, jp);

jp.add(up);
jp.add(down);
jp.add(left);
jp.add(right);

}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(up)) {

jp.remove(catHolder);

sl.putConstraint(SpringLayout.NORTH, catHolder,
0,
SpringLayout.NORTH, jp);

sl.putConstraint(SpringLayout.WEST, catHolder,
(this.getWidth() / 2) - (catHolder.getWidth() / 2),
SpringLayout.WEST, jp);

jp.add(catHolder);

this.revalidate();
} else if (e.getSource().equals(down)) {

jp.remove(catHolder);

sl.putConstraint(SpringLayout.SOUTH, catHolder,
0,
SpringLayout.SOUTH, jp);

sl.putConstraint(SpringLayout.WEST, catHolder,
(this.getWidth() / 2) - (catHolder.getWidth() / 2),
SpringLayout.WEST, jp);

jp.add(catHolder);

this.revalidate();
} else if (e.getSource().equals(left)) {

jp.remove(catHolder);

sl.putConstraint(SpringLayout.WEST, catHolder,
0,
SpringLayout.WEST, jp);

sl.putConstraint(SpringLayout.NORTH, catHolder,
(this.getHeight() / 2) - (catHolder.getHeight() / 2),
SpringLayout.NORTH, jp);

jp.add(catHolder);

this.revalidate();
} else if (e.getSource().equals(right)) {

jp.remove(catHolder);

sl.putConstraint(SpringLayout.EAST, catHolder,
0,
SpringLayout.EAST, jp);

sl.putConstraint(SpringLayout.NORTH, catHolder,
(this.getHeight() / 2) - (catHolder.getHeight() / 2),
SpringLayout.NORTH, jp);

jp.add(catHolder);

this.revalidate();
}
}
}

最佳答案

有多种方法可以做到这一点。我认为您正在寻找的更多内容是这样的:

private void prepareCatHolder() throws IOException {
BufferedImage image = ImageIO.read(new File("Black_Cat.png"));
BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image, 0, 0, 100, 100, null);
g2d.dispose();
Image img = Toolkit.getDefaultToolkit().createImage(bi.getSource());
ImageIcon icon = new ImageIcon(img);
catHolder = new JLabel(icon);

jp.setLayout(sl);
sl.putConstraint(SpringLayout.WEST, catHolder,
100,
SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, catHolder,
100,
SpringLayout.NORTH, jp);

jp.add(catHolder);
}

您可能想尝试的另一种方法是这样的:

private void prepareCatHolder() {
if (new File("Black_Cat.png").exists()) {
ImageIcon image = new ImageIcon("Black_Cat.png");
Image img = image.getImage();
Image resizedImage = img.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
image = new ImageIcon(resizedImage);
catHolder = new JLabel(image);
}
else {
catHolder = new JLabel("No Image");
}

jp.setLayout(sl);
sl.putConstraint(SpringLayout.WEST, catHolder,
100,
SpringLayout.WEST, jp);
sl.putConstraint(SpringLayout.NORTH, catHolder,
100,
SpringLayout.NORTH, jp);

jp.add(catHolder);
}

关于java - 如何正确调整大图像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60499654/

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