Draw circles to screen using mouse listener java applet?

Hi I am trying to create an applet that draws a christmas tree then using buttons the user can draw decorations (just circles at the moment) to the tree by clicking on a location on the tree. So far i've got it so that when i select a button and click on the tree it draws the desired circle however when i click somewhere else it removes the first circle, and the same when i click on another button to draw a different colored circle. My question is how to I get the circles to remain on the screen when I click to draw another one? I think the circles need to be defined as objects but i'm not sure how to do this. Any help would be much appreciated. Here is my code:

Just as multiple posts in the same forum create confusion and extra work, posting the same question in multiple forums isn't appreciated unless you let us know about it and maybe provide a reason for it, like "I also posted this question <here>, but I didn't understand the answers I got." (They were mean to me, etc.) And if that were the case, you should let those who answered in the other forum know you're still confused (or being mean) and give them a chance to explain (be nice).

P.S. - I've seen apologies for posts across multiple forums that went something like, "It was late, I posted multiple places, and now I'm on a different computer, and I can't find any of them." Kinda funny. In that case, I wouldn't expect the new posts to be nearly exactly the same as the late night ones, but I suppose it could happen.

Hi Greg, thanks for your reply, your answer in the other forum was a good answer, however I became stuck again so i thought I would post somewhere else and see if I might find something. I'm sorry about this,it wont happen again.
In response to your reply on the other forum, I tried to create a ball object and just draw it to the screen using a mouse click to start with however it was just drawn in the top left corner of the screen and mouse clicks weren't doing anything. my updated code is here, i am really starting to pull my hair out over this, and yes i have posted it in other forums aswell, I wont do this again. Im a beginner to java so please bear with me.

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563

posted Aug 21, 2012 16:10:02

0

You're close.

You declared an ArrayList, lightArray, but didn't do anything with it. What I suggest you do is:

Use a generic ArrayList<Ball>
Add a Ball( int x, int y ) constructor. (Add a color option variable, too? Or set the color randomly in the new constructor?)
When the mouse is clicked (pressed), create an instance of Ball using the new constructor with x/y set to the cursor's current position.
Add the new instance of Ball to the lightArray ArrayList Iterate the lightArray ArrayLlist in the paint() method, drawing each ball instance it contains.

Why aren't you using Swing, by the way?

Try making those changes and let us know if/when you need help. I haven't made all those changes myself, so I you may find something I missed, but errors are hopefully minimal.

Edit: I made all the changes I suggested, except I didn't mess with the colors, and it works great. If you're uncomfortable or unable to use generics, you don't have to, but it's the most 'proper.'

william phillips
Greenhorn

Joined: Aug 21, 2012
Posts: 6

posted Aug 21, 2012 16:49:54

0

ok so i created an arraylist:

added the Ball(int x, int y) constructor into my Ball class:

created an instance of Ball using the new constructor:

i did this in the init() method

then added the new instance of Ball to the lightArray ArrayList (in the mousePressed() method)

also it doesnt work, even the strings displaying the point locations of the mouse clicks are not showing up.

Sorry to keep hassling you, but any ideas?

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563

posted Aug 21, 2012 17:03:11

0

It's a lot easier to work the total revised source code rather than the bits and pieces, guessing where you actually stuck them, but I'll try. And always post your exact errors, copied and pasted from what you get at your end.

This declares lightArray to be an ArrayList of Ball objects and should be placed in you instance variable area:
ArrayList<Ball> lightArray;

The next line should occur in your init() method:
lightArray = new ArrayList<Ball>();

You should remove:
ball = new Ball();
from your original init() method.

You'll notice in my instructions, I said a ball instance should be created for each mouse click, so I'll show that whole updated method as I wrote it:

Or simplified so that an instance of ball isn't needed:

Then, the only thing you have to add to your paint() method is an iteration of lightArray that draws each ball, something like:

It could be done more elegantly, like with an enhanced for loop.

Can you make those changes and get it to work? Post your whole updated source next time with any errors you're getting.

william phillips
Greenhorn

Joined: Aug 21, 2012
Posts: 6

posted Aug 21, 2012 17:20:22

0

Got it working, thank you so much for your help. I think I can figure out how to get the buttons to draw different decorations to the tree myself. Here is the final code anyway

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563

posted Aug 21, 2012 17:26:37

0

Glad you got it. Other things you might be able to adjust:

You're not using the MouseMotionListener interface, but perhaps that's work to be done.
You have interfaces and methods in the Ball class that will probably never be used.

Here's the enhanced for loop I mentioned before. I needed the practice:

william phillips
Greenhorn

Joined: Aug 21, 2012
Posts: 6

posted Aug 21, 2012 17:36:59

0

Thanks, I thought all of the mouse methods needed to be called when a MouseListener was used?