I have a JPanel with an vector image that the user can zoom and pan on. Overlaying this image is a (transparent) JComponent, which I allow the user to annotate the underlying image. This works great at full scale, but If I zoom in, using AffineTransform, the overlaying coordinates are affected also. So, If a user draws a box on the image the box is scaled also.
Any suggestions on how to decouple this behaviour? So that the JComponent is not affected by the JPanel's AffineTransform?
Typically, you need both a forward and inverse transform to translate between the two co-ordinate systems. In this example, the scaling equations are explicit; in this alternate approach, a second AffineTransform is used.
Related
How can I scroll by JScrollPannel left-down to the negative coordinates area? I am developing an editor with drag and drop. For example, we have a small circle at the center of JPanel wich has a coordinates (50, 50). This circle is only drawing on the JPanel, not a java.awt.Component, thats why it can get any values of coordinates. We drag this circle left-down and its coordinates becomes (-30, 50). How can I see this circle after moving?
After dragging the circle to the right I can increase a size of my JPanel if the location of the circle will be outside of the bounds of JPannel. But what need I do to the first case?
Well, in addition I tried to draw a picture.
Check out the Drag Layout. It is a layout manager designed to allow you to drag components.
if a component is dragged to a negative location then the location of all components are translated by the negative amount to make sure the location of all components is positive
the preferred size is recalculated after each component is dragged.
I'm experiencing a problem with AWT in rotating the graphics in a panel by 90 degrees.
I can rotate the graphics in a panel by casting to Graphics2D and applying a transform.
The problem with this is that if the panel area is rectangular then part of the graphics becomes hidden. I can't seem to set clip bounds to the whole area.
If, for example, the window is short and wide then the clip region becomes narrow and tall. If the window is narrow and tall the clip region becomes short and wide. I don't know how to override this behavior.
Is there a better way of doing this or a way to work around the problem?
EDIT SOLVED:- It turns out that overriding behavior of getWidth() and getHeight() is a bad idea lol
As shown here, override getPreferredSize() on the enclosing panel to return a Dimension that can accommodate your desired view, e.g. Math.max(width, length). As shown here,
Translate the image to the origin.
Rotate the image.
Translate the image back to the center of the panel.
The knob of the JSlider originally has only 1 degree of freedom (it translates on the track either horizontally or vertically). I want to add a 2nd degree of freedom to the knob and manually rotate it. In other words, I want to be able to move the knob left and right AND rotate it. Both of these movements should be able to be set by either dragging the mouse in a linear or rotational direction. I havent seen code for this and was wondering how I could go about this.
I'd extend BasicSliderUI and override the paintThumb method so that you can paint your thumb at an arbitrary rotation, either by applying a rotation transform to the thumb image, or by manually drawing the thumb rotated. Then apply this UI to your JSlider.
To control the rotation with the mouse and keyboard, it may be easiest to add a MouseWheelListener to the JSlider so that you control thumb rotation with the mouse wheel.
I want to know if there is some way to rotate a panel without changing its shape. I mean I am able to rotate the drawings inside the panel using rotate() given in graphics2D but the rectangular drawings become diamond shaped. Is there some soln for it?? I mean can I avoid the drawing from becoming diamond shape. The problem is more evident when u change the resolution of the screen.
Yes, rotate the image around its center, as shown in this example.
We have an old (more than 10yrs old) Java Swing applicatin which draws lots of circles and connections (lines) between those circles on a JCanvas (a subclass of JComponent) based on lab data.
Because the data becames bigger and bigger, we cannot display the entire drawing now. We have put the JCavans into a JScrollPane but it is not convenience to scroll the drawing.
Can we add zoom in zoom out for it? if yes, how? I know we can zoom image but the drawing on Canvas is an image?
thanks,
EDIT:
we draw those circles and line with Graphics within paintComponent(Graphics g) method.
You could apply a scaling Transform to the Graphics2D object passed to the paintComponent method. You can learn how to use it in the Java 2D programming trail.
Without knowing anything about your application it's hard to provide useful advice (adding a code snippet or better yet a cutdown example app would be helpful to show how things are being drawn), but I'll give it a shot:
Why don't you multiply the x,y and width,height values by a scaling factor before you draw each circle/line? I assume that somewhere your canvas is using a Graphics object to draw each shape?