Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Drawing a polygon doughnut.
Drawing a polygon doughnut. [message #453206] Thu, 31 March 2005 19:24 Go to next message
Eclipse UserFriend
Originally posted by: waynetg.telkomsa.net

Hi all

I have two or more arrays of integers. I need to draw a "doughnut" with
one or more holes in the middle and not loose the detail behind the
doughnut that can be seen through the holes.

Does anyone have any idea as to how I can do this? I am totally at a loss...
Any pointers, help, reading material would be appreciated.
Re: Drawing a polygon doughnut. [message #453210 is a reply to message #453206] Thu, 31 March 2005 20:12 Go to previous messageGo to next message
Rohit Colaco is currently offline Rohit ColacoFriend
Messages: 36
Registered: July 2009
Member
You need to work with Regions defined in 'org.eclipse.swt.graphics.Region'

Create a new Region instance R1.
Add your outer polygon to R1 using add(int[]).
Subtract all inner polygons from R1 using subtract(int[]).
Hold the existing GC clipping state in another Region R2.
Clip on R1.
Fill and draw whatever you need within R1.
Restore the previous clipping on R2.
Oh and ... don't forget to dispose R1.

- Rohit

---

Subject: Drawing a polygon doughnut.
From: waynetg@xxxxxxxxxxxx (Tshwala)
Newsgroups: eclipse.platform.swt
Organization: EclipseCorner
Date: Mar 31 2005 14:24:13

Hi all

I have two or more arrays of integers. I need to draw a "doughnut" with
one or more holes in the middle and not loose the detail behind the
doughnut that can be seen through the holes.

Does anyone have any idea as to how I can do this? I am totally at a
loss...
Any pointers, help, reading material would be appreciated.
Re: Drawing a polygon doughnut. [message #453212 is a reply to message #453210] Thu, 31 March 2005 20:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: waynetg.telkomsa.net

Thanks, I'll try it in the morning. :)
Re: Drawing a polygon doughnut. [message #453283 is a reply to message #453210] Mon, 04 April 2005 15:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: waynetg.telkomsa.net

On Thu, 31 Mar 2005 20:12:13 +0000, Rohit Colaço wrote:

>
> You need to work with Regions defined in 'org.eclipse.swt.graphics.Region'
>
> Create a new Region instance R1.
> Add your outer polygon to R1 using add(int[]).
> Subtract all inner polygons from R1 using subtract(int[]).
> Hold the existing GC clipping state in another Region R2.
> Clip on R1.
> Fill and draw whatever you need within R1.
> Restore the previous clipping on R2.
> Oh and ... don't forget to dispose R1.
>
> - Rohit
>
Hi, this is my attempt at the above. I can't find the required functions
to do everything as outlined. Can anyone see what I'm doing wrong?


Region R1=new Region();
for(int i=0;i<partIndex.length;i++)
{
segStart=partIndex[i]*2;
if(i!=partIndex.length-1)
segEnd = (partIndex[i+1])*2;
else
segEnd = intAr.length;
partSegment[i] = new int[segEnd-segStart];
System.arraycopy(intAr,segStart,partSegment[i],0,partSegment [i].length);
if(i==0)
R1.add(partSegment[i]);
else
R1.subtract(partSegment[i]);
}
bufferGC.getClipping(R1);
R1.dispose();
Re: Drawing a polygon doughnut. [message #453299 is a reply to message #453210] Mon, 04 April 2005 20:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: waynetg.telkomsa.net

On Thu, 31 Mar 2005 20:12:13 +0000, Rohit Colaço wrote:

>
> You need to work with Regions defined in 'org.eclipse.swt.graphics.Region'
>
> Create a new Region instance R1.
> Add your outer polygon to R1 using add(int[]).
> Subtract all inner polygons from R1 using subtract(int[]).
> Hold the existing GC clipping state in another Region R2.
> Clip on R1.
> Fill and draw whatever you need within R1.
> Restore the previous clipping on R2.
> Oh and ... don't forget to dispose R1.
Hi heres a working(in a broken way) snippet showing what I'm trying to do.
Figured it would be more helpful. I'm missing a draw command or something
somewhere. Any pointers? I'm stumped...

import org.eclipse.swt.graphics.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;


public class testa {



private static int partSegment[][] = new int[][]
{
new int[] {10,10,70,10,250,20,400,100,600,200,400,250,60,50,10,10},
new int[] {100,60,350,80,370,110,320,150,100,60}
};

private static Display display;
private static Listener listener;
private static Shell shell;



public static void main(String[] argv) {

display=new Display();
shell=new Shell(display);
shell.setSize(650,670);
shell.setLocation(400,20);

redraw();
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
display.dispose ();
}

private static void redraw() {

listener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Paint: {
Image bufferImage = new Image(display, event.width,
event.height);

GC bufferGC = new GC(bufferImage);

bufferGC.setBackground(new Color(event.display, 0,255,255));
bufferGC.fillRectangle(300,100,50,50);

bufferGC.setLineWidth(2);
bufferGC.setLineCap(SWT.CAP_ROUND);


Region R1=new Region();


for(int i=0;i<2;i++)
{
if(i==0)
{
bufferGC.setBackground(new Color(event.display, 0,255,255));
R1.add(partSegment[i]);

}
else
{
bufferGC.setBackground(new Color(event.display, 255,0,0));
R1.subtract(partSegment[i]);

}
}
bufferGC.getClipping(R1);
R1.dispose();
event.gc.drawImage(bufferImage, 0, 0);
bufferImage.dispose();
break;
}
case SWT.BUTTON5:{
break;
}
};



}
};
shell.addListener(SWT.Paint,listener);
shell.redraw();
shell.update();
System.out.println("Done");

}

}
Re: Drawing a polygon doughnut. [message #453301 is a reply to message #453299] Mon, 04 April 2005 22:18 Go to previous messageGo to next message
Rohit Colaco is currently offline Rohit ColacoFriend
Messages: 36
Registered: July 2009
Member
Tshwala,

You didn't clearly follow the instructions I provided earlier.

I corrected your code.

Use this instead:

-----START-----

import org.eclipse.swt.graphics.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;


public class testa {

private static int partSegment[][] = new int[][]
{
new int[]
{10,10,70,15,250,20,400,100,600,200,400,250,60,50},
new int[] {100,60,350,80,370,110,320,150,100,60}
};

private static Display display;
private static Listener listener;
private static Shell shell;



public static void main(String[] argv) {

display=new Display();
shell=new Shell(display);
shell.setSize(650,670);
shell.setLocation(400,20);

redraw();
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
display.dispose ();
}

private static void redraw() {

listener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Paint: {
Image bufferImage = new Image(display, event.width,
event.height);

GC bufferGC = new GC(bufferImage);

bufferGC.setBackground(new Color(event.display,
0,255,255));
bufferGC.fillRectangle(300,100,50,50);

bufferGC.setLineWidth(2);
bufferGC.setLineCap(SWT.CAP_ROUND);


Region R1=new Region();
Region R2=new Region();
bufferGC.getClipping(R2);

for(int i=0;i<2;i++)
{
if(i==0)
{
bufferGC.setBackground(new
Color(event.display, 0,255,255));
R1.add(partSegment[i]);

}
else
{
bufferGC.setBackground(new
Color(event.display, 255,0,0));
R1.subtract(partSegment[i]);
}
}
bufferGC.setClipping(R1);

bufferGC.setBackground(event.display.getSystemColor(SWT.COLO R_RED));
bufferGC.fillRectangle(R1.getBounds());
R1.dispose();
bufferGC.setClipping(R2);
event.gc.drawImage(bufferImage, 0, 0);
bufferImage.dispose();
R2.dispose();
break;
}
case SWT.BUTTON5:{
break;
}
};



}
};
shell.addListener(SWT.Paint,listener);
shell.redraw();
shell.update();
System.out.println("Done");
}
}

