how to select particular rectangle box from canvas? [message #1748238] |
Mon, 21 November 2016 13:21 |
Sumit Fataniya Messages: 12 Registered: July 2016 |
Junior Member |
|
|
Qes: I have develop simple example base on SWT. Now I want to select a particular rectangle using selection or any other event? how to give selection event to particular rectangle?
Code:
public class DrawRoundRectangle {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setText("Canvas Example");
shell.setLayout(new GridLayout(1, true));
final Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
canvas.setLayout(null);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
// Point p = canvas.getSize();
for (int i = 0; i < 10; i++) {
// e.gc.drawRoundRectangle(10, 10, e.width, e.height, 0, 0);
e.gc.drawRoundRectangle((6 * i), (6 * i), e.width
- (14 * i), e.height - (14 * i), 0, 0);
e.gc.drawText("", (6 * i), (6 * i));
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
|
|
|
Re: how to select particular rectangle box from canvas? [message #1748422 is a reply to message #1748238] |
Wed, 23 November 2016 00:53 |
Eclipse User |
|
|
|
When drawing on a canvas, you are responsible for managing your objects. SWT is just manipulating a bitmap on your behalf: it doesn't know that you're manipulating some model. So you would need to add a MouseListener and then map the x,y back into your object model.
For a higher-level object framework, take a look at draw2d or its much more complex bigger brother, GEF, or JavaFX.
|
|
|
Powered by
FUDForum. Page generated in 0.02150 seconds