Results 1 to 12 of 12
  1. #1
    Tyler is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Post Draw rectangle by dragging mouse

    Hi all,
    I want to draw a rectangle by dragging mouse. I am getting co-ordinates of starting point and endpoint but rectangle is not drawn.
    Please help me about this.
    Thanks in advance...
    Last edited by Tyler; 07-14-2010 at 09:54 AM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Post the code. How are we supposed to tell you what to change, without seeing the code?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Tyler is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Post

    Hi PhHein,

    I modified my code. now rectangle is getting drawn by dragging mouse but it not showing path where we are drawing(like in paint , when we draw a rectangle it shows all its corners)

  4. #4
    Tyler is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Post

    Here is a code,

    public void makeRectangle(int x1, int y1, int x2, int y2,Display display) {
    // TODO Auto-generated method stub
    GC gc = new GC(display);
    gc.drawRectangle(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    gc.dispose();
    }

    public void run(IAction action) {
    // TODO Auto-generated method stub
    final Display display = PlatformUI.getWorkbench().getDisplay();
    final Shell shell = display.getActiveShell();
    Cursor crossCursor = display.getSystemCursor(SWT.CURSOR_CROSS);
    shell.setCursor(crossCursor);

    shell.addPaintListener(new PaintListener(){
    public void paintControl(PaintEvent e) {
    // TODO Auto-generated method stub
    if (startDrag != null && endDrag != null){
    makeRectangle(startDrag.x,endDrag.y,e.x,e.y,displa y);
    System.out.println("In paintlistener StartDrag:"+startDrag+" "+"end drag:"+endDrag);
    }
    }
    });
    shell.addMouseListener(new MouseAdapter(){
    public void mouseDown(MouseEvent e){
    startDrag = new Point(e.x, e.y);
    endDrag = startDrag;
    System.out.println("In mouse Down StartDrag:"+startDrag+" "+"end drag:"+endDrag);
    //shell.redraw();
    }
    public void mouseUp(MouseEvent e){
    makeRectangle(startDrag.x,startDrag.y,e.x,e.y,disp lay);
    System.out.println("In mouse Up StartDrag:"+startDrag+" "+"end drag:"+endDrag);
    System.out.println(e.x+" "+e.y);
    startDrag = null;
    endDrag = null;
    // shell.redraw();
    }
    });
    shell.addMouseMoveListener(new MouseMoveListener(){
    public void mouseMove(MouseEvent me){
    endDrag = new Point(me.x,me.y);
    System.out.println("In mouse move StartDrag:"+startDrag+" "+"end drag:"+endDrag);
    shell.redraw();
    }
    });
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    not showing path
    Please explain again. I don't understand what "path" is here?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Cross-posted in the JavaRanch swt forum. Please read what JavaRanch has to say about cross-posting in their FAQ's.

  7. #7
    Tyler is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Post

    path means all the sides of rectangle..

    During dragging mouse it is not showing rectangle which is being drawn....

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    You tried debugging the code by adding println() to show where the control is going and what the values are that you used to draw with.
    What do you see when you execute the code? Where is the program executing and what are the values?

  9. #9
    Tyler is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Post

    Values which I am getting are correct. Problem is GC is not drawing rectangle according to that values.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Can you post the debug output that shows what's happening?
    GC is not drawing rectangle
    Where is GC define? Without seeing the code, its very hard to help.

    Can you make a small executable program that demos the problem?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Spammer linzh6688 reported.

    db

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 7
    Last Post: 04-10-2010, 07:07 PM
  2. drawing Line by dragging mouse !!!
    By h9h in forum Java 2D
    Replies: 14
    Last Post: 10-23-2009, 05:10 AM
  3. Replies: 7
    Last Post: 04-14-2009, 07:12 PM
  4. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 12:17 AM
  5. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •