aspose file tools*
The moose likes Beginning Java and the fly likes MouseListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "MouseListener" Watch "MouseListener" New topic
Author

MouseListener

W Mckelvey
Greenhorn

Joined: Jun 30, 2011
Posts: 5
I have a program that creates random generate shapes on a canvas when the mouse is clicked. I then change the color of the shape if it already exists on the canvas. Right now, my code changes the shape to red and creates a new random shape. How can I get it to just color the shape without creating a new random shape. I tried adding the shapes to the arraylist and repaint right after coloring the shape but I get compile errors. Any nudge in the right direction would be appreciated. Thanks

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Looks okay to me. (Except for the title: you certainly wouldn't use a MouseListener to paint on a canvas.)

But you say you get compile errors? Then you should read them and fix your code accordingly. Of course you could TellTheDetails here if you were having problems understanding them.
W Mckelvey
Greenhorn

Joined: Jun 30, 2011
Posts: 5
I get the compile Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException when i try and add the shape back to the arraylist and repaint if it finds the shapes that was clicked.

My goal is to have it find a shape on the canvas and paint it a different color. Right now, it changes the color of the shape it finds and creates a new shape in the same mouse click location.




Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You're iteration through a List. During that iteration you're trying to add an element to that List. That is not allowed resulting in the exception you're getting. You probably want to rethink your algorithm.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Also, why are you trying to add shape to shapeList when it's already a member of the list?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
W Mckelvey wrote:I get the compile Exception . . .
[pedantic mode]There is no such thing as a compile Exception, and that code compiles correctly. You mean an Exception at run-time.[/pedantic mode]
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
This may sound very simplistic but have you tried an if/else condition? It could work if a new Shape, with matching values to an existing Shape, is considered equal to the other Shape. It all comes down to both the equals() method of your Shape objects and how your List evaluates its contains() method.


Aside, Is there something about switch that turns people away from using both if/else and switch in the same place? Like a mental block (I think I might also have this )?


Also, I really shouldn't encorage you to hog the Event dispatching Thread, with these things, by running several possibly long-running computations, which will freeze other Event calls until it returns. Perhaps you could call a method on an existing class, or use a Runnable, Thread, start()?

Finally a mention, because you reported a ConcurrentModificationException, that ArrayList isn't synchronized : Read the Java SE API's java.util.ArrayList class description -- "Note that this implementation is not synchronized.", and this could cause problems with special methods like paint().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MouseListener