Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Zoom and ScrollPane

Posted on 2007-04-03
7
Medium Priority
?
1,667 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 100 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

610 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