Image Scroll Panes

ScrollPaneDemo Example (Run Applet)

import java.awt.*;
import javax.swing.*;

public class ScrollPaneDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super(new BorderLayout());

            final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();
            final ImagePane imagePane = new ImagePane(image);

            //Put the imagePane into a scrollPane.
            final JScrollPane scrollPane = new JScrollPane(imagePane);

            this.add(scrollPane);
        }

        public class ImagePane extends JPanel
        {
            private Image image;

            public ImagePane(Image image)
            {
                this.image = image;
            }

            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                // force the ImagePane to be the size of the image
                setPreferredSize(new Dimension(this.image.getWidth(this), this.image.getHeight(this)));
                g.drawImage(this.image, 0, 0, this);

                // force the scroll bars to appear
                this.revalidate();
            }
        }
    }
}

ScrollPane_Mouse_Demo Example (Run Applet)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollPane_Mouse_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super(new BorderLayout());

            final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();
            final ImagePane imagePane = new ImagePane(image);

            // Put the imagePane into a scrollPane.
            final JScrollPane scrollPane = new JScrollPane(imagePane);
            imagePane.setScrollPane(scrollPane); // allow the imagePane to access the ScrollPane

            this.add(scrollPane);
        }

        public class ImagePane extends JPanel implements MouseMotionListener, MouseListener
        {
            private int curX = 0;
            private int curY = 0;
            private int offsetX = 0;
            private int offsetY = 0;
            private Image image;
            private JScrollPane scrollPane;

            ImagePane(Image image)
            {
                this.image = image;
                this.addMouseListener(this);
                this.addMouseMotionListener(this);
            }

            public void setScrollPane(JScrollPane scrollPane)
            {
                this.scrollPane = scrollPane;
            }

            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                // force the ImagePane to be the size of the image
                this.setPreferredSize(new Dimension(this.image.getWidth(this), this.image.getHeight(this)));
                g.drawImage(this.image, 0, 0, this);

                // force the scroll bars to appear
                this.revalidate();
            }

            @Override
            public void mouseDragged(MouseEvent e)
            {
                final int x = e.getX();
                final int y = e.getY();

                this.curX -= x - this.offsetX;
                if (this.curX < 0)
                {
                    this.curX = 0;
                }

                this.curY -= y - this.offsetY;
                if (this.curY < 0)
                {
                    this.curY = 0;
                }

                this.scrollPane.getViewport().setViewPosition(new Point(this.curX, this.curY));
            }

            @Override
            public void mouseMoved(MouseEvent me)
            {
            }

            @Override
            public void mouseClicked(MouseEvent me)
            {
            }

            @Override
            public void mousePressed(MouseEvent e)
            {
                final int x = e.getX();
                final int y = e.getY();

                this.offsetX = x;
                this.offsetY = y;
            }

            @Override
            public void mouseReleased(MouseEvent me)
            {
            }

            @Override
            public void mouseEntered(MouseEvent me)
            {
            }

            @Override
            public void mouseExited(MouseEvent me)
            {
            }
        }
    }
}

ScrollPane_Scale_Demo Example (Run Applet)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollPane_Scale_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements AdjustmentListener
    {
        private final JScrollBar scaleBar = new JScrollBar(JScrollBar.VERTICAL,
                                                           100, // start at normal size
                                                           5,
                                                           25, // can scale to 1/4 size
                                                           500); // can scale to 5 times bigger
        private final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();
        private final ImagePane imagePane = new ImagePane(image);

        public View()
        {
            super(new BorderLayout());

            final JScrollPane scrollPane;

            //Put this.imagePane into a scrollPane.
            scrollPane = new JScrollPane(imagePane);
            imagePane.setScrollPane(scrollPane); // allow the imagePane to access the ScrollPane

            this.setLayout(new BorderLayout());
            this.add("West", this.scaleBar);
            this.add("Center", scrollPane);
            this.scaleBar.addAdjustmentListener(this);
        }

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            imagePane.setScale((double) this.scaleBar.getValue());
            this.repaint();
        }

        public class ImagePane extends JPanel implements MouseMotionListener, MouseListener
        {
            private int curX = 0;
            private int curY = 0;
            private int offsetX = 0;
            private int offsetY = 0;
            private Image image;
            private JScrollPane scrollPane;
            private double scale = 100;   // initially 100%

            ImagePane(Image image)
            {
                this.image = image;
                this.addMouseListener(this);
                this.addMouseMotionListener(this);
            }

            public void setScrollPane(JScrollPane scrollPane)
            {
                this.scrollPane = scrollPane;
            }

            public void setScale(Double scale)
            {
                this.scale = scale;
            }

            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                // force the ImagePane to be the size of the image
                this.setPreferredSize(new Dimension((int) ((double) (this.image.getWidth(this)) * this.scale / 100.0),
                                                    (int) ((double) (this.image.getHeight(this)) * this.scale / 100.0)));
                g.drawImage(this.image, 0, 0, (int) ((double) (this.image.getWidth(this)) * this.scale / 100.0),
                            (int) ((double) (this.image.getHeight(this)) * this.scale / 100.0), this);

                // force the scroll bars to appear
                this.revalidate();
            }

            @Override
            public void mouseDragged(MouseEvent e)
            {
                final int x = e.getX();
                final int y = e.getY();

                this.curX -= x - this.offsetX;
                if (this.curX < 0)
                {
                    this.curX = 0;
                }

                this.curY -= y - this.offsetY;
                if (this.curY < 0)
                {
                    this.curY = 0;
                }

                this.scrollPane.getViewport().setViewPosition(new Point(this.curX, this.curY));
            }

            @Override
            public void mouseMoved(MouseEvent me)
            {
            }

            @Override
            public void mouseClicked(MouseEvent me)
            {
            }

            @Override
            public void mousePressed(MouseEvent e)
            {
                final int x = e.getX();
                final int y = e.getY();

                this.offsetX = x;
                this.offsetY = y;
            }

            @Override
            public void mouseReleased(MouseEvent me)
            {
            }

            @Override
            public void mouseEntered(MouseEvent me)
            {
            }

            @Override
            public void mouseExited(MouseEvent me)
            {
            }
        }
    }
}

ScrollPane_MouseWheel_Demo Example (Run Applet)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollPane_MouseWheel_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements AdjustmentListener
    {
        private final JScrollBar scaleBar = new JScrollBar(JScrollBar.VERTICAL,
                                                           100, // start at normal size
                                                           5,
                                                           25, // can scale to 1/4 size
                                                           500); // can scale to 5 times bigger
        private final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();
        private final ImagePane imagePane = new ImagePane(image);

        public View()
        {
            super(new BorderLayout());

            final JScrollPane scrollPane;

            //Put this.imagePane into a scrollPane.
            scrollPane = new JScrollPane(imagePane);
            imagePane.setScrollPane(scrollPane); // allow the imagePane to access the ScrollPane

            this.setLayout(new BorderLayout());
            this.add("West", this.scaleBar);
            this.add("Center", scrollPane);
            this.scaleBar.addAdjustmentListener(this);
        }

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            imagePane.setScale((double) this.scaleBar.getValue());
            this.repaint();
        }

        public class ImagePane extends JPanel implements MouseMotionListener, MouseListener, MouseWheelListener
        {
            private int curX = 0;
            private int curY = 0;
            private int offsetX = 0;
            private int offsetY = 0;
            private Image image;
            private JScrollPane scrollPane;
            private double scale = 100;   // initially 100%

            ImagePane(Image image)
            {
                this.image = image;
                this.addMouseListener(this);
                this.addMouseMotionListener(this);
                this.addMouseWheelListener(this);
            }

            public void setScrollPane(JScrollPane scrollPane)
            {
                this.scrollPane = scrollPane;
            }

            public void setScale(Double scale)
            {
                this.scale = scale;
            }

            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                // force the ImagePane to be the size of the image
                this.setPreferredSize(new Dimension((int) ((double) (this.image.getWidth(this)) * this.scale / 100.0),
                                                    (int) ((double) (this.image.getHeight(this)) * this.scale / 100.0)));
                g.drawImage(this.image, 0, 0, (int) ((double) (this.image.getWidth(this)) * this.scale / 100.0),
                            (int) ((double) (this.image.getHeight(this)) * this.scale / 100.0), this);

                // force the scroll bars to appear
                this.revalidate();
            }

            @Override
            public void mouseDragged(MouseEvent e)
            {
                final int x = e.getX();
                final int y = e.getY();

                this.curX -= x - this.offsetX;
                if (this.curX < 0)
                {
                    this.curX = 0;
                }

                this.curY -= y - this.offsetY;
                if (this.curY < 0)
                {
                    this.curY = 0;
                }

                this.scrollPane.getViewport().setViewPosition(new Point(this.curX, this.curY));
            }

            @Override
            public void mouseMoved(MouseEvent me)
            {
            }

            @Override
            public void mouseClicked(MouseEvent me)
            {
            }

            @Override
            public void mousePressed(MouseEvent e)
            {
                final int x = e.getX();
                final int y = e.getY();

                this.offsetX = x;
                this.offsetY = y;
            }

            @Override
            public void mouseReleased(MouseEvent me)
            {
            }

            @Override
            public void mouseEntered(MouseEvent me)
            {
            }

            @Override
            public void mouseExited(MouseEvent me)
            {
            }

            @Override
            public void mouseWheelMoved(MouseWheelEvent e)
            {
                this.scale += (double) e.getWheelRotation(); // Increment or decrement this.scale
                this.repaint();
            }
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>