gpt4 book ai didi

java - 在 JScrollPane 中按比例绘制图像

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

我有一个加载图像的滚动 Pane 。我不会用她的自然尺寸这张图片,如果这张图片太大,我不会激活滚动条,但是这个指令

  g.drawImage(immagine, 0, 0, getWidth(), getHeight(), this); 

用于放置在滚动 Pane 中的缩放图像。我能做什么?

桂类:

    import java.awt.*;    import java.awt.event.*;    import java.io.File;    import javax.swing.*;    public class Gui implements ActionListener {        private JFrame frmEditor;        private Mappa content;        private JMenuItem mntmSfondo;        private JScrollPane scrollabile;        /**         * Launch the application.         */        public static void main(String[] args) {            EventQueue.invokeLater(new Runnable() {                public void run() {                    try {                        Gui window = new Gui();                        window.frmEditor.setVisible(true);                    } catch (Exception e) {                        e.printStackTrace();                    }                }            });        }        /**         * Create the application.         */        public Gui() {            initialize();        }        /**         * Initialize the contents of the frame.         */        private void initialize() {            frmEditor = new JFrame();            frmEditor.setFont(UIManager.getFont("TextArea.font"));            frmEditor.setBounds(50, 50, 1024, 768);            frmEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            frmEditor.getContentPane().setLayout(new BorderLayout(0, 0));            JPanel panelTile = new JPanel();            panelTile.setLayout(new BorderLayout(0, 0));            content = new Mappa(null);            content.setMinimumSize(new Dimension(150, 150));            scrollabile = new JScrollPane(content);            frmEditor.getContentPane().add(scrollabile, BorderLayout.CENTER);            inizializzaMenu();        }        /**         * Initialize the menu.         */        private void inizializzaMenu() {            JMenuBar menuBar = new JMenuBar();            frmEditor.setJMenuBar(menuBar);            JMenu mnAltro = new JMenu("Modify");            menuBar.add(mnAltro);            mntmSfondo = new JMenuItem("Load Background");            mntmSfondo.addActionListener(this);            mnAltro.add(mntmSfondo);        }        public void actionPerformed(ActionEvent e) {            Object source = e.getSource();            if (source == mntmSfondo) {                JFileChooser fc = new JFileChooser("tuttiSfondi");                int result = fc.showOpenDialog(null);                if (result == JFileChooser.APPROVE_OPTION) {                    File file = fc.getSelectedFile();                    try {                        content.setImage(file);                        //content = new Mappa(file);                        //scrollabile.setViewportView(content);                    } catch (Exception ex) {                    }                }                if (result == JFileChooser.CANCEL_OPTION) {                }            }        }    }

map 类:

    import java.awt.*;    import java.awt.image.BufferedImage;    import java.io.*;    import javax.imageio.ImageIO;    import javax.swing.*;    public class Mappa extends JPanel {        BufferedImage immagine;        public Mappa(File fileImmagine) {            if (fileImmagine != null ) {                BufferedImage img = null;                try {                    img = ImageIO.read(new File(fileImmagine.getPath()));                } catch (IOException e) {                    e.printStackTrace();                }                this.immagine = img;            }            repaint();        }        public void setImage(File file) throws IOException {            this.immagine = ImageIO.read(file);            String name = file.getPath();            System.out.println(name);            repaint();        }        public void paintComponent(Graphics g) {            super.paintComponent(g);            g.clearRect(0, 0, 4000, 4000);            g.drawImage(this.immagine, 0, 0, getWidth(), getHeight(), this);            System.out.println("Called Repaint() on Mappa");        }    }

最佳答案

JScrollPane,或者更确切地说,JViewport 将使用组件(或在本例中为“ View ”)的首选大小作为确定 View 大小的基础应该是。

当 View 扩展到超出滚动 Pane 的大小时,它将显示滚动条。

所以基本上,您需要覆盖 public class Mappa extends JPanel { 面板的 getPreferredSize,例如

public class Mappa extends JPanel {
//...
public Dimension getPreferredSize() {
return immagine == null ? new Dimension(200, 200) : new Dimension(immagine.getWidth(), immagine.getHeight());
}
//...
}

这将鼓励 JViewport 始终与图像大小相同。

还有两件事......

首先,你不应该依赖魔数(Magic Number),例如

g.clearRect(0, 0, 4000, 4000);

应该更像是……

g.clearRect(0, 0, getWidth(), getHeight());

其次,

super.paintComponent(g);

无论如何都会这样做,所以调用 clearRect 有点毫无意义......

您可能还想看看 Scrollable , 但这是一个相当高级的话题

关于java - 在 JScrollPane 中按比例绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20254027/

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