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:
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.
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 ) );
Press the ''ESC'' key. Select Options. Slide the FOV bar to the left to zoom in or to the right to zoom out.
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.
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.
This is just a scetch:
JPanel
keep track of an AffineTransform which represents the scale factor (see AffineTransform.scale(double,double)
,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
afterwardsprocessMouseEvent
, 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.
Try SwingUtilities.convertPoint(source, point,destination);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With