Sunday, October 26, 2014

(Game: display a checkerboard) Write a program that displays a checkerboard
in which each white and black cell is a  Rectangle with a fill color black or
white, as shown in figure.

Solution:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Checkboard extends Application {

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
int i = 0;
int j = 0;

for(i = 0; i < 8; i++) {
for(j = 0; j < 8; j++) {
Rectangle rec = new Rectangle(20 * j, 20 * i, 20, 20);
if(i + j % 2 == 1) {
rec.setFill(Color.BLACK);
} else {
rec.setFill(Color.WHITE);
}

pane.getChildren().add(rec);
}
}

Scene scene = new Scene(pane, 160, 160);
primaryStage.setTitle("Checkboard");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}
5

(Color and font) Write a program that displays five texts vertically, as shown in figure below. Set a random color and opacity for each text and set the font of each text to Times Roman, bold, italic, and 22 pixels.


Solution:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TextJava extends Application {

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22);
Text[] texts = new Text[5];

for(int i = 0; i < texts.length; i++) {
texts[i] = new Text(20 * i, 50, "JAVA");
texts[i].setFont(font);
texts[i].setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
texts[i].setRotate(90);
pane.getChildren().add(texts[i]);
}

Scene scene = new Scene(pane, 100, 100);
primaryStage.setTitle("JAVA with color");
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String[] args) {
launch(args);
}
}

0

Saturday, October 25, 2014

(Tic-tac-toe board) Write a program that displays a tic-tac-toe board, as shown
in figure below. A cell may be X, O, or empty. What to display at each cell is
randomly decided.


Solution:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class TicTacToe extends Application {

@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
Image img1 = new Image("image/o.gif");
Image img2 = new Image("image/x.gif");
ImageView imgView1 = new ImageView(img1);
ImageView imgView2 = new ImageView(img2);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
int random = (int)(Math.random() * 2);
if(random == 0) {
pane.add(imgView1, j, i);
} else {
pane.add(imgView2, j, i);
}
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle("Tic-Tac-Toe Board");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
Application.launch(args);
}
}

1

(Display images) Write a program that displays four images in a grid pane, as
shown in figure below:


Solution:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Flags extends Application {

@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
Image img1 = new Image("image/ca.gif");
Image img2 = new Image("image/china.gif");
Image img3 = new Image("image/us.gif");
Image img4 = new Image("image/uk.gif");
ImageView imgView1 = new ImageView(img1);
ImageView imgView2 = new ImageView(img2);
ImageView imgView3 = new ImageView(img3);
ImageView imgView4 = new ImageView(img4);
pane.add(imgView1, 1, 0);
pane.add(imgView2, 0, 1);
pane.add(imgView3, 1, 1);
pane.add(imgView4, 0, 0);
Scene scene = new Scene(pane);
primaryStage.setTitle("Nations Flag");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

0

Author