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 another JPanel
in the remaining space (center). In that inner JPanel
, you would override the paintComponent
method and would draw the portion of your BufferedImage
into the JPanel
. Use the scroll bars' offset to offset your image, and the inner JPanel
for the width and height to draw (the viewport).
Note: your scroll bars will invoke the repaint
method of your inner JPanel
whenever their value change.
The paintComponent
will 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 JPanel
and 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.