Results 1 to 10 of 10

Thread: Painting in SWT

  1. #1
    jionnet is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default Painting in SWT

    Hi
    I am trying to write a paint program. In that I want to draw an object with mouse move event. (eg : Here I used OVAL object)

    My problem is, when i move mouse the object is also moving with the mouse.. But I want it like a drawing...


    SAMPLE CODE:

    class MAINCLASS
    {
    drawingCanvas = new Canvas(arg0, SWT.NONE);
    drawingCanvas.addPaintListener(new OvalExamplePaintListener());
    int X , Y;
    Listener listener = new Listener () {
    public void handleEvent (Event event) {
    switch (event.type) {
    case SWT.MouseDown:
    Rectangle rect = drawingCanvas.getBounds ();
    if (rect.contains (event.x, event.y)) {
    Point pt1 = drawingCanvas.toDisplay (0, 0);
    Point pt2 = drawingCanvas.toDisplay (event.x, event.y);
    offset [0] = new Point (pt2.x - pt1.x, pt2.y - pt1.y);
    }
    break;
    case SWT.MouseMove:
    if (offset [0] != null) {
    X= event.x;
    Y= event.y;
    drawingCanvas.redraw();
    }
    break;
    case SWT.MouseUp:
    offset [0] = null;
    break;
    }
    }
    };



    private class OvalExamplePaintListener implements PaintListener {
    public void paintControl( PaintEvent e) {
    e.gc.setLineWidth(2);
    e.gc.drawOval(X, Y , 10, 10);
    }
    }


    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    My problem is, when i move mouse the object is also moving with the mouse
    How do you want the drawing to respond to the mouse?

    Is the problem because the x,y location you use to draw is changing?
    Then you need logic to set it once and not change it until ....

    BTW Please use code tags when posting code. Info here:Java Forums - BB Code List

  3. #3
    jionnet is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default Thanks

    Hi
    Thanks for reply..
    Yes I want to draw with mouse move...
    Getting X,Y are fine...
    the problem is..
    When I change the X,Y of the object with the mouse pointer X,Y it happen like moving not like drawing...

    ex:
    Java Code:
     mouse_move_event(X , Y)
     {
       drawOval(x,y,width, height)
     }
    Now, it doesnt seem like drawing... It happen to move the Oval...

    Is this clear?
    Last edited by Fubarable; 09-15-2010 at 03:06 PM. Reason: Moderator edit: code tags added

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

    Default

    Post above edited and code tags added. OP, please have a look in my signature link to the forum FAQ's on how to add code tags.

    Luck.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    When I change the X,Y of the object with the mouse pointer X,Y it happen like moving not like drawing...
    Can you explain what you mean by "moving" and by "drawing"?

    Add some print outs to your code to show the values of x and y and offset[0] and to show the execution flow through your logic.

  6. #6
    jionnet is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.ui.part.ViewPart;
    import com.ibm.collaboration.realtime.chatwindow.ChatWind owHandler;
    import com.ibm.collaboration.realtime.chatwindow.addon.Ch atWindowExtension;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Layout;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.graphics.Cursor;
    import org.eclipse.swt.graphics.RGB;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.graphics.GC;

    import org.eclipse.swt.widgets.Canvas;


    import org.eclipse.swt.events.*;


    public class ChatWindowExtension1 extends ViewPart implements
    ChatWindowExtension {

    ChatWindowHandler handler;
    public static int X = 0; // X Coordinate
    public static int Y = 0; // Y coordinate

    private static Display display;
    static Canvas drawingCanvas;

    public void createPartControl(Composite arg0) {
    display = Display.getDefault();

    drawingCanvas = new Canvas(arg0, SWT.NONE );

    Color color_white = display.getSystemColor(SWT.COLOR_WHITE);
    drawingCanvas.setBackground(color_white);

    drawingCanvas.addPaintListener(new OvalExamplePaintListener()); //Paint Listener to draw oval on mouse move

    final Point [] offset = new Point [1];

    Listener listener = new Listener () {
    public void handleEvent (Event event) {
    switch (event.type) {
    case SWT.MouseDown:
    Rectangle rect = drawingCanvas.getBounds ();
    if (rect.contains (event.x, event.y)) {
    Point pt1 = drawingCanvas.toDisplay (0, 0);
    Point pt2 = drawingCanvas.toDisplay (event.x, event.y);
    offset [0] = new Point (pt2.x - pt1.x, pt2.y - pt1.y);
    }
    break;
    case SWT.MouseMove:
    if (offset [0] != null) {
    X= event.x; // When the Mouse is moved, X and Y cooridinate of mouse is moved to X and Y (local Variable)
    Y= event.y; //
    drawingCanvas.redraw();
    }
    break;
    case SWT.MouseUp:
    offset [0] = null;
    break;
    }
    }
    };
    drawingCanvas.addListener (SWT.MouseDown, listener);
    drawingCanvas.addListener (SWT.MouseUp, listener);
    drawingCanvas.addListener (SWT.MouseMove, listener);
    }



    private void drawobject()
    {
    drawingCanvas.redraw();
    }



    private class OvalExamplePaintListener implements PaintListener {
    public void paintControl( PaintEvent e) {
    gc= new GC(drawingCanvas.getDisplay());
    int width = 5;
    int height = 5;
    e.gc.setLineWidth(3);
    e.gc.drawOval(X, Y , width, height);
    e.gc.dispose();
    }
    }



    public void setFocus() {
    // TODO Auto-generated method stub
    }

    public ChatWindowHandler getChatWindowHandler() {
    // TODO Auto-generated method stub
    return handler;
    }

    public void setChatWindowHandler(ChatWindowHandler arg0) {
    // TODO Auto-generated method stub
    //this.handler = handler;
    }
    }



    This is the code:

    simple thing is I want a paint application...

    Moving -> Moving object from X1 position to X2 position without any trace on moving path

    Panting -> While moving the trace or mark of moving will be on the path ( Paint application)


    Sorry dude if I am confusing... Is this clear? :)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    Moving object from X1 position to X2 position without any trace on moving path
    Does this mean that you only what the drawing done twice, once when the mouse starts moving and once when the mouse stops moving?
    While moving the trace or mark of moving will be on the path ( Paint application)
    Does this mean that you want the movement to show a trace of all the positions of where the the mouse has been as it moves from one location to another.
    Is this clear
    No.
    Those two options seem the opposite to me. Can you explain in more detail what you are trying to do?

  8. #8
    jionnet is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks a lot for your help dude...

    Second one is what I want.. I want to draw all the path that mouse move...

    ================================================== ========
    what I want is to make a simple paint application.
    ================================================== ========



    I have done with the paint application... Now I can draw with the mouse move.. But I have a problem in that..

    I use SWT canvas to draw... The drawing are all goes fine....

    After drawing some line on canvas, when we make some application window to overlap or move on top of my paint application, the overlaped portion becomes blank or it is cleared...

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    Sounds like your code is not redrawing all of the parts you want to see. Without sample code it is hard to debug.
    Can you make a small program that compiles, executes and demonstrates the problem?
    I don't have the packages you are using, but maybe someone else does.

    OR make a small program using Java SE SDK classes to demo the problem.
    Last edited by Norm; 09-16-2010 at 03:21 PM.

  10. #10
    jionnet is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default Thanks

    Thanks Norm... Since I could use my system for these days I couldnt reply... Will post you the code...

Similar Threads

  1. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  2. Need help with synchronous painting
    By JavaRulez in forum Advanced Java
    Replies: 7
    Last Post: 05-09-2010, 08:34 AM
  3. Images don't painting
    By daniel.henrique in forum New To Java
    Replies: 2
    Last Post: 02-12-2010, 11:48 AM
  4. Objects painting themselves?
    By martypapa in forum Java 2D
    Replies: 19
    Last Post: 02-06-2010, 04:08 AM
  5. painting problem
    By hannes in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 11:44 AM

Posting Permissions

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