Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to generate a PDF document called Deck-of-Cards.pdf
- * and print in it a standard deck of 52 cards, following one after another.
- * Each card should be a rectangle holding its face and suit.
- * You are free to choose the size of each card, the spacing between the cards,
- * how many cards to put in each line, etc.
- * Note: you will need to use an external Java library for creating PDF documents.
- * Put your JAR files in a folder called "lib" (this is a standard approach
- * in Java projects) and reference them in the build path. */
- import java.io.FileOutputStream;
- import java.util.Locale;
- import java.util.Random;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfWriter;
- public class _09_GeneratePDFByExternalLibrary {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- /*
- * First we must import necessary External Java library for generating
- * PDF files. Here will using "iText®, a JAVA PDF library" from URL:
- * "http://sourceforge.net/". Next extract only "itextpdf-5.5.2.jar"
- * from whole download .zip file and put it in "lib" folder in our
- * project! The finally we must configure Build Path to location of that
- * .jar file!
- */
- Document document = new Document();
- try {
- PdfWriter.getInstance(document, new FileOutputStream(
- "Deck-of-Cards.pdf"));
- document.open();
- PdfPTable table = new PdfPTable(4);
- table.setWidthPercentage(100);
- table.getDefaultCell().setFixedHeight(180);
- /*
- * Now will using some True Type system font. Here will using that
- * font "times.ttf":
- */
- BaseFont baseFont = BaseFont.createFont("FreeSerifBold.ttf",
- BaseFont.IDENTITY_H, true);
- Font blackColorFont = new Font(baseFont, 65f, 0, BaseColor.BLACK);
- Font redColorFont = new Font(baseFont, 65f, 0, BaseColor.RED);
- String[] deckOfCards = createFullDeckOf52PlayCards();
- String[] randomizedCards = randomizeFullDeckOf52UniqueCards(deckOfCards);
- for (String card : randomizedCards) {
- boolean isRedCard = card.contains("" + '\u2665')
- || card.contains("" + '\u2666');
- if (isRedCard) {
- table.addCell(new Paragraph(card, redColorFont));
- } else {
- table.addCell(new Paragraph(card, blackColorFont));
- }
- }
- document.add(table);
- document.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private static String[] createFullDeckOf52PlayCards() {
- // TODO Auto-generated method stub
- String[] cardsFaces = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
- "J", "Q", "K", "A" };
- char[] suitCards = { '\u2663', '\u2666', '\u2665', '\u2660' };
- String[] fullDeck = new String[52];
- int index = 0;
- for (int face = 0; face < cardsFaces.length; face++) {
- for (int suit = 0; suit < suitCards.length; suit++) {
- fullDeck[index] = cardsFaces[face] + " " + suitCards[suit];
- index++;
- }
- }
- return fullDeck;
- }
- private static String[] randomizeFullDeckOf52UniqueCards(String[] deck) {
- // TODO Auto-generated method stub
- boolean[] drawnCards = new boolean[deck.length];
- Random randomGen = new Random();
- String[] randomCards = new String[deck.length];
- for (int i = 0; i < deck.length; i++) {
- int randomCard = randomGen.nextInt(deck.length);
- if (drawnCards[randomCard]) {
- i--;
- } else {
- drawnCards[randomCard] = true;
- randomCards[i] = deck[randomCard];
- }
- }
- return randomCards;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement