Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Did I need to add a condition for my mouseListener?

 
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One one of my homework questions, I got this one marked wrong and I still can't seem to figure out how to solve it. Was I missing a loop or a condition?

The question was as stated:
You have a GUI that keeps track of a list of pre-specified "special" points. If the user's mouse moves over one of these points, the point lights up. Write the code that would go inside of the handler/listener below. Use the references and also assume the existence of the method listed below (which you can invoke- you do not need to write).

--specialPointList // an ArrayList of the pre-specified "special" points; the type is ArrayList<Point>
--assume this method exists: public void lightUpAPoint(Point p)

Your code will go inside the handler method:


This was my answer:
 
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see at least two problems.

First of all you were supposed to assume that specialPointList was a reference to an ArrayList of the points. But instead you changed that variable to refer to a different ArrayList. (Which was empty, since you created it and didn't fill it.)

So the result of that would be that no points would ever light up, because your code wasn't looking at the special points.

However if you hadn't done that, then the second problem would appear. Namely, instead of lighting up the point that the mouse just went over, your code just lights up all of the special points. (I suppose that in one interpretation of the requirements, lighting up all of the points does cause the correct point to be lit up, and you could argue that the requirements didn't say that when the right point lights up, the wrong points must not light up. But you wouldn't win that argument.)

There could be other problems too but you'd have to fix the first two before looking for more.
 
Bartender
Posts: 3994
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Janny,

two questions:

1) you have this line in your mouseMoved code:

That does not compile. Do you mean:

If so, you are creating a new, empty, ArrayList<Point>, and you loose the reference to the existing specialPointList.
Why do you have this line of code?

2) You then have:

That is fine, but the next lines of code are:

That means that you light up every special Point!

So, what you should do is t check if the mousePoint is present in the specialPointList, and if so, light up that point. Don't forget to 'unlight' that point again when the mouse moves away.
 
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check if mouse point :

is inside your point to be lighted.
 
Janny Will
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would this work?

 
Janny Will
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**Edited

public void mouseMoved(MouseEvent event) {

   Point mousePoint = event.getPoint();
   for(Point point : specialPointList) {
       if(mousePoint.equals(point)) {
           lightUpAPoint(point);
       }
   }
}
 
Sheriff
Posts: 15754
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that is still wrong. (Was referring to the previous code)

Think about it like this: You know the game of Bingo? Assuming you do, think of the specialPoints list as your Bingo card. It has a bunch of numbers on it. You have to keep the same Bingo card throughout a round, right? When the caller shouts out a number, you look through your card and see if the number called is on the card you have. If the number is there, you go and put a mark on that number on your card. This goes on until you make Bingo or somebody else does. Make sense so far? Every time you hear a number called, that's like an event happening. Every time an event happens, you hear a certain number. That number is like the event.point. When you go look for the number on your card and mark it, marking the number is like making the point light up.

The code you originally wrote does the Bingo equivalent of this: every time the caller calls out a number, you go get a new Bingo card. That card has no numbers on it. Then you go and try to mark all the numbers on it.

P.S. It looks like you got the right code now but I'll post this anyway because I typed it all with just two thumbs (on my iPad) and the analogy is nice, in my totally biased opinion.
 
Junilu Lacar
Sheriff
Posts: 15754
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Janny, good for you for figuring it out. Since you got a zero from your teacher, I hope the cow will make you feel better.
 
Marshal
Posts: 69782
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many shapes in Swing and AWT have contains and intersects methods, so you can use those methods to find whether your mouse pointer is inside a shape.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic