在规模内在JScrollPane中绘制图像 [英] Drawing an image in JScrollPane within scale

查看:134
本文介绍了在规模内在JScrollPane中绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a scrollpane where load an image. I wont this image with her natural size, and if this image is too big, I wont activated the scrollbar, but this instruction

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

放置在滚动窗格中的缩放图像。我该怎么办?

scaled image for placing in scrollpane. What can I do?

Class Gui:

Class Gui:



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

    }

Class Mappa:

Class Mappa:



    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)的首选大小作为确定视图大小的基础。

JScrollPane, or more to the point JViewport will use the component's (or in this case the "view's") preferred size as a bases for determining how big the view should be.

当视图扩展超出滚动窗格的大小时,它将显示滚动条。

When the view expands beyond the size of the scroll pane, it will show the scroll bars.

所以基本上,你需要覆盖公共类的 getPreferredSize Mappa扩展JPanel {面板,例如

So basically, you need to override the getPreferredSize of the public class Mappa extends JPanel { panel, for example

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

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

This will encourage the JViewport to always be the same size as the image.

此外,还有两件事......

Also, two things...

首先,你不应该依赖幻数,例如

First, you shouldn't rely on magic numbers, for example

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

应该更像......

Should be more like...

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

其次,

super.paintComponent(g);

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

Will do this any way, so calling clearRect is kind of pointless...

您可能还想看看 可滚动 ,但这是一个非常高级的主题

You might also like to take a look at Scrollable, but it is quite an advanced topic

这篇关于在规模内在JScrollPane中绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