-1

I am currently trying to make it possible for a JPanel to be zoomed in. My idea is pretty much as follows :

I have a JPanel (custom with overriden paintComponent etc.) that I place inside my JScrollPane.

What I do to zoom in is to scale up my JPanel using the following code (overriding the paint method)

@Override
public void paint(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;

    if (m_hasBeenScaled)
    {
        m_transform.scale(m_zoomValue, m_zoomValue);
        g2.setTransform(m_transform);
        m_transform = new AffineTransform();
    }

    super.paint(g);
}

This works well, however my JScrollPane doesn't display scrollbars as I scale to bigger dimensions. How do I make the JScrollPane respond to this scale up of my JPanel ?

Here's the code I use to create both my JPanel and JScrollPan (Grid is my class extending JPanel):

m_gridPanel = new Grid();
m_gridContainer = new JScrollPane(m_gridPanel);
m_gridContainer.setPreferredSize(new Dimension(605, 605));
Bypp
  • 331
  • 3
  • 22
  • 1
    1) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) I'd approach this the same way as in [this answer](http://stackoverflow.com/a/27119520/418556).. – Andrew Thompson Nov 25 '14 at 06:12
  • This `m_gridContainer.setPreferredSize(new Dimension(605, 605));` is a bad idea, the size of the component will change based on the zoom factor as well. Zooming a component is much more complex than changing the transform, you should be translating the mouse events as well, see http://stackoverflow.com/questions/21174997/how-to-add-mouselistener-to-item-on-java-swing-canvas/21175125#21175125 for example... – MadProgrammer Nov 25 '14 at 06:12
  • I am now overriding the getPreferredSize method as you said, and I now see the scrollbars appearing. However, when I slide them, the view doesn't change at all. Any reason for that ? – Bypp Nov 25 '14 at 06:25

1 Answers1

1

The size of the component will be affected by the zoom factor as well, to that end, setting the preferredSize to a "static" value makes no sense, instead, you should be overriding the getPreferredSize and adjusting the size returned by applying the zoom factor to it as well.

Zooming a component is much more complex than changing the transform, you should be translating the mouse events as well, see How to add MouseListener to item on Java Swing Canvas for example.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I am now overriding the getPreferredSize method as you said, and I now see the scrollbars appearing. However, when I slide them, the view doesn't change at all. Any reason for that ? – Bypp Nov 25 '14 at 06:25
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Nov 25 '14 at 06:25