jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Problem when repainting rectangles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem when repainting rectangles" Watch "Problem when repainting rectangles" New topic
Author

Problem when repainting rectangles

Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Hi!

I want a user to draw rectangles in a Indesign fashion. Yoy should be able to resize and so on. The problem is that when I drag the mouse real fast the rectangle moves to a position that is not correct

If someone could look at this code. If you press the 1 button you can
select rectangles and move them. If you press the 2 button you can drag rectangles. Please try and drag them fast and you will see them jump around.

I use the technique that if the offset Point. ( The first point on the mouse ) i less than the mouse coordinate in the drag then the x , y of the rectangle becomes the drag mouse coordinate. I think the problem lies here but I don't know why and what I'm doing wrong. Here is my code. It is under process so It is not built with speed in mind. If you have another approach to my problem then please tell me.



SCJP1.4
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Works better now. I changed the class names so you can run this as–is without name–clashing.
Made changes in each class, mostly finishing up the mouse code. Moving the Block rect
requires a different technique than dragging its sides/corners. mouseReleased is still
rough but I think enough of your question is answered now.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks alot

But there is still a major problem when resizing the block

When I place a block it works fine. When i want to resize it and drag ex upper right corner real fast the block "JUMPS". It means it does not retain x and y coordinates. I don't know why but and I can't it. Do you know what the problem is?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Do you mean that the entire Block moves with no edges/parts stationary?
Or that the lines of the Block cannot be drawn quickly enough with rapid
movement and appear jumpy?
Using the TesterRx class, when I drag the upper right corner slower or
faster, the block resizes okay. The lower left corner remains stationary,
the top side and right side move with the mouse pointer. The y coordinate
of the upper left corner changes and the x coordinate of the lower right
corner changes — but I would say this is the expected behavior. The
entire block does not translate when dragging a corner or side edge. The
moving lines, especially the ones connected to the mouse pointer, do
appear jumpy when the mouse is moved very fast. But this seems normal/not excessive for this kind of program.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Ok thanks.... I tried it in Indesign but it doesn't get jumpy there.

I have tried to create a rule that is for the grid mesurement. The TesterRX and the new panel is in the
same JScrollpane. When i try to scroll the JscollPane the RuleRx flickers a lot. It is a very small JPanel. I there a better way of doing this beside using image.



This is a mesure in mm and a pixel is 0.353 mm so I have to cheat a little as you can see.


// Mathias
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I added your RulerRx class and the RectsRx class to a JPanel and put it in a JScrollPane
and it worked okay — no flickering, jerkiness or rough behavior.

Have you seen how rulers are added to a custom component in the tutorial page How to Use Scroll Panes?
It is demonstrated in the section Providing Custom Decorations.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks!

It worked for me now. Many thanks.

Is there anything special that I should think about when designing this. Is there a better approach on dragging the rectangles? Or perhaps the onmose over method.

// Mathias
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem when repainting rectangles