Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseEvent (mousePressed and mouseReleased) only works from left to right?

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

First of all Happy Holidays to everyone!! I have a question regarding a MousEvent, i have a small Graphics program where i draw some simple shapes and am using the MouseEvent for the beginning and end posistion. But it seem after using some System.out.println()'s that it is only picking up the mouseReleased when i go to the right and down, but nothing when i go to the left or up? I am appearently missing something or there is another method i can use instead?



thanks for any help in advance!!
Mike
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indeed you are. it isnt really the mouse events but what drawRectangle and drawOval and such require. i probably shouldn't just give you the answer but i will anyway
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randall,

Thanks for the help, I will have to read over that some more to understand it still.And try it with my code once i get it ;)

Thanks
Mike
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just call that private method before you draw your shape i did a project that started out just like yours. that's how i know. i would post the whole thing but why spoil your fun
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have it working thanks allot!! The only problem with this is when drawing a straight line it won't work. I am working on how to do that now. I have another question regarding this program and the question is, In Photoshop for instance when i am drawing a shape i will get the broken line marking the area when the left mouse button is clicked , until i release it and then it is gone and the shape is there.This helps to see exactly where the shape will end up. Is this something that also has to be done with a mouseEvent or is it part of the Operating system i am using and just need to utilize this in the OS?

Also, in Photoshop the shapes are each thier own (JPanel? i think), so you can move it around and resize it, is this also possible in Java?

Thanks
Mike
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ryan wrote:Is this something that also has to be done with a mouseEvent or is it part of the Operating system i am using and just need to utilize this in the OS?

You have to code it yourself, and you'll likely need to familiarize yourself with BasicStroke and setStroke(...).
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Daryll!

I will read up on the BasicStroke.
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i did was start with a homework that was just like you are doing now. you are correct in that you deal with drawLine differently than with drawRectangle or drawOval. it is actually easier. you dont have to call the method. just use the starting and ending x and y. i also didn't like how the program waited until i released the mouse before it drew and stored the shape. so i changed that so it draws the shape on mouseDragged and only stores it on mouseReleased. it will be more fun if you figure this out by yourself(at least i had fun improving it). if you really get stuck i will post my whole program. i am just looking for a reason to "show it off" hehe

i also improved it further using Graphics2d methods to turn antialiasing on and also to setStroke.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randall,

You are correct i am doing homework however the "Homework" part i already have done, but this course is older and i also like to do some extra methods and things to just make it work more like i would like it too;) I actually had started trying to use the mouseDragged event as it just seemed to make sense, but i haven't got it working yet.I also agree that it is fun working things out on your own(as long as it doesn't give me a heart attack ), but sometimes i get stuck and then i ask questions here ;)
The Graphics2D methods look cool as well but i will have to do some testing with that still.

Thanks
Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic