[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform.swt] Question about PaintSurface.
|
- From: irum@xxxxxxxxxx (Irum Godil)
- Date: Thu, 12 Jun 2008 15:50:24 +0000 (UTC)
- Newsgroups: eclipse.platform.swt
- Organization: Eclipse
- User-agent: NewsPortal/0.36 (http://florian-amrhein.de/newsportal)
Hi,
I am working with the SWT PaintSurface class. I am extending it to provide
capability to set an image in the background and then draw some unfilled
rectangles on top of it. To this end, I have changed the code in the
"PaintSurface's" 'addPaintListener' API. I have attached the code at the
end of this email. \n
I am facing 2 problems as follows: \n
1) The rectangle that comes up generally only comes with 2 edges and not
all 4. \n
2) If I bring up another application in the foreground and switch back to
the Paint view, all the rectangles that I had drawn disappear. \n
In the code below, importedImage is the image I am importing for the
background image, and I have also copied code to set it. If someone can
please take a look at this code and suggest what is going wrong I will
really appreciate. \n
I could also send you my entire project if needed or snapshots to see what
to do here. \n
I will really appreciate your help on this. \n
Inside PaintSurface \n:
paintCanvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
if (rubberband.isEmpty()) {
// Nothing to merge, so we check if there is an image to show or we
just refresh
if (importedImage != null) {
showImportedImage(event.gc);
}
else {
event.gc.drawImage(image,
displayFDC.xOffset + event.x, displayFDC.yOffset + event.y,
event.width, event.height,
event.x, event.y, event.width, event.height);
}
} else {
/*
* Avoid flicker when merging overlayed objects by constructing the
image on
* a backbuffer first, then blitting (cp from memory to screen) it to
the screen.
*/
// Check that the backbuffer is large enough, if not, dispose it
if (paintImage != null) {
Rectangle rect = paintImage.getBounds();
if ((event.width + event.x > rect.width) ||
(event.height + event.y > rect.height)) {
paintFDC.gc.dispose();
paintImage.dispose();
paintImage = null;
}
}
if (paintImage == null) {
// get the paintSurface's display
Display display = getDisplay();
// rect - rectangle that can display data
Rectangle rect = display.getClientArea();
// construct image of needed size
paintImage = new Image(display,
Math.max(rect.width, event.width + event.x),
Math.max(rect.height, event.height + event.y));
paintFDC.gc = new GC(paintImage);
}
// Setup clipping and the FDC
Region clipRegion = new Region();
// set clipRegion to the current clipping region of event.gc
event.gc.getClipping(clipRegion);
// sets displayable area of paintFDC.gc to clip region of event.gc
paintFDC.gc.setClipping(clipRegion);
clipRegion.dispose();
// sets offsets and scales of paintFDC to that of displayFDC
paintFDC.xOffset = displayFDC.xOffset;
paintFDC.yOffset = displayFDC.yOffset;
paintFDC.xScale = displayFDC.xScale;
paintFDC.yScale = displayFDC.yScale;
// Merge the overlayed objects into the image, then blit (cp form
memory to screen)
// draw image that we had before to paintFDC.gc
paintFDC.gc.drawImage(image,
displayFDC.xOffset + event.x, displayFDC.yOffset + event.y,
event.width, event.height,
event.x, event.y, event.width, event.height);
// calls draw from Figure for each object in rubber band
rubberband.draw(paintFDC);
// draw the new object
event.gc.drawImage(paintImage,
event.x, event.y, event.width, event.height,
event.x, event.y, event.width, event.height);
}
}
});
\n
/* Show image function function */
private void showImportedImage(GC gc) {
// get rid of all figures up to now
this.clearRubberbandSelection();
// clientRect is the size of displayable area
Rectangle clientRect = paintCanvas.getClientArea(); /* Canvas' painting
area */
// sets the area of gc that can be changed (by drawing) to clientRect
gc.setClipping(clientRect);
// fills clientRect by backgrnd color (white)
gc.fillRectangle(clientRect);
// clear the gc of the imageFDC
imageFDC.gc.setClipping(clientRect);
imageFDC.gc.fillRectangle(clientRect);
// draw the image to gc
gc.drawImage(importedImage, 0, 0);
}