0

I have a program containing five buttons:

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

Clicking on any of the first four buttons will draw the respective shape on the drawing canvas using the following code:

The shapes can be dragged. They also should be resized. I was just trying to change the cursor of the mouse around the four edges of the shape (NW, NE, SW, SE). This works fine for squares and rectangles since they have four sides. However, I have some problem implementing the same for the circle and triangle.

This is what I did in my mouseClicked event:

What I want to do is that if the string shape_type contains circle, for example, it draws a border around it so that the user can see the boundaries of the shape.

However, I can't just use the following code inside the if statement:

How can I draw the border please? Thanks :)

Matthew
  • 4,477
  • 21
  • 70
  • 93
  • 2
    Why are you using `paint(..)` you should use a `JPanel` and override `paintComponent(..)`. Maybe see here for some usage of [`paintComponent(..)`](http://stackoverflow.com/questions/13358658/paintcomponent-draws-other-components-on-top-of-my-drawing/13359279#13359279). Also you will get better help sooner by posting an [SSCCE](http://sscce.org). – David Kroukamp Nov 21 '12 at 10:22
  • That's what the lecturer told us to do. I am quite new to GUI programming. In fact, this is my first Java GUI project. – Matthew Nov 21 '12 at 10:23
  • Basically, every shape has its own class, each containing its paint method. There is also a class called Shapes, and this is the place where the original paint method is. – Matthew Nov 21 '12 at 10:27
  • Ok. Thanks for the information nonetheless :) – Matthew Nov 21 '12 at 10:32
  • 1
    Shape.getBounds() as I mentioned. – StanislavL Nov 21 '12 at 10:50

1 Answers1

1

You are correct that you can't do painting in a MouseListener method. What you can do is set shapeUnderMouse in your mouseClicked method, then call repaint. You'll get better performance if you only repaint the areas that you know are changing visually, but it's not strictly necessary.

The if (shapeUnderMouse != null) block should be placed in your paint method.

VGR
  • 40,506
  • 4
  • 48
  • 63