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
Solved

Zoom and ScrollPane

Posted on 2007-04-03
7
1,616 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
reverse digits of a number using for loop 5 52
jboss 7.1 start up error 1 39
Java class and jar 3 24
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

837 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