Meaningless Drivel is fun!*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mini Paint Program" Watch "Mini Paint Program" New topic
Author

Mini Paint Program

Jess Te Wiata
Greenhorn

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
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
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 ?


Joanne
Jess Te Wiata
Greenhorn

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
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
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
Greenhorn

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: 462
    
    6
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?

Greetz,
Piet
Jess Te Wiata
Greenhorn

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.
 
 
subject: Mini Paint Program
 
Similar Threads
drawing arrows
Drawing a Diamond Shape (like Rectangle, Circle)
drawRect() issues
Help me how to drag and move drawing shaps
Single Main class into Multiple class.