Emoji PlayingCards_javafx

The following source code will create emoji playing cards.

//https://stackoverflow.com/questions/11145681/how-to-convert-a-string-with-unicode-encoding-to-a-string-of-letters/54285760#54285760
//https://unicode.org/charts/PDF/U1F0A0.pdf

import javafx.scene.canvas.*;
import javafx.*;
import javafx.application.*;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

String unicodeToStr(String[] unicodeArray) {
  StringBuffer buffer = new StringBuffer();
  for (int i = 0; i < unicodeArray.length; i++) {
    int hexVal = Integer.parseInt(unicodeArray[i], 16);
    buffer.append(Character.toChars(hexVal));
  }
  return buffer.toString();
}

void setup() {
  size(1, 1, FX2D);
  Stage stage = new Stage();
  stage.setTitle("Emoji PlayingCards_Unicode To String Demo");
  Pane pane = new Pane();
  
  // **** Spades **** //
  String[] spadeArray = {"1F0A1", "1F0A2", "1F0A3", "1F0A4", "1F0A5", "1F0A6", "1F0A7", "1F0A8", "1F0A9", "1F0AA", "1F0AB", "1F0AD", "1F0AE"};
  String spadeStr = unicodeToStr(spadeArray);
  Text spades = new Text(30, 100, spadeStr );
  spades.setFont(new Font(100));
  spades.setFill(Color.BLACK);
  pane.getChildren().add(spades);
  
  // **** Hearts **** //
  String[] heartArray = {"1F0B1", "1F0B2", "1F0B3", "1F0B4", "1F0B5", "1F0B6", "1F0B7", "1F0B8", "1F0B9", "1F0BA", "1F0BB", "1F0BD", "1F0BE"};
  String heartStr = unicodeToStr(heartArray);
  Text hearts = new Text(30, 200, heartStr);
  hearts.setFont(new Font(100));
  hearts.setFill(Color.RED);
  pane.getChildren().add(hearts);

  // **** Diamonds **** //
  String[] diamondArray = {"1F0C1", "1F0C2", "1F0C3", "1F0C4", "1F0C5", "1F0C6", "1F0C7", "1F0C8", "1F0C9", "1F0CA", "1F0CB", "1F0CD", "1F0CE"};
  String diamondStr = unicodeToStr(diamondArray);
  Text diamonds = new Text(30, 300, diamondStr );
  diamonds.setFont(new Font(100));
  diamonds.setFill(Color.RED);
  pane.getChildren().add(diamonds);

  // **** Clubs **** //
  String[] clubArray = {"1F0D1", "1F0D2", "1F0D3", "1F0D4", "1F0D5", "1F0D6", "1F0D7", "1F0D8", "1F0D9", "1F0DA", "1F0DB", "1F0DD", "1F0DE"};
  String clubStr = unicodeToStr(clubArray);
  Text clubs = new Text(30, 400, clubStr );
  clubs.setFont(new Font(100));
  clubs.setFill(Color.BLACK);
  pane.getChildren().add(clubs);
  
  // **** Joker and Back of Card **** //
  String[] JokerAndBackArray = {"1F0A0", "1F0CF"};
  String JokerAndBackStr = unicodeToStr(JokerAndBackArray);
  Text jokerAndBack = new Text(30, 500, JokerAndBackStr );
  jokerAndBack.setFont(new Font(100));
  jokerAndBack.setFill(Color.BLACK);
  pane.getChildren().add(jokerAndBack);

  Scene scene = new Scene(pane, 980, 560, Color.WHITE);
  stage.setScene(scene);
  stage.show();
}

Output:

1 Like

Great!

Can you please add the Joker?

In simplified version…

And maybe a back side of a card?

Impressive.

Very cool code.

The code is not complete, I guess?

What is the difference between stage and scene conceptually?

See edit; joker and backside of card included.

I’m not sure what you mean by this? It should be a runnable demo.

What is the difference between stage and scene conceptually?

The following is a quotation from “Learn JavaFX 17” by Sharan and Spath:

Similar to a stage in the real world, a JavaFX stage is used to display a scene. A scene has visuals–such as text, shapes, images, controls, animations, and effects–with which the user may interact, as in the case with all GUI-based applications.

2 Likes