在规模内在JScrollPane中绘制图像

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

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

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

class贵:


     import java.awt。*;
     import java.awt.event。*;
     import java.io.File;
     import javax.swing。*;

    公共类Gui实现ActionListener {

        私人JFrame frmEditor;

        私人Mappa内容;
        私人JMenuItem mntmSfondo;
         private JScrollPane scrollabile;

         / **
          *启动应用程序。
          * /
         public static void main(String [] args){
             EventQueue.invokeLater(new Runnable(){
                 public void run(){
                    尝试{
                        桂窗=新桂();
                         window.frmEditor.setVisible(真);
                     } catch(例外e){
                         e.printStackTrace();
                     }
                 }
             });
         }

         / **
          *创建应用程序。
          * /
         public Gui(){
            初始化();
         }

         / **
          *初始化框架的内容。
          * /
         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();
         }

         / **
          *初始化菜单。
          * /
         private void inizializzaMenu(){

             JMenuBar menuBar = new JMenuBar();
             frmEditor.setJMenuBar(菜单栏);

             JMenu mnAltro =新的JMenu(“修改”);
             menuBar.add(mnAltro);

             mntmSfondo =新的JMenuItem(“加载背景”);
             mntmSfondo.addActionListener(本);
             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 = fc.getSelectedFile();
                    尝试{
                         content.setImage(文件);
                         // content = new Mappa(文件);
                         //scrollabile.setViewportView(content);
                     } catch(Exception ex){
                     }
                 }
                 if(result == JFileChooser.CANCEL_OPTION){
                 }
             }
         }

     }

Mappa类:


     import java.awt。*;
     import java.awt.image.BufferedImage;
     import java.io. *;

     import javax.imageio.ImageIO;
     import javax.swing。*;

    公共课Mappa扩展JPanel {

         BufferedImage immagine;

         public Mappa(File fileImmagine){

             if(fileImmagine!= null){

                 BufferedImage img = null;
                尝试{
                     img = ImageIO.read(new File(fileImmagine.getPath()));
                 } catch(IOException e){
                     e.printStackTrace();
                 }
                 this.immagine = img;
             }
            重绘();
         }

         public void setImage(File file)throws IOException {

             this.immagine = ImageIO.read(file);
             String name = file.getPath();
            的System.out.println(名);
            重绘();

         }

         public void paintComponent(Graphics g){

             super.paintComponent方法(克);
             g.clearRect(0,0,4000,4000);
             g.drawImage(this.immagine,0,0,getWidth(),getHeight(),this);

             System.out.println(“在Mappa上调用Repaint()”);

         }
     }

JScrollPane ,或更多, JViewport将使用组件(或在本例中为“视图”)首选大小作为确定视图应该有多大的基础。

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

所以基本上,你需要覆盖public class Mappa extends JPanel {getPreferredSize public class Mappa extends JPanel { panel,例如

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

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

还有两件事……

首先,你不应该依赖魔术数字

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

应该更像……

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

其次,

 super.paintComponent(g); 

会这样做,所以调用clearRect是没有意义的……

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

我不会用她的自然尺寸来看这张图片,如果这张图片太大,我就不会激活滚动条,

使用JLabel包含图像并将其包装在JScrollPane应该可以轻松实现您想要的效果。 从以下示例中获取提示:

 class AFrame extends JFrame { public AFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Image view Demo with JScrollPane"); ImageIcon image = new ImageIcon("myImage.png"); // pass the file location of an image JLabel label = new JLabel(image); JScrollPane scrollPane = new JScrollPane(label); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); add(scrollPane, BorderLayout.CENTER); pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new AFrame().setVisible(true); } }); } }