Skip to content

Commit fc8b5ad

Browse files
committed
Rewrote Command Line
-Rewrote the command line making it easier to use and understand -Fixed a bug when reading a decklist file it will not ignore comment symbols like # and ! and will throw NullPointerException -Added card images to pdf
1 parent b7a8e48 commit fc8b5ad

File tree

4 files changed

+132
-28
lines changed

4 files changed

+132
-28
lines changed

src/yugiohproxymaker/CardCreator.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
*/
66
package yugiohproxymaker;
77

8+
import com.itextpdf.text.BadElementException;
89
import com.itextpdf.text.BaseColor;
910
import com.itextpdf.text.Document;
1011
import com.itextpdf.text.DocumentException;
1112
import com.itextpdf.text.Element;
1213
import com.itextpdf.text.Font;
14+
import com.itextpdf.text.Image;
1315
import com.itextpdf.text.Paragraph;
1416
import com.itextpdf.text.Phrase;
1517
import com.itextpdf.text.Rectangle;
@@ -22,12 +24,23 @@
2224
import com.itextpdf.text.pdf.PdfShading;
2325
import com.itextpdf.text.pdf.PdfShadingPattern;
2426
import com.itextpdf.text.pdf.PdfWriter;
27+
import java.awt.Graphics2D;
28+
import java.awt.Toolkit;
29+
import java.awt.image.BufferedImage;
30+
import java.awt.image.FilteredImageSource;
31+
import java.awt.image.ImageFilter;
32+
import java.awt.image.ImageProducer;
33+
import java.io.ByteArrayOutputStream;
34+
import java.io.FileOutputStream;
2535
import java.io.IOException;
36+
import java.net.MalformedURLException;
2637
import java.util.ArrayList;
2738
import java.util.Arrays;
2839
import java.util.logging.Level;
2940
import java.util.logging.Logger;
3041
import javafx.scene.paint.Color;
42+
import javax.imageio.ImageIO;
43+
import javax.swing.GrayFilter;
3144

