Ejemplo n.º 1
0
  private void checkBoxMouseClicked(MouseEvent event, int index) {
    CheckBox cb = checkBoxArray[index];
    int y = (int) cb.getLayoutY() + radius;
    if (cb.isSelected() && index < 5) {

      // Reader selected
      Ball b = new Ball(minX, maxX, minCsX, maxCsX, y, balColor1, "reader");
      ballArray[index] = b;
      Thread t = new Thread(new BallRunnable(b, readWrite));
      threadArray[index] = t;
      circleArray[index].setVisible(true);
      t.start();

    } else if (cb.isSelected() && index >= 5) {
      // Writer selected
      Ball b = new Ball(minX, maxX, minCsX, maxCsX, y, balColor2, "writer");
      ballArray[index] = b;
      Thread t = new Thread(new BallRunnable(b, readWrite));
      threadArray[index] = t;
      circleArray[index].setVisible(true);
      t.start();

    } else {

      threadArray[index].interrupt();
      threadArray[index] = null;
      ballArray[index] = null;
      circleArray[index].setVisible(false);
      circleArray[index].setCenterX(minX);
    }
  }
Ejemplo n.º 2
0
  @Override
  public void start(Stage primaryStage) {

    // Create the scene
    Group root = new Group();
    Scene scene = new Scene(root, maxX, maxY);

    // Check boxes
    for (int i = 0; i < checkBoxArray.length; i++) {
      String text;
      if (i < 5) {
        // Check box for reader
        text = "Reader" + (i + 1);
      } else {
        // Check box for writer
        text = "Writer" + (i - 4);
      }
      final int index = i;
      checkBoxArray[i] = new CheckBox(text);
      checkBoxArray[i].addEventHandler(
          MouseEvent.MOUSE_CLICKED,
          new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
              checkBoxMouseClicked(event, index);
            }
          });
      checkBoxArray[i].setLayoutX(radius);
      checkBoxArray[i].setLayoutY((i * 4 + 1) * radius);
      root.getChildren().add(checkBoxArray[i]);
    }

    // Indicate entire section
    Rectangle entireSection = new Rectangle(minX, 0, maxX - minX, maxY);
    entireSection.setFill(Color.LIGHTGREY);
    root.getChildren().add(entireSection);

    // Indicate critical section
    Rectangle criticalSection = new Rectangle(minCsX, 0, maxCsX - minCsX, maxY);
    criticalSection.setFill(Color.GREEN);
    root.getChildren().add(criticalSection);

    // Define circles for each ball
    for (int i = 0; i < circleArray.length; i++) {
      CheckBox cb = checkBoxArray[i];
      int y = (int) cb.getLayoutY() + radius;
      if (i < 5) {
        // Reader
        circleArray[i] = new Circle(minX, y, radius, balColor1);
      } else {
        // Writer
        circleArray[i] = new Circle(minX, y, radius, balColor2);
      }
      circleArray[i].setVisible(false);
      root.getChildren().add(circleArray[i]);
    }

    // Define title and assign the scene for main window
    primaryStage.setTitle("Moving Balls");
    primaryStage.setScene(scene);
    primaryStage.show();

    // Start thread to draw each 20 ms
    threadDraw = new Thread(new DrawRunnable());
    threadDraw.start();
  }