Solved

Zoom and ScrollPane

Posted on 2007-04-03
7
1,624 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:mm7627
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18844712
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
0
 

Author Comment

by:mm7627
ID: 18845240
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);
            }
      }
0
 
LVL 10

Assisted Solution

by:ADSLMark
ADSLMark earned 25 total points
ID: 18846032
This page shows an example of a scrollable image. Should be easy to adapt so it can also be zoomed.

http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html

Mark
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 18847036
u need to call revalidate() after changing the zoom factor
0
 

Author Comment

by:mm7627
ID: 18847083
Do I need to do stuff with setPreferredSize()?  Because simply calling revalidate() doesn't seem to change anything.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 18847133
no, but u need to change your getPreferredSize() to return the correct size.
0
 

Author Comment

by:mm7627
ID: 18847235
Thanks a lot.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question