我正在尝试调整尺寸为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);
}