1
votes

I have an application having five buttons:

i) Square ii) Rectangle iii) Circle iv) Triangle v) Clear

Clicking on any of the first four buttons will draw a shape on a drawing canvas (which is a JPanel).

Now the shapes drawn are stored in an ArrayList called Shapes. Now, I want to let the user click on any shape and drag it on the screen.

This is how my project is organized in terms of classes:

i) Square ii) Rectangle iii) Circle iv) Triangle v) Drawing Canvas (for the JPanel where the shapes are drawn) vi) Application (for the User Interface)

This is what I have done so far:

The first method checks whether the mouse cursor is inside the drawn shape. The second method draws the square.

The above mouse event handlers are supposed to drag the shape on the canvas. However, the shapes are not being dragged. How can I solve this problem please?

Note

The shapes are added to the arraylist correctly as they are drawn when clicking on any of the four buttons. What does not work is the dragging.

1
Do you know for sure that shapeUnderMouse is being set correctly?dashrb
Also, I thought that paintComponent() was the desired method to override, rather than paint().dashrb
No, I'm not sure. I will check at once.Matthew
I added System.out.println(shapeUnderMouse) in the loop and it is basically displaying all the shapes added to the canvas. For some reason, it is displaying the same shape five times. However, when I click on a shape on the canvas, nothing happens, so it is not working correctly.Matthew

1 Answers

0
votes

You could have a look at my customizer framework I wrote some years ago for such tasks: Move and resize components with the mouse and allow further customizations. "Snap-to-grid"-feature included!

It also supports moving shapes with JXIconCustomizer and ShapeIcon.

Maybe you find it useful. (It's open source!)

I started to write a tutorial (still under construction!):

http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/customizer/index.html

For adding components see:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/CustomizerBar.html

To customize properties use:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JCustomizerPropertyTable.html

Register it to the SelectionManager of the JCustomizerPane and set the CustomizableProperties-property of the JCustomizer objects.

Also have a look at the subclasses of JCustomizer.

E.g. there are

customizers for images & shapes:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JXIconCustomizer.html

a label customizer with inline editing:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLabelCustomizer.html

a line customizer:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLine2DCustomizer.html

And there are many classes providing support for menu- and toolbar-actions. (Incuding support for some of the "Java Look and Feel Graphics Repository" actions.)

Look at the *.swing and the *.swing.action packages.

Just ask me if you don't find them.

Homepage:

http://www.softsmithy.org

Download:

http://sourceforge.net/projects/softsmithy/files/softsmithy/

Maven:

<dependency>  
    <groupid>org.softsmithy.lib</groupid>  
    <artifactid>softsmithy-lib-core</artifactid>  
    <version>0.2</version>  
</dependency> 

API:

http://softsmithy.sourceforge.net/lib/docs/api/index.html