This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

tooltext or what? & how about a marquee?

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am adding a crop feature to my paint program. i am going to(for now) draw a rectangle and then pop up a dialogue asking crop/cancel. i would like it so when the mouse is being dragged it shows numbers near the cursor showing relative x and y coordinates(width and height). i would also like it if instead of drawing a solid rectangle, i drew a marquee. any help or ideas welcome
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would certainly get more help here if you showed what you have done so far (if anything).

Suppose you have a custom panel defined as a canvas for working with image (say, ImagePanel which extends JPanel). For what you want, you only need to keep track on two points: starting (point where user has clicked mouse and began marking an area to crop) and ending (point where user released mouse click). With those you have actually marked an area to crop from original image. These would be some steps you should follow in order to get it done:
  • For getting a starting point, you need to register MouseListener to ImagePanel. Since you just use two events, MouseAdapter would do the job, where you would override mousePressed(MouseEvent event) which will get you starting point and mouseReleased(MouseEvent event) which you can use to reset starting point to null so area to crop will not be displayed when user finishes cropping.
  • In order to obtain coordinates of mouse cursor over canvas, you need to register MouseMotionListener to your ImagePanel. Since you want to get that information only when drawing area to crop, MouseMotionAdapter would do the job, where you would override mouseDragged(MouseEvent event) method. From this event you can get ending point for cropping area.
  • In addition, your canvas should override paintComponent(Graphics g) method in order to draw a rectangle marking area to crop.


  • So, basically, you would have something like this:

    You should also check Graphics2D#setStroke(Stroke stroke) method to make your rectangle be dashed (or whatever).
     
    Randall Twede
    Ranch Hand
    Posts: 4716
    9
    Scala Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i have already done ALL those thing. i am just adding a new feature. i just want to know how to display a tooltip kind of thing showing the height and width of the selection, and to draw a marquee instead of a regular rectangle(or nothing)
     
    Bartender
    Posts: 5167
    11
    Netbeans IDE Opera Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    1. Use a JWindow with setAlwaysOnTop(true) and position it relative to the mouse coordinates. Have the JWindow contain a label/labels or text component(s) to show the coordinates.

    2. What's a marquee? I guess you're not talking about the IE HTML <MARQUEE> tag.
     
    Randall Twede
    Ranch Hand
    Posts: 4716
    9
    Scala Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks Darryl. a marquee looks like crawling ants around the border of a selection. im not sure if java has anything like drawMarquee(), i may have to do it myself
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Netbeans IDE Opera Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Check out BasicStroke. Since it doesn't have a setDashPhase(...) you'll probably need to construct a new stroke each time, using a Swing Timer, and call repaint().
     
    Evacuate the building! Here, take this tiny ad with you:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!