Advertisement
mellowdeep

DeckOfCards

Feb 5th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import com.itextpdf.text.BaseColor;
  3. import com.itextpdf.text.Chunk;
  4. import com.itextpdf.text.Document;
  5. import com.itextpdf.text.Element;
  6. import com.itextpdf.text.Font;
  7. import com.itextpdf.text.Image;
  8. import com.itextpdf.text.Paragraph;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.BaseFont;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. public class GenerateDeckOfCards {
  16.  
  17.     private static String FILE = "Deck.pdf";
  18.     public static final String IMG = "Joker.png";
  19.     public static void main(String[] args) {
  20.         try {
  21.        
  22.             Document doc = new Document();
  23.             PdfWriter.getInstance(doc, new FileOutputStream(FILE));                      
  24.             doc.open();
  25.             PdfPTable table = new PdfPTable(4);
  26.             table.setWidthPercentage(100);
  27.             table.getDefaultCell().setFixedHeight(160);
  28.          
  29.             BaseFont baseFont = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, true);
  30.             Font suitBlack = new Font(baseFont, 30f, 0, BaseColor.BLACK);
  31.             Font suitRed = new Font(baseFont, 30f, 0, BaseColor.RED);
  32.             Font cardBlack = new Font(baseFont, 20f, 0, BaseColor.BLACK);
  33.             Font cardRed = new Font(baseFont, 20f, 0, BaseColor.RED);
  34.            
  35.            
  36.            
  37.             String card;
  38.             String suit[] = new String[] {"\u2660", "\u2665","\u2663","\u2666"};
  39.            
  40.             for (int i = 2; i < 15; i++) {
  41.                 switch (i) {
  42.                 case 10: card = "10";
  43.                         break;
  44.                 case 11: card = " J";
  45.                         break;
  46.                 case 12: card = " Q";
  47.                         break;
  48.                 case 13: card = " K";
  49.                         break;
  50.                 case 14: card = " A";
  51.                         break;
  52.                 default: card = " " + Integer.toString(i);
  53.                     break;
  54.                 }
  55.                 for (int j = 0; j < suit.length; j++) {
  56.                     PdfPCell cell = new PdfPCell();
  57.                     cell.setPadding(3);
  58.                     cell.setBorder(Rectangle.NO_BORDER);
  59.    
  60.                     PdfPTable singleCellTable = new PdfPTable(1);
  61.                     PdfPCell singleCell = new PdfPCell();
  62.                    
  63.                     if (j == 0 || j == 2) {
  64.                        
  65.                         singleCell.addElement(setParameters(cardBlack, card,0));
  66.                         singleCell.addElement(setParameters(suitBlack, suit[j],3));
  67.                        
  68.                     } else {
  69.                         singleCell.addElement(setParameters(cardRed, card,0));
  70.                         singleCell.addElement(setParameters(suitRed, suit[j],3));
  71.                     }
  72.                    
  73.                     Image image2 = Image.getInstance(IMG);
  74.                     singleCell.setFixedHeight(140f);  
  75.                     singleCell.addElement(new Chunk(image2, 20, 20, true));
  76.                     singleCellTable.addCell(singleCell);
  77.                     cell.addElement(singleCellTable);
  78.                     table.addCell(cell);
  79.                 }
  80.             }
  81.             doc.add(table);
  82.             doc.close();    
  83.            
  84.         } catch (Exception e) {
  85.             e.printStackTrace();
  86.         }
  87.     }
  88.     private static Element setParameters(Font font, String card, int index) {
  89.         Paragraph pr = new Paragraph();
  90.         pr.add(card);
  91.         pr.setFont(font);
  92.         pr.setSpacingAfter(5);
  93.         pr.setIndentationLeft(index);
  94.         pr.setAlignment(Element.ALIGN_LEFT);
  95.        
  96.         return pr;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement