Advertisement
AngelGeorgiev

Java basics Problem 9

May 9th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import com.pdfjet.*;
  4.  
  5.  
  6. public class GeneratePDFDeck {
  7.     /*
  8.      * Write a program to generate a PDF document called Deck-of-Cards.pdf
  9.      * and print in it a standard deck of 52 cards, following one after another.
  10.      * Each card should be a rectangle holding its face and suit. A few examples are shown below:
  11.      * A ♠     * 2 ♥     * Q ♦     * K ♣     * J ♦     * 9 ♦     * 7 ♠
  12.      * You are free to choose the size of each card, the spacing between the cards,
  13.      * how many cards to put in each line, etc.
  14.      * Note: you will need to use an external Java library for creating PDF documents.
  15.      * Find some in Internet. Put your JAR files in a folder called "lib"
  16.      * (this is a standard approach in Java projects) and reference them in the build path.
  17.      * Hint: solve the problem step by step:
  18.      * 1. Generate the deck of cards and print it at the console (the console in Eclipse fully supports Unicode).
  19.      * 2. Find a Java library for generating PDF documents and play with it.
  20.      * Try to print some string in a PDF document.
  21.      * 3. Print the cards in PDF file (without the rectangular border).
  22.      * 4. Try to change the colors of the red cards.
  23.      * 5. Try to add the rectangular border around each card, e.g. by putting tables in the PDF.
  24.      */
  25.    
  26.     public GeneratePDFDeck() throws Exception {
  27.  
  28.  
  29.         PDF pdf = new PDF(
  30.                 new BufferedOutputStream(
  31.                         new FileOutputStream("Deck.pdf")));
  32.  
  33.         Page page = new Page(pdf, A4.LANDSCAPE);
  34.        
  35.         Font f1 = new Font(pdf, "KozMinProVI-Regular", CodePage.UNICODE);
  36.         f1.setSize(20f);
  37.  
  38.         String[] faces = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
  39.         String[] suites = {"\u2663", "\u2666", "\u2665", "\u2660"};
  40.        
  41.         for (int i = 0; i < faces.length; i++) {
  42.             for (int j = 0; j < suites.length; j++) {
  43.                
  44.                 Box cardback = new Box();
  45.                 cardback.setLocation((i + 1) * 65f - 55f, (j + 1) * 100f - 20f);
  46.                 cardback.setSize(40f, 55f);
  47.                 cardback.setColor(Color.black);
  48.                 cardback.drawOn(page);
  49.                
  50.                 TextLine face = new TextLine(f1,faces[i]);
  51.                 face.setLocation((i + 1) * 65f -50f, (j + 1) * 100f);              
  52.                 TextLine suite = new TextLine(f1,suites[j]);
  53.                 suite.setLocation((i + 1) * 65f - 30f, (j + 1) * 100f + 30f);
  54.                 if (j == 1 || j == 2) {
  55.                     face.setColor(Color.red);
  56.                     suite.setColor(Color.red);
  57.                 }
  58.                 face.drawOn(page);
  59.                 suite.drawOn(page);
  60.             }
  61.         }
  62.        
  63.         pdf.close();
  64.     }
  65.    
  66.     public static void main(String[] args) {
  67.         try {
  68.             new GeneratePDFDeck();
  69.         }
  70.         catch (Exception e) {
  71.             e.printStackTrace();
  72.         }
  73.        
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement