Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Hexagon
Hexagon [message #467655] Fri, 03 February 2006 05:59 Go to next message
Mark Baldridge is currently offline Mark BaldridgeFriend
Messages: 1
Registered: July 2009
Junior Member
I have been attempting to render a (playing) field of 60x100 hexagons in a
JScrollPane that I have working in Swing into an SWT environment as a
learning project.
I have perused SWT Volume 1 by Steve Northover, and have so far only
succeeded by modifying the Shell bounded by a Region as in one of the
book's examples. Each shell, however, has its own identity on the task
bar. It seems that only a shell supports the Region clipping. The Swing
implementation subclasses a JButton and uses the contains() to clip the
hexagon.
In SWT, I extended Canvas as the typical extension point. I have not
figured out how to clip the drawing area to a hexagon so another hexagonal
image can fit up to its edge. The Canvas only understands a rectangular
bounds. I thought I might peruse the source to
org.eclipse.tptp.platform.report.drawutil.internal.ISymbol.H exagon for
additional ideas, but have not been able to access/find it.
So if a hexagon looks like:

1
6 2
5 3
4

and I add another hexagon adjacent to the 3-4 edge, the rectangle at the
3-4 edge overwrites the corner of the lower hexagon. I have not been able
to get the clipping to stop the rectangular bounds from obscuring the next
hexagon.
The following code demonstrates my consternation.

import org.eclipse.swt.SWT;
//import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
//import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;
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;
import org.eclipse.swt.custom.ScrolledComposite;
/*
* Created on Jan 18, 2006
*
* To change this generated comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

public class Hexagon extends Canvas implements Listener {

static final Color[][] colors = new Color[5][5];
static Region staticRegion = null;
Region region = null;

int[] hexagon = new int[12];
int colorNumber = 0;
boolean stateNormal = true;

public Hexagon(Composite parent, int style) {
super(parent, style);
addListener(SWT.Paint, this);
addListener(SWT.FocusIn, this);
addListener(SWT.FocusOut, this);
addListener(SWT.KeyDown, this);
addListener(SWT.MouseDown,this);
addListener(SWT.MouseUp,this);
// Rectangle bounds = this.getClientArea();
// System.out.println("ClientArea: "+bounds.toString());
// this.setSize(bounds.width,bounds.height);

}

static {

}

public void setColorNumber(int colorNumber) {
this.colorNumber = colorNumber;
}
public void setSize(int w, int h /*Shell hexShell*/) {
// int[] hexagon = new int[12];
// Rectangle bounds = hexShell.getBounds();
// int w = bounds.x;
// int h = bounds.y;
super.setSize(w,h);
if (staticRegion == null) {
staticRegion = new Region();
staticRegion.add(makeHexagon(w,h));
}
if (region == null) {
region = new Region();
this.hexagon = makeHexagon(w,h);
region.add(hexagon);
}
}
public int[] makeHexagon(int x, int y, int width, int height) {
// 5
// 4 6
// 3 1
// 2
int[] hexagon = new int[12];
// int x = rectangle.x;
// int y = rectangle.y;
// int width = rectangle.width;
// int height = rectangle.height;
int height1Quarter = (height-y)*1/4;
int height3Quarter = (height-y)*3/4;
int widthHalf = (width-x)/2;
hexagon[ 0] = width;
hexagon[ 1] = height3Quarter;
hexagon[ 2] = widthHalf;
hexagon[ 3] = height;
hexagon[ 4] = x;
hexagon[ 5] = height3Quarter;
hexagon[ 6] = x;
hexagon[ 7] = height1Quarter;
hexagon[ 8] = widthHalf;
hexagon[ 9] = y;
hexagon[10] = width;
hexagon[11] = height1Quarter;
return hexagon;
}
public static int[] makeHexagon(int w, int h) {
int[] hexagon = new int[12];
w = w-1;
hexagon[ 0] = w;
hexagon[ 1] = (h*3)/4;
hexagon[ 2] = w/2;
hexagon[ 3] = h;
hexagon[ 4] = 0;
hexagon[ 5] = (h*3)/4;
hexagon[ 6] = 0;
hexagon[ 7] = h/4;
hexagon[ 8] = w/2;
hexagon[ 9] = 0;
hexagon[10] = w;
hexagon[11] = h/4;
// hexagon[12] = w;
// hexagon[13] = (h*3)/4;
return hexagon;
}

