[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: adding event to a line

If it's a horizontal or vertical line then you could use a Tracker instead.
For an example of this see
http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet23.java
(change the Rectangle's width or height from 100 to 2).

For lines that aren't horizontal or vertical you'll need a custom approach.
I've pasted an example of this below, which demonstrates a vertical line (I
chose this case because it's straight-forward).  You can substitute a
Composite for Canvas, it doesn't matter here.

public class Main4 {
    static Rectangle line = new Rectangle(100,100,1,100);
    static int pointerOffset = 0;
    static boolean isDragging = false;
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setBounds(10,10,400,400);
        shell.setLayout(new FillLayout());
        final Canvas canvas = new Canvas(shell, SWT.NONE);
        canvas.addListener(SWT.Paint, new Listener() {
            public void handleEvent(Event event) {
                event.gc.drawLine(line.x, line.y, line.x, line.y +
line.height);
            }
        });
        canvas.addListener(SWT.DragDetect, new Listener() {
            public void handleEvent(Event event) {
                if (Math.abs(event.x - line.x) < 2 && line.y <= event.y &&
event.y <= (line.y + line.height)) {
                    isDragging = true;
                    pointerOffset = event.y - line.y;
                }
            }
        });
        canvas.addListener(SWT.MouseUp, new Listener() {
            public void handleEvent(Event event) {
                isDragging = false;
            }
        });
        canvas.addListener(SWT.MouseMove, new Listener() {
            public void handleEvent(Event event) {
                if (!isDragging) return;
                canvas.redraw(line.x, line.y, line.width, line.height + 1,
false); // erase the old line
                line.x = event.x;
                line.y = event.y - pointerOffset;
                canvas.redraw(line.x, line.y, line.width, line.height + 1,
false); // draw the new line
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

Grant


"caesar" <caesarkim@xxxxxxxxxxx> wrote in message
news:h8a06s$m6p$1@xxxxxxxxxxxxxxxxxxxx
> I am trying to adding an event to a line so that I can drag a line on
composite.
>
> Here is my code.
>
>
>     final Composite composite = new Composite(scomposite, SWT.NONE);
>     Color backgroundColor = new Color(composite.getDisplay(), 246, 160,
11);
>     scomposite.setContent(composite);
>     composite.setLayout(null);
>     composite.setSize(editorWidth, editorHeight);
>     composite.setBackground(backgroundColor);
>     composite.addPaintListener(new PaintListener() {
> public void paintControl(PaintEvent e) {
>     e.gc.drawLine(Constants.LEFT_XCOORDINATE, Constants.LEFT_YCOORDINATE,
Constants.HORIZONTAL_WIDTH, 20);
>     e.gc.drawLine(20, 20, 20,
> }
>     });
>
>
> Is there a way to make a draggable line?  I want a user to drag a line on
a composite. It would be a great help if anybody tells me an alternative way
to do it.
>
> Please help.
>
> Thanks.