3245
/**
3346
*
@@ -75,7 +88,7 @@ public CardCreator(Document document, PdfContentByte pcb){
7588

7689
}
7790

78-
public void renderCard(Card card, boolean blackAndWhite){
91+
public void renderCard(Card card, boolean useImage, boolean blackAndWhite){
7992
font = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL);
8093

8194
if(curCol == maxColsPerPage){
@@ -92,7 +105,12 @@ public void renderCard(Card card, boolean blackAndWhite){
92105
doc.newPage();
93106
}
94107

95-
drawCard(currentX, currentY, card, blackAndWhite);
108+
if(useImage){
109+
drawCardImage(currentX, currentY, card, blackAndWhite);
110+
}
111+
else{
112+
drawCard(currentX, currentY, card, blackAndWhite);
113+
}
96114
currentX += cardWidth + gap;
97115
curCol += 1;
98116
}
@@ -142,6 +160,44 @@ public void renderCard(Card card, boolean blackAndWhite){
142160
// drawCard(30,30, card10);//topleft
143161
// }
144162

163+
public static BufferedImage toBufferedImage(java.awt.Image img) {
164+
if (img instanceof BufferedImage) {
165+
return (BufferedImage) img;
166+
}
167+
168+
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
169+
170+
Graphics2D bGr = bimage.createGraphics();
171+
bGr.drawImage(img, 0, 0, null);
172+
bGr.dispose();
173+
174+
return bimage;
175+
}
176+
177+
private void drawCardImage(float x, float y, Card card, boolean blackAndWhite){
178+
y = doc.getPageSize().getHeight() - cardHeight - y;
179+
180+
BufferedImage bufferedImage = card.getImage();
181+
if(blackAndWhite){
182+
ImageFilter filter = new GrayFilter(true, 25);
183+
ImageProducer producer = new FilteredImageSource(bufferedImage.getSource(), filter);
184+
bufferedImage = toBufferedImage(Toolkit.getDefaultToolkit().createImage(producer));
185+
}
186+
187+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
188+
try {
189+
ImageIO.write(bufferedImage, "png", baos);
190+
Image iTextImage = Image.getInstance(baos.toByteArray());
191+
iTextImage.scaleAbsolute(cardWidth, cardHeight);
192+
iTextImage.setAbsolutePosition(x, y);
193+
doc.add(iTextImage);
194+
} catch (IOException | BadElementException ex) {
195+
Logger.getLogger(CardCreator.class.getName()).log(Level.SEVERE, null, ex);
196+
} catch (DocumentException ex) {
197+
Logger.getLogger(CardCreator.class.getName()).log(Level.SEVERE, null, ex);
198+
}
199+
}
200+
145201
private void drawCard(float x, float y, Card card, boolean blackAndWhite){
146202
y = doc.getPageSize().getHeight() - y;
147203

src/yugiohproxymaker/CardFetcher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
public class CardFetcher {
2626
private final String BASE_URL = "https://yugioh.fandom.com/wiki/";
2727

28+
//add different image urls
29+
2830
private final String SEARCH_URL = "https://yugioh.fandom.com/wiki/Special:Search?query=";
2931

3032
private Document currentDoc;
3133

3234
private HashMap<String, String> cardTableDetails;
3335

34-
public CardFetcher(){
35-
36-
}
36+
public CardFetcher(){}
3737

3838
public Card fetch(String passcode){
3939
passcode = passcodeCorrector(passcode);

src/yugiohproxymaker/Main.java

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,30 @@ public class Main {
3535

3636
private static final String DEFAULT_FILE_NAME = "Generated Deck";
3737

38-
public static void main(String[] args) {
39-
//DeckListLocation [DestinationLocation] [Colored MarginLeft MarginTop MarginRight MarginBottom]
40-
if(args.length == 1){//defaults margin settings [deckListLocation]
38+
public static void main(String[] args) {
39+
/*
40+
// <desk_list_location>
41+
// <desk_list_location> <destination>
42+
// <desk_list_location> <use_image> <colored>
43+
// <desk_list_location> <destination> <use_image> <colored> [--left=<px>] [--top=<px>] [--right=<px>] [--bottom=<px>]
44+
*/
45+
46+
if(args.length == 1){//desk_list_location
4147
File deckListFile = new File(args[0]);
4248
if(!deckListFile.exists()){
4349
System.out.println("File doesn't exist");
4450
System.exit(0);
4551
}
4652
File dest = getDefaultFileName(DEFAULT_FILE_NAME, 0);
53+
System.out.println(dest);
4754
boolean overwrite = overwrite(dest);
4855
if(overwrite){
4956
System.out.println("Generating...");
50-
generate(deckListFile, dest, false, marginLeft, marginTop, marginRight, marginBottom);
51-
System.out.println("Finished, File Location : "+dest.getPath());
57+
generate(deckListFile, dest, false, false, marginLeft, marginTop, marginRight, marginBottom);
58+
System.out.println("Finished, File Location : "+dest.getAbsolutePath());
5259
}
5360
}
54-
else if(args.length == 2){//default margin settings [deskListLocation, destinationLocation]
61+
else if(args.length == 2){//desk_list_location destination
5562
File deckListFile = new File(args[0]);
5663
if(!deckListFile.exists()){
5764
System.out.println("File doesn't exist");
@@ -61,32 +68,70 @@ else if(args.length == 2){//default margin settings [deskListLocation, destinati
6168
boolean overwrite = overwrite(destinationFile);
6269
if(overwrite){
6370
System.out.println("Generating...");
64-
generate(deckListFile, destinationFile, false, marginLeft, marginTop, marginRight, marginBottom);
65-
System.out.println("Finished, File Location : "+destinationFile.getPath());
71+
generate(deckListFile, destinationFile, false, false, marginLeft, marginTop, marginRight, marginBottom);
72+
System.out.println("Finished, File Location : "+destinationFile.getAbsolutePath());
6673
}
6774
}
68-
else if (args.length == 7){//[deskListLocation, destinationLocation, colored, left, top, right, bottom]
75+
else if(args.length == 3){//desk_list_location <use_image> <colored>
6976
File deckListFile = new File(args[0]);
7077
if(!deckListFile.exists()){
7178
System.out.println("File doesn't exist");
7279
System.exit(0);
7380
}
74-
File destinationFile = new File(touchUpFileName(args[1]));
81+
boolean useImage = getBoolean(args[1], false);
7582
boolean colored = getBoolean(args[2], false);
76-
float left = getFloat(args[3], marginLeft);
77-
float top = getFloat(args[4], marginTop);
78-
float right = getFloat(args[5], marginRight);
79-
float bottom = getFloat(args[6], marginBottom);
8083

84+
File dest = getDefaultFileName(DEFAULT_FILE_NAME, 0);
85+
boolean overwrite = overwrite(dest);
86+
if(overwrite){
87+
System.out.println("Generating...");
88+
generate(deckListFile, dest, useImage, colored, marginLeft, marginTop, marginRight, marginBottom);
89+
System.out.println("Finished, File Location : "+dest.getAbsolutePath());
90+
}
91+
}
92+
else if(args.length >= 4 && args.length <= 8){//<desk_list_location> <destination> <use_image> <colored> [--left=<px>] [--top=<px>] [--right=<px>] [--bottom=<px>]
93+
File deckListFile = new File(args[0]);
94+
if(!deckListFile.exists()){
95+
System.out.println("File doesn't exist");
96+
System.exit(0);
97+
}
98+
99+
boolean useImage = getBoolean(args[2], false);
100+
boolean colored = getBoolean(args[3], false);
101+
102+
float left = marginLeft;
103+
float top = marginTop;
104+
float right = marginRight;
105+
float bottom = marginBottom;
106+
107+
for(int i=4;i<args.length;i++){
108+
String param = args[i];
109+
if(param.startsWith("--")){
110+
if(param.startsWith("left", 2)){
111+
left = getFloat(param.substring(param.indexOf("=") + 1), marginLeft);
112+
}
113+
else if(param.startsWith("top", 2)){
114+
top = getFloat(param.substring(param.indexOf("=") + 1), marginTop);
115+
}
116+
else if(param.startsWith("right", 2)){
117+
right = getFloat(param.substring(param.indexOf("=") + 1), marginRight);
118+
}
119+
else if(param.startsWith("bottom", 2)){
120+
bottom = getFloat(param.substring(param.indexOf("=") + 1), marginBottom);
121+
}
122+
}
123+
}
124+
125+
File destinationFile = new File(touchUpFileName(args[1]));
81126
boolean overwrite = overwrite(destinationFile);
82127
if(overwrite){
83128
System.out.println("Generating...");
84-
generate(deckListFile, destinationFile, colored, left, top, right, bottom);
85-
System.out.println("Finished, File Location : "+destinationFile.getPath());
129+
generate(deckListFile, destinationFile, useImage, colored, left, top, right, bottom);
130+
System.out.println("Finished, File Location : "+destinationFile.getAbsolutePath());
86131
}
87132
}
88133
else{
89-
System.out.println("Nothing Generated, Requires DeckListLocation");
134+
System.out.println("Invalid Command");
90135
}
91136
}
92137

@@ -150,14 +195,15 @@ private static boolean overwrite(File file){
150195
*
151196
* @param deckList
152197
* @param destination
198+
* @param useImage
153199
* @param colored
154200
* @param left
155201
* @param top
156202
* @param right
157203
* @param bottom
158204
* @return true if the deck successfully generated, false if an error occured
159205
*/
160-
private static boolean generate(File deckList, File destination, boolean colored, float left, float top, float right, float bottom){
206+
private static boolean generate(File deckList, File destination, boolean useImage, boolean colored, float left, float top, float right, float bottom){
161207
if(!deckList.exists()){
162208
return false;
163209
}
@@ -173,7 +219,7 @@ private static boolean generate(File deckList, File destination, boolean colored
173219

174220
CardCreator creator = new CardCreator(document, cb);
175221
for(Card card : cards){
176-
creator.renderCard(card, !colored);
222+
creator.renderCard(card, useImage, !colored);
177223
}
178224
document.close();
179225
return true;
@@ -210,9 +256,11 @@ private static ArrayList<Card> getCards(ArrayList<String> cardIds){
210256
CardFetcher cardFetch = new CardFetcher();
211257
ArrayList<Card> cards = new ArrayList<>();
212258
for(String id : cardIds){
213-
Card card = cardFetch.fetch(id);
214-
if(card != null){
215-
cards.add(card);
259+
if(!id.startsWith("!") && !id.startsWith(("#"))){
260+
Card card = cardFetch.fetch(id);
261+
if(card != null){
262+
cards.add(card);
263+
}
216264
}
217265
}
218266
return cards;

src/yugiohproxymaker/YugiohProxyMaker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void testGenerateDeck(){
8282

8383
CardCreator creator = new CardCreator(document, cb);
8484
for(Card card : cards){
85-
creator.renderCard(card, true);
85+
creator.renderCard(card, false, true);
8686
}
8787
document.close();
8888
} catch (FileNotFoundException | DocumentException ex) {}

0 commit comments

Comments
 (0)