The moose likes Swing / AWT / SWT and the fly likes tooltext or what? & how about a marquee? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "tooltext or what? & how about a marquee?" Watch "tooltext or what? & how about a marquee?" New topic
Author

tooltext or what? & how about a marquee?

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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


SCJP
Visit my download page
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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).


    The quieter you are, the more you are able to hear.
    Randall Twede
    Ranch Hand

    Joined: Oct 21, 2000
    Posts: 4347
        
        2

    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

    Joined: May 03, 2008
    Posts: 4658
        
        5

    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.


    luck, db
    There are no new questions, but there may be new answers.
    Randall Twede
    Ranch Hand

    Joined: Oct 21, 2000
    Posts: 4347
        
        2

    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

    Joined: May 03, 2008
    Posts: 4658
        
        5

    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().
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: tooltext or what? & how about a marquee?