I currently have an image that I load into program as a BufferedImage. This BufferedImage is put inside a JPanel class which has a fixed size.
Now the problem I am facing is that how can I pan this large BufferedImage inside this fixed size JPanel.
The JPanel dimension is definitely smaller than the image.
Thanks!
First, attach some scroll bars to the JPanel (south and east, using a
BorderLayout) that will be visible only if the image is too large and/or high. Attach anotherJPanelin the remaining space (center). In that innerJPanel, you would override thepaintComponentmethod and would draw the portion of yourBufferedImageinto theJPanel. Use the scroll bars’ offset to offset your image, and the innerJPanelfor the width and height to draw (the viewport).Note: your scroll bars will invoke the
repaintmethod of your innerJPanelwhenever their value change.The
paintComponentwill be called by the system automatically (or manually) whenever the image needs to be redrawn. Doing this, you will be able to customize the view you give your image (if such feature is needed); rotation, scaling, pixel manipulation, custom overlays etc.You can also attach some MouseMotionListener to the inner
JPaneland modify the scroll bars’ offset according the mouse movements (modifying the scroll bars will automatically trigger the repaint on the image) for mouse interaction with your component. Just a thought.