import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class PictureLabel3 extends Canvas{
Color white;
int x , y;
int width , height;
Listener listener = new ListenerImpl(getShell());
public PictureLabel3(final Composite parent, int style , int width , int height) {
super(parent, style);
this.width = width;
this.height = height;
// TODO Auto-generated constructor stub
white = new Color(null, 255, 255, 255);
setBackground(white);
addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
// TODO Auto-generated method stub
PictureLabel3.this.widgetDisposed(e);
}
});
addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
// TODO Auto-generated method stub
PictureLabel3.this.paintControl(e);
}
});
addMouseTrackListener(new MouseTrackListenerImpl(getShell()));
}
class ListenerImpl implements Listener
{
private Shell shell;
private boolean SE = false;
private boolean NW = false;
private boolean NE = false;
private boolean SW = false;
private boolean N = false;
private boolean W = false;
private boolean S = false;
private boolean E = false;
public ListenerImpl(Shell shell) {
// TODO Auto-generated constructor stub
this.shell = shell;
}
@Override
public void handleEvent(Event event) {
// TODO Auto-generated method stub
Point size;
switch (event.type) {
case SWT.MouseDown:
x = event.x;
y = event.y;
System.out.println("MouseDown " + x + " " + y);
size = computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
if((event.x > width - 8 && event.x < width + 8) && (event.y > height - 8 && event.y < height + 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZESE));
System.out.println("SE");
SE = true;
}
else if((event.x > -8 && event.x < 8) && (event.y > -8 && event.y < 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZENW));
NW = true;
}
else if((event.x > width - 8 && event.x < width + 8) && (event.y > -8 && event.y < 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZENE));
NE = true;
}
else if((event.x > -8 && event.x < 8) && (event.y > height - 8 && event.y < height + 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZESW));
SW = true;
}
else if((event.x > width / 2 - 8 && event.x < width / 2 + 8) && (event.y > -8 && event.y < 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEN));
N = true;
}
else if((event.x > -8 && event.x < 8) && (event.y > height / 2 - 8 && event.y < height / 2 + 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEW));
W = true;
}
else if((event.x > width / 2 - 8 && event.x < width / 2 + 8) && (event.y > height - 8 && event.y < height + 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZES));
S = true;
}
else if((event.x > width - 8 && event.x < width + 8) && (event.y > height / 2 - 8 && event.y < height / 2 + 8))
{
// shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEE));
E = true;
}
break;
case SWT.MouseMove:
if((event.stateMask & SWT.BUTTON1) == 0)
{
size = computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
System.out.println("size " + size.x + " " + size.y);
System.out.println("e " + event.x + " " + event.y);
if((event.x > width - 8 && event.x < width + 8) && (event.y > height - 8 && event.y < height + 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZESE));
}
else if((event.x > -8 && event.x < 8) && (event.y > -8 && event.y < 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZENW));
}
else if((event.x > width - 8 && event.x < width + 8) && (event.y > -8 && event.y < 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZENE));
}
else if((event.x > -8 && event.x < 8) && (event.y > height - 8 && event.y < height + 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZESW));
}
else if((event.x > width / 2 - 8 && event.x < width / 2 + 8) && (event.y > -8 && event.y < 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEN));
}
else if((event.x > -8 && event.x < 8) && (event.y > height / 2 - 8 && event.y < height / 2 + 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEW));
}
else if((event.x > width / 2 - 8 && event.x < width / 2 + 8) && (event.y > height - 8 && event.y < height + 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZES));
}
else if((event.x > width - 8 && event.x < width + 8) && (event.y > height / 2 - 8 && event.y < height / 2 + 8))
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEE));
}
else
{
shell.setCursor(Display.getDefault().getSystemCursor(SWT.CURSOR_ARROW));
}
break;
}
if(SE)
{
System.out.println("SE");
Point point = getLocation();
int x = point.x;
int y = point.y;
System.out.println("event " + event.x + " " + event.y);
System.out.println("size " + width + " " + height);
if(event.x > 50)
width = event.x;
if(event.y > 50)
height = event.y;
System.out.println((width) + " " + (height));
setSize(width, height);
redraw();
}
else if(NW)
{
System.out.println("NW");
Point point = getLocation();
int x = point.x + width;
int y = point.y + height;
if(width - event.x > 50)
width -= event.x;
if(height - event.y > 50)
height -= event.y;
setLocation(x - width, y - height);
setSize(width, height);
redraw();
}
else if(NE)
{
System.out.println("NE");
Point point = getLocation();
int x = point.x;
int y = point.y + height;
if(event.x > 50)
width = event.x;
if(height - event.y > 50)
height = height - event.y;
setLocation(x, y - height);
setSize(width, height);
redraw();
}
else if(SW)
{
System.out.println("SW");
Point point = getLocation();
int x = point.x + width;
int y = point.y;
if(width - event.x > 50)
width = width - event.x;
if(event.y > 50)
height = event.y;
setLocation(x - width , y);
setSize(width, height);
redraw();
}
else if(N)
{
System.out.println("N");
Point point = getLocation();
int x = point.x;
int y