Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tooltext or what? & how about a marquee?

 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • 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
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • 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: 4481
    3
    Java Python Scala
    • 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)
     
    Darryl Burke
    Bartender
    Posts: 5148
    11
    Java Netbeans IDE Opera
    • 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: 4481
    3
    Java Python Scala
    • 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: 5148
    11
    Java Netbeans IDE Opera
    • 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().
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic