Showing posts with label circle. Show all posts
Showing posts with label circle. Show all posts

Friday, 25 July 2014


A small prototype for a switch-lights problem (four or more consecutive lights will be turned off) By Henry Chen https://henry416.wordpress.com

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class LightSwitch extends Application {
int[] array = new int[25];
Circle[] light = new Circle[25];
@Override
public void start(Stage stage) {
init(stage);
stage.show();
}
public void init(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 700, 500, Color.WHITE);
for (int i = 0; i < 25; i++) {
circleChange(i, root);
}
root.getChildren().addAll(light);
stage.setTitle("Lights Problem");
stage.setScene(scene);
}
public void circleChange(final int i, final Group root) {
light[i] = new Circle(i * 25 + 25, 100, 10);
light[i].setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
if (array[i] == 1)
array[i] = 0;
else
array[i] = 1;
int counter = 0;
int place = -1;
boolean x = false;
for (int a = 0; a < 25; a++) {
if (array[a] == 1) {
if (x == false) {
place = a;
x = true;
}
counter++;
} else {
if (counter >= 4 && x == true)
a = 26;
else {
x = false;
counter = 0;
place = -1;
}
}
}
if (counter >= 4 && x == true)
for (int b = 0; b < counter; b++)
array[b + place] = 0;
paint();
me.consume();
}
});
}
public void paint() {
for (int a = 0; a < 25; a++) {
if (array[a] == 0)
light[a].setFill(Color.BLACK);
else
light[a].setFill(Color.RED);
}
}
public static void main(String args[]) {
Application.launch(args);
}
}


Monday, 19 May 2014

Draw a semi ring - JavaFX



public class SemiDemo extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
root.getChildren().add(drawSemiRing(120, 120, 100, 50, Color.LIGHTGREEN, Color.DARKGREEN));
root.getChildren().add(drawSemiRing(350, 350, 200, 30, Color.LIGHTSKYBLUE, Color.DARKBLUE));
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
private Path drawSemiRing(double centerX, double centerY, double radius, double innerRadius, Color bgColor, Color strkColor) {
Path path = new Path();
path.setFill(bgColor);
path.setStroke(strkColor);
path.setFillRule(FillRule.EVEN_ODD);
MoveTo moveTo = new MoveTo();
moveTo.setX(centerX + innerRadius);
moveTo.setY(centerY);
ArcTo arcToInner = new ArcTo();
arcToInner.setX(centerX - innerRadius);
arcToInner.setY(centerY);
arcToInner.setRadiusX(innerRadius);
arcToInner.setRadiusY(innerRadius);
MoveTo moveTo2 = new MoveTo();
moveTo2.setX(centerX + innerRadius);
moveTo2.setY(centerY);
HLineTo hLineToRightLeg = new HLineTo();
hLineToRightLeg.setX(centerX + radius);
ArcTo arcTo = new ArcTo();
arcTo.setX(centerX - radius);
arcTo.setY(centerY);
arcTo.setRadiusX(radius);
arcTo.setRadiusY(radius);
HLineTo hLineToLeftLeg = new HLineTo();
hLineToLeftLeg.setX(centerX - innerRadius);
path.getElements().add(moveTo);
path.getElements().add(arcToInner);
path.getElements().add(moveTo2);
path.getElements().add(hLineToRightLeg);
path.getElements().add(arcTo);
path.getElements().add(hLineToLeftLeg);
return path;
}
public static void main(String[] args) {
launch(args);
}
}
view raw SemiDemo hosted with ❤ by GitHub