-1

I'm trying to use the scrollbars of a JScrollPane to simulate scrolling in a Canvas (JPanel). The panel has a fixed size and an image will be drawn inside it using paintComponent. I need to be able to zoom in (and scroll around), but I only want the image to zoom, while keeping it's JPanel the same fixed size. I try to avoid making my own scrollbars inside of the panel (by drawing them, listening for mouse events and such things).

Is it possible to use a JScrollPane to manage the offsets and just altering the size of my image/scaling it up when it gets painted, without ever touching the bounds of my JPanel?

The initial reason for this is that I'd like to manipulate pixel data as an array, which does not work well if I need to resize the panel...

Thanks in advance -

machinateur
  • 502
  • 5
  • 10
  • Post a short example of what you're trying to do which we can run ourselves. – user1803551 May 25 '15 at 23:40
  • 1
    I wouldn't use `JScrollBar`s for scaling to start with – MadProgrammer May 25 '15 at 23:59
  • [example](http://stackoverflow.com/questions/19643637/zoom-using-mouse-and-graphics/19646246#19646246), [example](http://stackoverflow.com/questions/15699916/how-do-i-make-this-panel-zoom-toward-the-middle-of-the-panel/15700496#15700496), [example](http://stackoverflow.com/questions/27498104/java-image-zooming-japplet/27498381#27498381), [example](http://stackoverflow.com/questions/19674058/zooming-effect-not-shown-in-the-frame/19674742#19674742) – MadProgrammer May 26 '15 at 00:05
  • You should also have a look at [maintaining aspect ratio of JPanel background image](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [Quality of Image after resize very low](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer May 26 '15 at 00:06
  • 1
    The secert here is ensuring that the `preferredSize` of the `JPanel` matches the scaled size of the image in question, this way the `JScrollPane` can adjust the scroll bars to match – MadProgrammer May 26 '15 at 00:07
  • @MadProgrammer thank you for all the examples! I'll look into them asap :) – machinateur May 26 '15 at 04:24

1 Answers1

0

What I would do in this case is create another class that extends ImageIcon and then override the paint method. In this method I would have it scale the image to whatever scale it is set to.

NOTE: The mirror feature just flips the image horizontally.

Example:

public class Icon extends ImageIcon{

private int scale = 1;
private boolean mirror = false;

    public Icon(URL url) throws IOException{
        super(ImageIO.read(url));
    }

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

    @Override
    public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2 = (Graphics2D)g.create();
        int height = 0, width = this.getIconWidth(), x1 = 1;
        if(mirror || scale != 1){
            height = -this.getIconHeight();
        }
        if(mirror){
            x1 = -1;
        }else{
            width = 0;
        }
        g2.translate(width * scale, height);
        g2.scale(x1 * scale, 1 * scale);
        super.paintIcon(c, g2, x, y);
    }

    public boolean isMirror() {
        return mirror;
    }

    public void setMirror(boolean mirror) {
        this.mirror = mirror;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Cyphereion
  • 71
  • 11