• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Draw circles to screen using mouse listener java applet?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


Thanks
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post the same question multiple times.

What was the matter with the answer I gave you in the other forum?

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.
 
william phillips
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)


finally attempted to draw the array of objects in the paint method


nothing else changed.

the code compiles but with the error:

christmasTree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

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
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I thought all of the mouse methods needed to be called when a MouseListener was used?
reply
    Bookmark Topic Watch Topic
  • New Topic