Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hot to select a region with mouse,java  RSS feed

 
Aggelos wat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,
It is my first time here,so I hope to be patient with me,and sorry if my english is not so good.
I want to ask you how i can select-paint a region on java. I have made a project at Netbeans,and I have a background (.GIF) . So, I want to choose a part of this background as I am in Paint and make a circle or square or something like this.
I hope to make it easy for you and you will understand.
Thank you
 
Piet Souris
Rancher
Posts: 2015
71
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Aggelos,

If I understand you correctly, you have an image in a panel, and you want to draw a
rectangle or a circle over it, maybe with the intention to cut that part and save it
somewhere.

Well, in principle that is easy to do, but I have found it to be a lot more nasty than I
had anticipated.

The idea is: you put both a MouseListener and a MouseMotionListener to your panel.
Then, when you receive a mouseClicked event, you store the coordinates. Then it
is your MouseMotionListener that takes over. You will get mouseDragged events,
and for every mouse-coordinate pair you have to draw a rectangle or a circle,
using the starting point and the end point.

That may sound easy, but there is the erasure of your former rectangle or circle.
I solved that by first using a BufferedImage for my original background, painting
that after each mouseDragged event, and then drew the new rectangle/circle.

Also, when you drag the mouse to the upperleft of your start point, you must take care
when drawing a rectangle. And lastly, if you use a photo that is much bigger than your
screen, you have to work with scaled images, adding to the complexity.

Anyway, I hope you have some ideas how to proceed.

Greetings,
Piet
 
Randall Twede
Ranch Hand
Posts: 4691
8
Java Scala
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can get a working version of what you are trying to create at my download page(look at the bottom)
i will post one of the classes for you. even though you might lose something from not figuring it out yourself. not a direct answer, but it should be helpful.
 
Aggelos wat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Randall Twede I want to thank you very much. I have a small project and I have to make it until 2 days. So,I read and use your code in a few days. Thank you very much and I let you know if it is helpful for me !
 
Aggelos wat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Piet Souris Thank you very much for your interesting. I have to make a project until 2 days,so I read and work again with this project in a few days. I hope all of these are helpful. Thank you again !
 
Aggelos wat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Randall Twede

I think that your answer is helpfull for me.But as I said,I am s beginner so I have a lot of difficulties. I decide to ask you. I hope,you do not have any problem.

So,I run your application at your page www.angelfire.com/games2/programming/downloads/ ,especially the paint example. I do not want to make a paint application but a part of your project is useful for me. I want to have a specific background and user has the ability to choose or paint or mark a region of this background.

In your project,you have the choise to crop the image.I think that it is something like this,which I want to do.I make my projects at Netbeans,and I want to ask if it is possible to use your code and how can I use it only the part with the Crop.
Especially, I do not want to crop the background but I want ,user can choose this region and I know the height and the width of this region as you do on your project.

Can you help me ? I hope to not tired you.I am looking forward about your answer,positive or negative.
Thank you about your time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!