public void handleEvent(Event event) {
GC gc;
switch (event.type) {
case SWT.Paint:
gc = event.gc;
// gc.getForeground()
gc.setClipping(region);
System.out.println("Paint isClipped: "+gc.isClipped()+" doit:
"+event.doit+", stateNormal: "+stateNormal);
System.out.println("colorNumber: "+colorNumber+", Color:
"+colors[colorNumber][((stateNormal) ? 0 : 1)]);
// this.setBackground(colors[colorNumber][((stateNormal) ? 0 : 1)]);
this.setForeground(colors[colorNumber][((stateNormal) ? 0 : 1)]);
// this.setForeground(colors[colorNumber][0]);
// gc.drawPolygon(hexagon);
// gc.fillPolygon(hexagon);
Rectangle size = this.getClientArea();
System.out.println("Size: "+size);
int x = size.x;
int y = size.y;
int width = size.width;
int height = size.height;
while ((width>0) && (height>0)) {
// System.out.println("x,y,w,h: "+x+","+y+","+width+","+height);
gc.drawPolygon(makeHexagon(x++,y,width--,height));
}
break;
case SWT.MouseDown:
System.out.println("MouseDown, xy: "+event.x+","+event.y+" doit:
"+event.doit);
System.out.println("Bounds: "+this.getBounds());
System.out.println("ClientArea: "+this.getClientArea());
if (region.contains(event.x,event.y)) {
stateNormal = false;
redraw();
} else {
event.doit = false;
}
break;
case SWT.MouseUp:
System.out.println("MouseUp, xy: "+event.x+","+event.y);
if (region.contains(event.x,event.y)) {
stateNormal = true;
redraw();
}
break;
case SWT.Dispose:
region.dispose();
break;
}
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Color black = display.getSystemColor(SWT.COLOR_BLACK);
Color gray = display.getSystemColor(SWT.COLOR_GRAY);
final Color green = display.getSystemColor(SWT.COLOR_GREEN);
Color greenDark = display.getSystemColor(SWT.COLOR_DARK_GREEN);
final Color cyan = display.getSystemColor(SWT.COLOR_CYAN);
Color cyanDark = display.getSystemColor(SWT.COLOR_DARK_CYAN);
final Color red = display.getSystemColor(SWT.COLOR_RED);
Color redDark = display.getSystemColor(SWT.COLOR_DARK_MAGENTA);
// Color[] colors = new Color[32];
colors[0][0] = gray;
colors[0][1] = gray;
colors[1][0] = green;
colors[1][1] = greenDark;
colors[2][0] = cyan;
colors[2][1] = cyanDark;
colors[3][0] = red;
colors[3][1] = redDark;
GC gc = null;
// Region region = new Region();
int WIDTH = 60;
int HEIGHT = 80;
int WIDTH_DELTA = 30;
int HEIGHT_DELTA = 60;
// region.add(makeHexagon(WIDTH,HEIGHT));
Shell shell = new Shell(display,SWT.SHELL_TRIM);
Composite composite1 = new Composite(shell,SWT.NONE);
// ScrolledComposite composite1 = new
ScrolledComposite(shell,SWT.H_SCROLL | SWT.V_SCROLL);
// ScrolledComposite composite1 = new ScrolledComposite(shell,SWT.NONE);
Hexagon hexagon = new Hexagon(composite1,SWT.NONE);
// hexagon.setBounds(100,100,160,180);
hexagon.setLocation(100,100);
hexagon.setSize(WIDTH,HEIGHT);
hexagon.setColorNumber(1);
// gc = new GC(hexagon);
// gc.setClipping(region);
Hexagon hexagon2 = new Hexagon(composite1,SWT.NONE);
hexagon2.setLocation(130,160);
// hexagon2.setLocation(200,200);
// hexagon2.setLocation(100,100);
// hexagon2.setBounds(130,160,190,240);
hexagon2.setSize(WIDTH,HEIGHT);

hexagon2.setColorNumber(2);

Hexagon hexagon3 = new Hexagon(composite1,SWT.NO_BACKGROUND);
hexagon3.setLocation(250,250);
hexagon3.setSize(100,100);
hexagon3.setColorNumber(3);

composite1.pack();

shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
// region.dispose();
for (int ndxRow=0; ndxRow<5; ndxRow++) {
for (int ndxCol=0; ndxCol<5; ndxCol++) {
if (colors[ndxRow][ndxCol] != null) colors[ndxRow][ndxCol].dispose();
}
}
// region.dispose();
display.dispose();
}

}
Re: Hexagon [message #467769 is a reply to message #467655] Mon, 06 February 2006 15:06 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Only Shells support a non-rectangular shape.

If you are drawing a playing field, why not just have one Canvas and draw
all of the hexagons on the same canvas? Why do you need to create a
separate widget (with on OS handle) for each hexagon? I would consider the
hexagons to be items - much in the same relationship as CTabItem and
CTabFolder. All the items are drawn on the same Canvas and you can impement
clipping within the Canvas. See GC.setClipping(Region).


"Mark Baldridge" <mbaldrid@us.ibm.com> wrote in message
news:c2e1a42ff86f403dc3a72e699788acf1$1@www.eclipse.org...
>I have been attempting to render a (playing) field of 60x100 hexagons in a
>JScrollPane that I have working in Swing into an SWT environment as a
>learning project.
> I have perused SWT Volume 1 by Steve Northover, and have so far only
> succeeded by modifying the Shell bounded by a Region as in one of the
> book's examples. Each shell, however, has its own identity on the task
> bar. It seems that only a shell supports the Region clipping. The Swing
> implementation subclasses a JButton and uses the contains() to clip the
> hexagon.
> In SWT, I extended Canvas as the typical extension point. I have not
> figured out how to clip the drawing area to a hexagon so another hexagonal
> image can fit up to its edge. The Canvas only understands a rectangular
> bounds. I thought I might peruse the source to
> org.eclipse.tptp.platform.report.drawutil.internal.ISymbol.H exagon for
> additional ideas, but have not been able to access/find it.
> So if a hexagon looks like:
>
> 1
> 6 2
> 5 3
> 4
>
> and I add another hexagon adjacent to the 3-4 edge, the rectangle at the
> 3-4 edge overwrites the corner of the lower hexagon. I have not been able
> to get the clipping to stop the rectangular bounds from obscuring the next
> hexagon.
> The following code demonstrates my consternation.
>
> import org.eclipse.swt.SWT;
> //import org.eclipse.swt.events.PaintEvent;
> import org.eclipse.swt.graphics.Color;
> import org.eclipse.swt.graphics.GC;
> //import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.graphics.Region;
> import org.eclipse.swt.widgets.Button;
> 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;
> import org.eclipse.swt.custom.ScrolledComposite;
> /*
> * Created on Jan 18, 2006
> *
> * To change this generated comment go to * Window>Preferences>Java>Code
> Generation>Code and Comments
> */
>
> public class Hexagon extends Canvas implements Listener {
>
> static final Color[][] colors = new Color[5][5];
> static Region staticRegion = null;
> Region region = null;
>
> int[] hexagon = new int[12];
> int colorNumber = 0;
> boolean stateNormal = true;
>
> public Hexagon(Composite parent, int style) {
> super(parent, style);
> addListener(SWT.Paint, this);
> addListener(SWT.FocusIn, this);
> addListener(SWT.FocusOut, this);
> addListener(SWT.KeyDown, this);
> addListener(SWT.MouseDown,this);
> addListener(SWT.MouseUp,this);
> // Rectangle bounds = this.getClientArea();
> // System.out.println("ClientArea: "+bounds.toString());
> // this.setSize(bounds.width,bounds.height);
> }
>
> static {
>
> }
>
> public void setColorNumber(int colorNumber) {
> this.colorNumber = colorNumber;
> }
> public void setSize(int w, int h /*Shell hexShell*/) {
> // int[] hexagon = new int[12];
> // Rectangle bounds = hexShell.getBounds();
> // int w = bounds.x;
> // int h = bounds.y;
> super.setSize(w,h);
> if (staticRegion == null) {
> staticRegion = new Region();
> staticRegion.add(makeHexagon(w,h));
> }
> if (region == null) {
> region = new Region();
> this.hexagon = makeHexagon(w,h);
> region.add(hexagon);
> }
> }
> public int[] makeHexagon(int x, int y, int width, int height) {
> // 5
> // 4 6
> // 3 1
> // 2
> int[] hexagon = new int[12];
> // int x = rectangle.x;
> // int y = rectangle.y;
> // int width = rectangle.width;
> // int height = rectangle.height;
> int height1Quarter = (height-y)*1/4;
> int height3Quarter = (height-y)*3/4;
> int widthHalf = (width-x)/2;
> hexagon[ 0] = width;
> hexagon[ 1] = height3Quarter;
> hexagon[ 2] = widthHalf;
> hexagon[ 3] = height;
> hexagon[ 4] = x;
> hexagon[ 5] = height3Quarter;
> hexagon[ 6] = x;
> hexagon[ 7] = height1Quarter;
> hexagon[ 8] = widthHalf;
> hexagon[ 9] = y;
> hexagon[10] = width;
> hexagon[11] = height1Quarter;
> return hexagon;
> }
> public static int[] makeHexagon(int w, int h) {
> int[] hexagon = new int[12];
> w = w-1;
> hexagon[ 0] = w;
> hexagon[ 1] = (h*3)/4;
> hexagon[ 2] = w/2;
> hexagon[ 3] = h;
> hexagon[ 4] = 0;
> hexagon[ 5] = (h*3)/4;
> hexagon[ 6] = 0;
> hexagon[ 7] = h/4;
> hexagon[ 8] = w/2;
> hexagon[ 9] = 0;
> hexagon[10] = w;
> hexagon[11] = h/4;
> // hexagon[12] = w;
> // hexagon[13] = (h*3)/4;
> return hexagon;
> }
>
> public void handleEvent(Event event) {
> GC gc;
> switch (event.type) {
> case SWT.Paint:
> gc = event.gc;
> // gc.getForeground()
> gc.setClipping(region);
> System.out.println("Paint isClipped: "+gc.isClipped()+" doit:
> "+event.doit+", stateNormal: "+stateNormal);
> System.out.println("colorNumber: "+colorNumber+", Color:
> "+colors[colorNumber][((stateNormal) ? 0 : 1)]);
> // this.setBackground(colors[colorNumber][((stateNormal) ? 0 : 1)]);
> this.setForeground(colors[colorNumber][((stateNormal) ? 0 : 1)]);
> // this.setForeground(colors[colorNumber][0]);
> // gc.drawPolygon(hexagon);
> // gc.fillPolygon(hexagon);
> Rectangle size = this.getClientArea();
> System.out.println("Size: "+size);
> int x = size.x;
> int y = size.y;
> int width = size.width;
> int height = size.height;
> while ((width>0) && (height>0)) {
> // System.out.println("x,y,w,h: "+x+","+y+","+width+","+height);
> gc.drawPolygon(makeHexagon(x++,y,width--,height));
> }
> break;
> case SWT.MouseDown:
> System.out.println("MouseDown, xy: "+event.x+","+event.y+" doit:
> "+event.doit);
> System.out.println("Bounds: "+this.getBounds());
> System.out.println("ClientArea: "+this.getClientArea());
> if (region.contains(event.x,event.y)) {
> stateNormal = false;
> redraw();
> } else {
> event.doit = false;
> }
> break;
> case SWT.MouseUp:
> System.out.println("MouseUp, xy: "+event.x+","+event.y);
> if (region.contains(event.x,event.y)) {
> stateNormal = true;
> redraw();
> }
> break;
> case SWT.Dispose:
> region.dispose();
> break;
> }
> }
> /**
> * @param args
> */
> public static void main(String[] args) {
> Display display = new Display();
> Color black = display.getSystemColor(SWT.COLOR_BLACK);
> Color gray = display.getSystemColor(SWT.COLOR_GRAY);
> final Color green = display.getSystemColor(SWT.COLOR_GREEN);
> Color greenDark = display.getSystemColor(SWT.COLOR_DARK_GREEN);
> final Color cyan = display.getSystemColor(SWT.COLOR_CYAN);
> Color cyanDark = display.getSystemColor(SWT.COLOR_DARK_CYAN);
> final Color red = display.getSystemColor(SWT.COLOR_RED);
> Color redDark = display.getSystemColor(SWT.COLOR_DARK_MAGENTA);
> // Color[] colors = new Color[32];
> colors[0][0] = gray;
> colors[0][1] = gray;
> colors[1][0] = green;
> colors[1][1] = greenDark;
> colors[2][0] = cyan;
> colors[2][1] = cyanDark;
> colors[3][0] = red;
> colors[3][1] = redDark;
> GC gc = null;
> // Region region = new Region();
> int WIDTH = 60;
> int HEIGHT = 80;
> int WIDTH_DELTA = 30;
> int HEIGHT_DELTA = 60;
> // region.add(makeHexagon(WIDTH,HEIGHT));
> Shell shell = new Shell(display,SWT.SHELL_TRIM);
> Composite composite1 = new Composite(shell,SWT.NONE);
> // ScrolledComposite composite1 = new ScrolledComposite(shell,SWT.H_SCROLL
> | SWT.V_SCROLL);
> // ScrolledComposite composite1 = new ScrolledComposite(shell,SWT.NONE);
> Hexagon hexagon = new Hexagon(composite1,SWT.NONE);
> // hexagon.setBounds(100,100,160,180);
> hexagon.setLocation(100,100);
> hexagon.setSize(WIDTH,HEIGHT);
> hexagon.setColorNumber(1);
> // gc = new GC(hexagon);
> // gc.setClipping(region);
> Hexagon hexagon2 = new Hexagon(composite1,SWT.NONE);
> hexagon2.setLocation(130,160);
> // hexagon2.setLocation(200,200);
> // hexagon2.setLocation(100,100);
> // hexagon2.setBounds(130,160,190,240);
> hexagon2.setSize(WIDTH,HEIGHT);
>
> hexagon2.setColorNumber(2);
>
> Hexagon hexagon3 = new Hexagon(composite1,SWT.NO_BACKGROUND);
> hexagon3.setLocation(250,250);
> hexagon3.setSize(100,100);
> hexagon3.setColorNumber(3);
>
> composite1.pack();
>
> shell.pack();
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> // region.dispose();
> for (int ndxRow=0; ndxRow<5; ndxRow++) {
> for (int ndxCol=0; ndxCol<5; ndxCol++) {
> if (colors[ndxRow][ndxCol] != null) colors[ndxRow][ndxCol].dispose(); }
> }
> // region.dispose();
> display.dispose();
> }
>
> }
>
>
>
Previous Topic:Problems with Table(Viewer) and Columns
Next Topic:CTabFolder and TabFolder
Goto Forum:
  


Current Time: Wed Apr 24 18:33:04 GMT 2024

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

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

Back to the top