Link to home
Start Free TrialLog in
Avatar of mm7627
mm7627

asked on

Zoom and ScrollPane

I am working on creating a zoom on a JPanel inside of a JFrame.

At the current moment, the JPanel which houses an image is in paintComponent with Graphics2D.  I plan on using the bufferedimage to scale it according to button presses.  Right now, I add the JScrollPane when I first call my constructor:

    private void createMainPanel()
    {
            main = new MainPanel();

            scrollPane = new JScrollPane();
            scrollPane.getViewport().add(main);

            getContentPane().add(createToolBar(), BorderLayout.NORTH);
            getContentPane().add(main);
            getContentPane().add(scrollPane, BorderLayout.CENTER);
    }

the problem is, once I resize (call repaint), the JScrollPane stays the same size as before, because the createMainPanel() is never called again.  Is there a way to have the JScrollpane change with the MainPanel class which houses the Graphics2D?

Thanks a lot,

Rob
Avatar of ADSLMark
ADSLMark

You add the main panel and you add a scrollpane with main as viewport. That's double.
Just make the main panel scrollable by saying:

JPanel main = new JPanel();
getContentPane().add(new JScrollPane(main), BorderLayout.CENTER);

Mark
Avatar of mm7627

ASKER

I see I was adding extra code there.  But the problem now is, when I scale the image using g2.drawImage() and change the width and height to zoom, the scroll bar doesn't change with the size of the picture.  I want to zoom in on the panel and still be able to see the whole thing using scroll bars.

Here's the mainPanel class:

class MainPanel extends JPanel
      {
            private BufferedImage img;
            private double zoomX, zoomY;

            public MainPanel()
            {
                  zoomX = 1.0;
                  zoomY = 1.0;
                  try
                  {
                        img = ImageIO.read(new File("purdue-map.jpg"));
                  } catch (IOException e) {
                  }
            }

            public void zoomFactor(int i)
            {
                  if (i == 0)
                  {
                        zoomX += .1;
                        zoomY += .1;
                        // zoom in
                  } else {
                        zoomX -= .1;
                        zoomY -= .1;
                        // zoom out
                  }
            }

            public Dimension getPreferredSize()
            {
                  if (img == null)
                  {
                        return new Dimension(100,100);
                  } else {
                        return new Dimension(img.getWidth(null), img.getHeight(null));
                  }
            }

            public void paintComponent(Graphics g)
            {
                  super.paintComponent(g);
                  Graphics2D g2d = (Graphics2D)g;
                  //g2d.scale(zoomX, zoomY);
                  /* NOTE: we can use scale or scale with width and height */
                  g2d.drawImage(img,-img.getWidth()/2,-img.getHeight()/2,(int)(img.getWidth()*zoomX),(int)(img.getHeight()*zoomY),null);
            }
      }
SOLUTION
Avatar of ADSLMark
ADSLMark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
u need to call revalidate() after changing the zoom factor
Avatar of mm7627

ASKER

Do I need to do stuff with setPreferredSize()?  Because simply calling revalidate() doesn't seem to change anything.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mm7627

ASKER

Thanks a lot.