Advertisement
nadiahristova

Prob09_DeckOfCards

Jan 24th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.74 KB | None | 0 0
  1. //This program is created with the help of the IText JAVA PDF Library
  2. //The cards in the PDF are ordered either by default or randomly, a choice that can be made in the beginning of the execution of the program
  3. package de.vogella.itext;
  4.  
  5. import java.io.FileOutputStream;
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8. import java.util.Scanner;
  9.  
  10. import com.itextpdf.awt.geom.Rectangle;
  11. import com.itextpdf.text.BaseColor;
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Element;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.Font.FontFamily;
  17. import com.itextpdf.text.FontFactory;
  18. import com.itextpdf.text.Image;
  19. import com.itextpdf.text.PageSize;
  20. import com.itextpdf.text.Paragraph;
  21. import com.itextpdf.text.Phrase;
  22. import com.itextpdf.text.pdf.BaseFont;
  23. import com.itextpdf.text.pdf.PdfContentByte;
  24. import com.itextpdf.text.pdf.PdfGState;
  25. import com.itextpdf.text.pdf.PdfWriter;
  26.  
  27.  
  28. public class DeckOfCards {
  29.   private static String FileExpLoc = "/home/DeckOfCards.pdf";
  30.   //Sets the path and the name(DeckOfCards.pdf) of the newly created PDF
  31.   private static String FONT = "/home/CARDC___.TTF";
  32. //Keeps the path to the wanted font, in our case - CARDC___.TTF in /home folder
  33.  
  34.   public static void main(String[] args) {
  35.     try {
  36.         Document document = new Document(PageSize.A3.rotate());
  37.         PdfWriter.getInstance(document, new FileOutputStream(FileExpLoc));
  38.         PdfWriter writer
  39.         = PdfWriter.getInstance(document, new FileOutputStream(FileExpLoc));
  40.         document.open();      
  41.        
  42.         Scanner in = new Scanner(System.in);
  43.         System.out.print("Would you like a random ordination of the cards in the deck or a default one?[default]: ");
  44.         String answer = in.nextLine();
  45.         in.close();
  46.        
  47.         int leftX = 20;
  48.         int bottoY = 550;
  49.         String[][] cards = cardGen(answer);
  50.        
  51.         Random gen = new Random();
  52.         int imi = gen.nextInt(6);
  53.         String ImgHomeFolder = "/homeImg/" + Integer.toString(imi) + ".jpg";
  54.         //Gives the path and the name of the jpg-files needed for the background of the PDF. The pictures are located in the "Img" folder
  55.         Image img = Image.getInstance(ImgHomeFolder);
  56.         img.setAbsolutePosition(
  57.                 (PageSize.A3.getHeight() - img.getScaledWidth()) / 2,
  58.                 (PageSize.A3.getWidth() - img.getScaledHeight()) / 2);      
  59.         document.add(img);
  60.        
  61.         addTitlePage(document, answer);
  62.         addMetaData(document);
  63.        
  64.         PdfContentByte canv = writer.getDirectContent();
  65.          
  66.         canv.saveState();
  67.         PdfGState gs1 = new PdfGState();
  68.         gs1.setFillOpacity(0.4f);
  69.         canv.setGState(gs1);
  70.         Rectangle rect = new Rectangle(10, 192, 1177, 470);
  71.         canv.roundRectangle(
  72.                 (float)rect.x + 3f, (float)rect.y + 3f, (float)rect.getWidth() - 6,
  73.                 (float)rect.getHeight() - 6, 4);
  74.         canv.setLineWidth(3f);
  75.         canv.setColorStroke(BaseColor.LIGHT_GRAY);            
  76.         canv.fillStroke();
  77.         canv.restoreState();
  78.        
  79.         for (int i = 1; i <= cards.length; i++) {
  80.            
  81.             PdfContentByte canvas = writer.getDirectContent();
  82.          
  83.             canvas.saveState();
  84.             canvas.setColorFill(BaseColor.WHITE);
  85.             rect = new Rectangle(leftX, bottoY, 75, 100);
  86.             canvas.roundRectangle(
  87.                     (float)rect.x + 1.5f, (float)rect.y + 1.5f, (float)rect.getWidth() - 3,
  88.                     (float)rect.getHeight() - 3, 4);
  89.             canvas.setLineWidth(1.5f);
  90.             canvas.setColorStroke( BaseColor.LIGHT_GRAY);            
  91.             canvas.fillStroke();
  92.             canvas.restoreState();
  93.            
  94.             PdfContentByte signs = writer.getDirectContent();
  95.            
  96.             signs.saveState();
  97.             signs.beginText();  
  98.             int ini = i-1;
  99.             BaseFont bf = BaseFont.createFont(FONT,BaseFont.IDENTITY_H,true);      
  100.             signs.setFontAndSize(bf,14);
  101.            
  102.             if (cards[ini][1].equals("\u2666") || cards[ini][1].equals("\u2665")) {
  103.               signs.setColorFill(BaseColor.RED);
  104.             } else {
  105.             signs.setColorFill(BaseColor.BLACK);
  106.             }  
  107.  
  108.             signs.setTextMatrix(leftX+56, bottoY+8);
  109.             signs.showText(cards[ini][0]);
  110.             signs.setTextMatrix(leftX+8, bottoY+83);
  111.             signs.showText(cards[ini][0]);
  112.             signs.setTextMatrix(leftX+56, bottoY+22);    
  113.             String suit = cards[ini][1];
  114.             signs.showText(suit);
  115.             signs.setTextMatrix(leftX+8, bottoY+69);
  116.             signs.showText(suit);  
  117.             signs.setFontAndSize(bf,45);
  118.             signs.setTextMatrix(leftX+18, bottoY+34);
  119.             signs.showText(suit);    
  120.             signs.endText();
  121.             signs.restoreState();
  122.            
  123.             if (i % 4 == 0 ) {
  124.         leftX +=90;
  125.         bottoY +=345;
  126.         }else {      
  127.         bottoY -=115;
  128.         }
  129.         }  
  130.        
  131.         document.close();
  132.        
  133.     } catch (Exception e) {
  134.       e.printStackTrace();
  135.     }
  136.   }
  137.  
  138.   private static String[][] cardGen(String kind) {
  139.     String[][] deckOfCards= new String[52][2];
  140.     String suits = "\u2665\u2666\u2663\u2660";
  141.    
  142.     Random generator = new Random();
  143.    
  144.     if (kind.equals("random")) {
  145.        
  146.         ArrayList<String> deck = new ArrayList<String>();
  147.         for (int i = 0; i < 13; i++) {
  148.             deck.add(suits);
  149.         }
  150.        
  151.         ArrayList<Integer> posHolder = new ArrayList<Integer>();
  152.         for (int i = 0; i < 13; i++) {
  153.             posHolder.add(i);
  154.         }
  155.        
  156.         for (int i = 0; i < 52; i++) {
  157.             int facePos = generator.nextInt(posHolder.size());
  158.             int face = posHolder.get(facePos);
  159.            
  160.             int whichSuit = generator.nextInt((deck.get(face)).length());
  161.            
  162.             switch (face+1) {
  163.             case 1: deckOfCards[i][0] = "A"; break;
  164.             case 11: deckOfCards[i][0] = "J"; break;
  165.             case 12: deckOfCards[i][0] = "Q"; break;
  166.             case 13: deckOfCards[i][0] = "K"; break;
  167.             default: deckOfCards[i][0] = Integer.toString(face+1); break;
  168.             }
  169.             deckOfCards[i][1] = "" + (deck.get(face)).charAt(whichSuit);
  170.             deck.set(face,removeCharAt(deck.get(face),whichSuit));     
  171.             if (deck.get(posHolder.get(facePos)).length() == 0) {
  172.                 posHolder.remove(facePos); 
  173.             }          
  174.         }
  175.     } else {
  176.         for (int i = 0; i < 52; i++) {
  177.             int num = i/4+1;
  178.             int num1 = i%4;        
  179.             switch (num) {
  180.             case 1: deckOfCards[i][0] = "A"; break;
  181.             case 11: deckOfCards[i][0] = "J"; break;
  182.             case 12: deckOfCards[i][0] = "Q"; break;
  183.             case 13: deckOfCards[i][0] = "K"; break;
  184.             default: deckOfCards[i][0] = Integer.toString(num); break;
  185.             }
  186.             deckOfCards[i][1] = "" + suits.charAt(num1);
  187.         }
  188.     }      
  189.    
  190.     return deckOfCards;
  191. }
  192.  
  193.   private static void addMetaData(Document document) {
  194.     document.addTitle("A Deck Of Cards");
  195.     document.addSubject("My first PDF with the help of Java and iText");
  196.     document.addKeywords("Java, PDF, iText");  
  197.     document.addCreator("Nadia Hristova");
  198.   }
  199.  
  200.   private static void addTitlePage(Document document, String kind)
  201.           throws DocumentException {     
  202.         Font font = new Font(FontFamily.HELVETICA,35,Font.BOLDITALIC,BaseColor.WHITE);
  203.         String str = null;
  204.         if (kind.equals("random")) {
  205.             str = "A Randomly Shuffled Deck Of Cards:";        
  206.         } else {
  207.             str = "A Deck Of Cards Ordered By Default:";
  208.         }
  209.        
  210.         Paragraph title = new Paragraph(str, font);      
  211.         title.setAlignment(Element.ALIGN_CENTER);
  212.         for (int i = 0; i < 4; i++) {
  213.             document.add(new Phrase("\n"));
  214.         }    
  215.        
  216.         document.add(title);    
  217.   }
  218.  
  219.   private static String removeCharAt(String string, int whichSuit) {
  220.          return string.substring(0, whichSuit) + string.substring(whichSuit + 1);      
  221.   }  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement