aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Mini Paint Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mini Paint Program" Watch "Mini Paint Program" New topic

Mini Paint Program

Jess Te Wiata

Joined: Jun 03, 2014
Posts: 4
Hello, I am currently writing a small drawing program and I am having trouble with changing the size of the shapes. To do this, I have to access the arraylist shapes, check whether pressedX/pressedY is on any of the shapes in the arraylist using the findShape() method and then when released, uses moveBy() in the Rectangle/Oval/Line class and moveShape() in the miniDraw class to move the shape and draw it in the newreleasedX/releasedY position.

So far I think I have pin pointed the problem to being the method in all the shapes classes, that checks whether the pressedX/pressedY which is the on() method, and the findShape() method in the miniDraw class.

I am really noobish and I take ages to understand things so bear with me if either there are a lot of silly things in the code or the answer is really simple.

Help is much appreciated.

This is the minidraw class

This is the Rectangle class

This is my Oval class

And the interface class

Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Jess Te Wiata wrote:I think I have pin pointed the problem

You may have pinpointed the problem, but you haven't actually told us what the problem is.
Is the code doing something that you didn't expect it to do ?
Is the code not doing something that you wanted it to do ?
Something else ?

Jess Te Wiata

Joined: Jun 03, 2014
Posts: 4
Ah, yes, sorry. The shapes are not moving when I click and release. At one point it was giving me null pointer exceptions but I managed to fix that and now it compiles and runs fine, but it still doesn't move the shapes.
Tony Docherty

Joined: Aug 07, 2007
Posts: 2679
Without the classes in the ecs100 package we can't compile and run your code which makes it harder for us to see what is happening.

May I suggest you add a lot more trace statements so you can see the program flow.
One thing that struck me as odd was in your mousePerformed method you add a new shape every time the mouse is released, are you sure this is correct?
Jess Te Wiata

Joined: Jun 03, 2014
Posts: 4
Yea, I need to add comments. I would probably pick up errors a lot quicker.

Ah yes, I see that and I will investigate it. Maybe that is the reason the shape is not showing up. It's a good start and I didn't pick up on it. Thank you.
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 853
hi Jess,

in your opening post you mentioned having problems with the 'findShape' method. If you look at the code:

you can see that when you do find a Shape s, you nevertheless return 'currentShape'.
You also do not use the parameters of this method, but you use the variables 'pressedX'
and 'pressedY'. These also happen to be the actual parameters when you invoke this
method, so it will not make any difference here.

Be aware though, that when shapes overlap, then you will only find one of these, namely
the one that comes first in the 'shapes' array. In these cases, you could for instance use a
double click, or shift-click, to select another shape. It will make you program a bit more
complex. How do you display a selected shape? By putting some squares in the corners?

Jess Te Wiata

Joined: Jun 03, 2014
Posts: 4
So, when I call currentShape, it's not returning the shape that x, y is on, it's returning whatever the currentShape is at the time? In that case I should be using the arraylist.get(index) to return whatever element makes s.on(pressedX, pressedY) true?

The code is only supposed to selected the topmost shape but I am all for making the program nicer. Once I get the shapes moving, I am definitely going to look into other ways I can make the program better.
I agree. Here's the link:
subject: Mini Paint Program