Programming exercises : 14.1(Display images); 14.2 (Tic, tac, toe board); 14.3 (
ID: 3855584 • Letter: P
Question
Programming exercises: 14.1(Display images); 14.2 (Tic, tac, toe board); 14.3 (Display three cards). JAVA
You can use the link below for 52 cards.
public Image randomImage(){
Random rand = new Random();
int random = rand.nextInt(12);
int randomType = rand.nextInt(3);
String[] value = {"2","3","4","5","6","7","8","9", "a","j","k","q","t"};
String[] typeOfCard = {"c","h","d","s"};
Image image = new Image("http://www.cs.duke.edu/csed/ap/cards/images/"+value[random]+typeOfCard[randomType]+".gif");
return image;
}
Explanation / Answer
Exercise_04.java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
/**
* Chapter 14 Exercise 4:
* <p>
* <p>
* (Color and font)
* Write a program that displays five texts vertically,
* as shown in Figure 14.44a. Set a random color and opacity for each text
* and set the font of each text to Times Roman, bold, italic, and 22 pixels.
* <p>
*/
public class Exercise_04 extends Application {
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
Label[] labels = new Label[5];
for (int i = 0; i < labels.length; i++) {
labels[i] = new Label("Java");
labels[i].setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22));
labels[i].setTextFill(getRandomColor());
labels[i].setRotate(90);
pane.add(labels[i], i, 0);
}
Scene scene = new Scene(pane, 250, 100);
primaryStage.setTitle("Java text");
primaryStage.setScene(scene);
primaryStage.show();
}
private Color getRandomColor() {
return new Color(Math.random(), Math.random(), Math.random(), Math.random());
}
public static void main(String[] args) {
Application.launch(args);
}
}
Exercise_02.java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
* Chapter 14 Exercise 2:
*
* (Tic-tac-toe board)
* Write a program that displays a tic-tac-toe board,
* as shown in Figure 14.43b. A cell may be X, O, or empty.
* What to display at each cell is randomly decided.
* The X and O are images in the files x.gif and o.gif.
*
*/
public class Exercise_02 extends Application {
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int random = (int)(Math.random() * 3);
if (random != 2) {
String image = (random > 0) ? "/image/x.gif" : "/image/o.gif";
pane.add(new ImageView(new Image(image)), j, i);
}
}
}
Scene scene = new Scene(pane, 150, 150);
primaryStage.setTitle("Tic Tac Toe");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Exercise_01.java
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;
/**
* Chapter 14 Exercise 1:
*
* (Display images)
* Write a program that displays four images in a grid pane,
* as shown in Figure 14.43a.
*
*/
public class Exercise_01 extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
GridPane gridPane = new GridPane();
int flagCount = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
gridPane.add(new ImageView(new Image("image/flag"+flagCount+".gif")),i,j);
flagCount++;
}
}
Scene scene = new Scene(gridPane, 1000, 500);
primaryStage.setTitle("Flags");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Exercise_03.java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
* Chapter 14 Exercise 3:
*
* (Display three cards)
* Write a program that displays three cards randomly selected from
* a deck of 52, as shown in Figure 14.43c. The card image files are
* named 1.png, 2.png, ..., 52.png and stored in the image/card directory.
* All three cards are distinct and selected randomly.
* Hint: You can select random cards by storing the numbers 1–52 to an array list,
* perform a random shuffle introduced in Section 11.12, and use the first three
* numbers in the array list as the file names for the image.
*/
public class Exercise_03 extends Application {
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
boolean[] usedCards = new boolean[52];
// choose 3 random distinct cards from the deck
int count = 0;
while (count < 3) {
int card = (int) (Math.random() * 52);
if (!usedCards[card]) {
usedCards[card] = true;
// card png files start from 1, so increment card by one
// for the correct file name.
pane.add(new ImageView(new Image("image/card/"+(++card)+".png")), count, 0);
count++;
}
}
Scene scene = new Scene(pane, 250, 150);
primaryStage.setTitle("3 Random Cards");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.