Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom JPanel in Java Swing

I have an existing Java Swing application. In the middle of the application is a single JPanel which I want to be able to zoom in and out of. The JPanel has a number of JComponents on it (mostly other JPanels and JLabels).

Also mouse position will need to be adjusted appropriately as well - so mouseevents need to remain same even after the JPanel has been zoomed. As such simply changing the paint methods of each component doesn't seem plausible.

EDIT:

enter image description here

OK i kind of got it working using the MagnifierUI class with some minor edits. However the magnified panel I create has the wrong mouseevents - i.e. the panel is scaled, mouseevents are not.

like image 720
sboy031 Avatar asked Feb 13 '13 04:02

sboy031


People also ask

How do you change the size of the JPanel in Java?

add( new Board(), BorderLayout. CENTER ); pack(); setResizable( false ); setLocationRelativeTo( null ); setVisible( true ); setDefaultCloseOperation( EXIT_ON_CLOSE ); setTitle( "Snake (by daGrevis)" ); Board class: setPreferredSize( new Dimension( 640, 480 ) );

How do you zoom in Java?

Press the ''ESC'' key. Select Options. Slide the FOV bar to the left to zoom in or to the right to zoom out.

What is JPanel Swing Java?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.

Does JPanel extend JComponent?

The BasicPanelUI can also install default settings for the JPanel font, foreground and background colors, and border. Generally if you want to draw something manually you'll extend JComponent and override the paintComponent() method. When people extend JPanel they rarely override any JPanel methods.


2 Answers

This is just a scetch:

  • in your JPanel keep track of an AffineTransform which represents the scale factor (see AffineTransform.scale(double,double),
  • override the paint method of your JPanel: before calling super.paint apply the affine transformation to your Graphics2D object (cast from the parameter of the paint method) by calling Graphics2D.setTransform(AffineTransform), call super.paint afterwards
  • override the methods processMouseEvent, processMouseMotionEvent and processMouseWheelEvent, apply the affine transformation to the coordinates of their mouse event parameter (AffineTransform.transform(java.awt.geom.Point2D,java.awt.geom.Point2D)), call respective super-method afterwards.

Hope this helps.

like image 132
Claude Avatar answered Sep 19 '22 15:09

Claude


Try SwingUtilities.convertPoint(source, point,destination);

like image 45
shinds Avatar answered Sep 22 '22 15:09

shinds