drawing in negative coordinates java swing - java

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.

Related

How do I refer to the distance I’ve scrolled in a JScrollPane?

I’m writing code for drawing in a JPanel as if it’s a canvas, where I can create shapes by clicking inside the panel. When I click, it places the dots where I want them to go, however if I scroll and then click, it places the dots where the mouse would be if I hadn’t scrolled. I assume this is because the mouse coordinates don’t change, so I’m guessing I need to add the number of pixels I’ve scrolled (horizontally and vertically). Is there a command I can use to refer to these values?
It's been a while since I've worked with Swing, but I believe you want to call:
JScrollPane sp = ...
Point viewPosition = sp.getViewport().getViewPosition();
The viewPosition.x and .y will return the offset in pixels from the top left corner.

Java - Getting screen position of buffered image

I'm working with some code where I have a few buffered images in a scrollbar. When I click one of these images, I want to draw a rectangle around the image to indicate that the image is selected.
My issue is that because my panel is scrollable, the image position set when drawing is not necessarily the actual screen position. So when the mouse is clicked, the point of the cursor's position doesn't intersect any of the position attributes (x, y, height, & width) I have for the object holding the actual image.
Does anybody know how to get the actual position of an image relative to the screen instead of the panel?
So far I haven't found any solutions to this online.
Thanks.
You can use the SwingUtilities.convertPoint(...) method.
I would guess you need to convert the mouse point the coordinates of the viewport of the scroll pane.
Maybe an easier approach is to use a JList with a custom renderer. You can set the JList to wrap components horizontally. Then in you custom renderer you just add a Border to the selected item. Read the section from the Swing tutorial on How to Use Lists for more information and examples.

Finding Coordinates on a Bufferred Image

I have a buffered image of a 4x4 checker Board (resolution 400x400) rendered on half a JPanel. Is it possible to find the coordinates of each square corner without doing it manually? I'm using absolute positioning on the JPanel and it is the only container besides the Frame
If you have a 4 x 4 checkerboard that's 400 x 400, then each square is 100 x 100 pixels.
When you construct a BufferedImage like this, you save a Rectangle for each square as you're doing the construction.
That way, when you do a mouse click later, you can use the contains method of Rectangle to determine which square was clicked.
You shouldn't use absolute positioning. If your checker board takes up half the JPanel, FlowLayout or BoxLayout works well.

Making fillRect fill dynamically from right to left

Hi i am currently working on a Java app and i have to draw a little using Canvas, Graphics etc..
So if i click a point and drag it across, it should have a line drawn in between (Think of drawing a line in paint).
I am currently using fillRect and the question is, is there a way to fillRect from right to left? Or do i have to explicitly create a workaround for this?
fillRect() method needs four arguments, x, y, width, height. What you should do is just compute the values of those arguments. If you want to draw fillRect from right to left, you just need to decrease x and increase width perhaps as your mouse move. That's it.
To add to ntalbs excellent answer (+1).
Basically, when the user clicks a point, you need to store that as the anchor point. When they drag the mouse, you need to determine in which direction the mouse has dragged.
If the click.x > drag.x, the the drag.x becomes the x parameter for your rectangle, otherwise it's the click.x.
Width and height are simple determine as the difference between the click and drag points (taking into consideration which is larger ;))

JComponent JPanel Zoom, pan and Coordinates Question

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.

Categories