Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Drawing a polygon doughnut.
Drawing a polygon doughnut. [message #453206] |
Thu, 31 March 2005 19:24 |
Eclipse User |
|
|
|
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 #453283 is a reply to message #453210] |
Mon, 04 April 2005 15:59 |
Eclipse User |
|
|
|
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 |
Eclipse User |
|
|
|
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 |
Rohit Colaco 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.
|
|
| | |
Goto Forum:
Current Time: Thu Sep 26 07:35:42 GMT 2024
Powered by FUDForum. Page generated in 0.05182 seconds
|