------END------

Also, the following objects need to be disposed:

bufferGC.setBackground(new Color(event.display, 0,255,255)); // COLOR
CREATION
GC bufferGC = new GC(bufferImage); // GC CREATION

... or else you'll end up with no more SWT handles.
Re: Drawing a polygon doughnut. [message #453312 is a reply to message #453301] Tue, 05 April 2005 07:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: waynetg.telkomsa.net

Rohit,

Thank you for your time. Could you please tell me why R2 is needed. The
code seems to run fine without it.
Re: Drawing a polygon doughnut. [message #453450 is a reply to message #453312] Tue, 05 April 2005 21:18 Go to previous message
Rohit Colaco is currently offline Rohit ColacoFriend
Messages: 36
Registered: July 2009
Member
R2 is needed to preserve and restore any existing clipping state on your
GC. In the context of your app and image's GC, R2 may not be needed but
when you render multiple doughnuts on the same GC and eventually restore
the previous clip state, you would use R2.

In general, be disciplined about restoring a previous state irrespective
of the type of resource (colors, fonts, etc) being used. This is a
standard OS programming rule (especially Win32) and since SWT is
implemented as a thin JNI layer over OS API, you want to make sure you
follow that rule.

- Rohit
Previous Topic:Drag/Drop SWT-TreeItems to JTable in SWT.EMBEDDED Composite?
Next Topic:Applet wrapper for SWT Composite
Goto Forum:
  


Current Time: Tue Apr 16 22:25:41 GMT 2024

Powered by FUDForum. Page generated in 0.68772 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top