Created
May 12, 2011 14:31
-
-
Save hoop33/968605 to your computer and use it in GitHub Desktop.
SWT program that draws a circle and plots 9 regularly-spaced points on its circumference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.events.PaintEvent; | |
import org.eclipse.swt.events.PaintListener; | |
import org.eclipse.swt.layout.FillLayout; | |
import org.eclipse.swt.widgets.Canvas; | |
import org.eclipse.swt.widgets.Display; | |
import org.eclipse.swt.widgets.Shell; | |
public class Circle { | |
public void run() { | |
Display display = new Display(); | |
Shell shell = new Shell(display); | |
createContents(shell); | |
shell.open(); | |
while (!shell.isDisposed()) | |
if (!display.readAndDispatch()) | |
display.sleep(); | |
display.dispose(); | |
} | |
private void createContents(Shell shell) { | |
shell.setLayout(new FillLayout()); | |
final Canvas canvas = new Canvas(shell, SWT.NONE); | |
canvas.addPaintListener(new PaintListener() { | |
@Override | |
public void paintControl(PaintEvent e) { | |
Canvas canvas = (Canvas) e.widget; | |
int width = canvas.getBounds().width - 10; | |
int height = canvas.getBounds().height - 10; | |
int min = Math.min(width, height); | |
// Draw the circle | |
e.gc.setLineWidth(1); | |
e.gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE)); | |
e.gc.drawOval(5, 5, min, min); | |
// Divide into 9 pieces | |
int numPieces = 9; | |
e.gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED)); | |
e.gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED)); | |
double cx = (5 + min) / 2.0; | |
double cy = (5 + min) / 2.0; | |
double radius = min / 2.0; | |
double increment = 360.0 / (double) numPieces; | |
for (int i = 0; i < numPieces; i++) { | |
int x = (int) (cx + radius * (Math.cos(i * increment * Math.PI / 180.0))); | |
int y = (int) (cy + radius * (Math.sin(i * increment * Math.PI / 180.0))); | |
e.gc.fillOval(x, y, 5, 5); | |
} | |
} | |
}); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
new Circle().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment