3

Are there any methods which i want to zoom the view of the viewport but the location of the components inside does not change. The setting is that i have a big jpanel inside a jscrollpane in which i only want to only change the view into zoomed mode. the objects can be dragged and drop anywhere inside the panel so that is why it is important that its xy position remains.

here's my present zoom code, what i do is i use the graphics 2d scale to zoom the view. the problem is that the position is a mess. I also want to zoom it out so that the whole panel fits the jscrollpane.

//zoom in draw method using scale
g2.translate(zoomX, zoomY);

g2.scale(zoomScale, zoomScale);

g2.translate(-zoomX, -zoomY);

Im also thinking of manipulating the viewport. here is my viewport initialization. the 2000 by 1000 dimension is the panel and the 750 by 698 is the extentsize of my viewport. any suggestion on how will i zoom without messing the position of the components? Maybe moving the setviewposition is part of the solution?

 view = new JViewport();

 view.setViewSize(new Dimension(2000,1000));

 view.setExtentSize(new Dimension(750,698));

 view.setViewPosition(new Point(view.getExtentSize().width/2,view.getExtentSize().height/2));
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • there is zooming in JScollPane solved a few times , including centering to desired Point, please search here, – mKorbel Apr 17 '13 at 08:58
  • yes there are many zooming solutions using scrollpane, but this time i want it to be like google map zooming function where the whole jpanel inside the jscrollpane is zoomed out only in the view. Other solutions are only for zooming jscrollpane with image but not jscrollpane with components inside that can be dragged and dropped inside the panel. Its like a cisco packet tracer application zoom and routers and switches can be manipulated after zooming. – Lore Jairus Camero Apr 18 '13 at 01:44
  • no idea how Google Maps are coded, but I think thead there is about switch betweens view not real zoom, sure maybe I'm wrong – mKorbel Apr 18 '13 at 05:16

1 Answers1

0

Use two buttons which increase/decrease the zoom factor. Override the paintComponent method of the panel in the JScrollPane like this.

@Override
public void paintComponent(Graphics g)
{
    g.scale(zoomFactor, zoomFactor);
    // Change the size of the panel
    setSize(origWidth * zoomFactor, origHeight * zoomFactor);
    // Re-Layout the panel
    validate();
    super.paintComponent(g);
}

Hope it works.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
  • the zoom view works but the xy position of the component inside remains. where if you click on object icon the object is not in that position. the xy position remains even though it is zoomed. – Lore Jairus Camero Apr 17 '13 at 03:25
  • still not working well sir, the zoom view is working but i dont only need to zoom the graphics also the objects which is draggable. I think it has something to do with the viewport or its view. where it will only adjust the view but not the panel. – Lore Jairus Camero Apr 17 '13 at 05:12
  • For dragging objects, you have to convert the mouse point to the scaled point. – Sri Harsha Chilakapati Apr 17 '13 at 05:16
  • I think its not only the point that's needed to be changed. Because when i zoom it and when i scroll it down where the lower part does not have object present. still the object is clickable in the same position only there is no image represented in the position. – Lore Jairus Camero Apr 17 '13 at 05:22
  • I don't know how to do that. This is the code from memory which I used in making a picture viewer. – Sri Harsha Chilakapati Apr 17 '13 at 05:25