Solved

Zoom and ScrollPane

Posted on 2007-04-03
7
1,597 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now