【问题标题】:How do I properly resize a large image?如何正确调整大图像的大小?
【发布时间】:2020-06-15 09:17:23
【问题描述】:

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

【问题讨论】:

  • “无法正确编译” - 有什么错误?

标签: java javafx netbeans compiler-errors resize


【解决方案1】:

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

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);
}

【讨论】:

  • 非常感谢!我对自己做错了什么感到有点沮丧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多