Skip to content

Commit f3d6527

Browse files
committed
Add renderScore
1 parent e8f394a commit f3d6527

File tree

4 files changed

+106
-20
lines changed

4 files changed

+106
-20
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ dependencies {
5050

5151

5252
implementation 'com.github.APN-Pucky:GitJarUpdate:0.0.7'
53-
implementation 'com.github.APN-Pucky:TULib:0.2.30'
53+
implementation 'com.github.APN-Pucky:TULib:0.3.6'
5454
//compile project(":TULib")
5555

5656
}

src/main/java/de/neuwirthinformatik/Alexander/TU/Render/Render.java

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.awt.Graphics;
99
import java.awt.Graphics2D;
1010
import java.awt.Image;
11+
import java.awt.Rectangle;
1112
import java.awt.geom.AffineTransform;
1213
import java.awt.geom.Line2D;
1314
import java.awt.image.AffineTransformOp;
@@ -38,6 +39,7 @@
3839
import de.neuwirthinformatik.Alexander.TU.Basic.Deck;
3940
import de.neuwirthinformatik.Alexander.TU.Basic.GlobalData;
4041
import de.neuwirthinformatik.Alexander.TU.Basic.SkillSpec;
42+
import de.neuwirthinformatik.Alexander.TU.TUO.TUO.Result;
4143
import de.neuwirthinformatik.Alexander.TU.util.StringUtil;
4244
import de.neuwirthinformatik.Alexander.TU.util.Wget;
4345

@@ -52,6 +54,57 @@ public Render() throws FontFormatException, IOException {
5254
optimus = Font.createFont(Font.TRUETYPE_FONT, TU.class.getResourceAsStream("/Optimus.otf"));
5355
arial = Font.createFont(Font.TRUETYPE_FONT, TU.class.getResourceAsStream("/arialbold.ttf"));
5456
}
57+
58+
public BufferedImage renderScore(Result r, CardInstance y_com,CardInstance y_dom,CardInstance y_ass, CardInstance e_com,CardInstance e_dom,CardInstance e_ass,File y_s,File e_s) {
59+
60+
// int offset_y = 10;
61+
int width = CARD_WIDTH * 2;
62+
int height = CARD_HEIGHT * 2;
63+
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
64+
Graphics g = img.getGraphics();
65+
g.setColor(Color.white);
66+
g.fillRect(0, 0, width, height);
67+
68+
g.drawImage(render(y_ass, new String[] { "", "", "" }, y_s, y_ass.getCardType()), 0, 0, null);
69+
g.drawImage(render(e_ass, new String[] { "", "", "" }, e_s, e_ass.getCardType()), 0, CARD_HEIGHT, null);
70+
71+
if(r.WINS > r.LOSSES) {
72+
g.setColor(Color.GREEN);
73+
}
74+
else if (r.LOSSES > r.WINS){
75+
g.setColor(Color.RED);
76+
}
77+
else {
78+
g.setColor(Color.BLACK);
79+
}
80+
drawCenteredString(g,""+(int) Math.round(r.WINS) + "%", new Rectangle(CARD_WIDTH,0,CARD_WIDTH,CARD_HEIGHT),new Font("Serif", Font.BOLD, 50));
81+
82+
if(r.WINS > r.LOSSES) {
83+
g.setColor(Color.RED);
84+
}
85+
else if (r.LOSSES > r.WINS){
86+
g.setColor(Color.GREEN);
87+
}
88+
else {
89+
g.setColor(Color.BLACK);
90+
}
91+
drawCenteredString(g,""+(int) Math.round(r.LOSSES) + "%",new Rectangle(CARD_WIDTH,CARD_HEIGHT,CARD_WIDTH,CARD_HEIGHT),new Font("Serif", Font.BOLD, 50));
92+
93+
return img;
94+
95+
}
96+
public void drawCenteredString(Graphics g, String text, Rectangle rect, Font font) {
97+
// Get the FontMetrics
98+
FontMetrics metrics = g.getFontMetrics(font);
99+
// Determine the X coordinate for the text
100+
int x = rect.x + (rect.width - metrics.stringWidth(text)) / 2;
101+
// Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
102+
int y = rect.y + ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent();
103+
// Set the font
104+
g.setFont(font);
105+
// Draw the String
106+
g.drawString(text, x, y);
107+
}
55108

56109
public BufferedImage renderDeck(CardInstance com, CardInstance dom, CardInstance ass, File s) {
57110
// int offset_y = 10;
@@ -68,8 +121,7 @@ public BufferedImage renderDeck(CardInstance com, CardInstance dom, CardInstance
68121
BufferedImage cimg = render(ass, new String[] { "", "", "" }, s, ass.getCardType());
69122
for (int iid = 0; iid < 12; iid++) {
70123
if (i > 1)
71-
g.drawImage(cimg,
72-
CARD_WIDTH * (1 + (i - 2) % 5), ((i - 2) / 5) * CARD_HEIGHT, null);
124+
g.drawImage(cimg, CARD_WIDTH * (1 + (i - 2) % 5), ((i - 2) / 5) * CARD_HEIGHT, null);
73125
i++;
74126
}
75127

@@ -98,7 +150,10 @@ public BufferedImage renderDeck(CardInstance[] deck, File s) {
98150
return img;
99151
}
100152

153+
@Deprecated
101154
public BufferedImage renderDeck(CardInstance[] deck, String s) {
155+
return renderDeck(deck,new File(s));
156+
/*
102157
// int offset_y = 10;
103158
int width = CARD_WIDTH * 6;
104159
int height = CARD_HEIGHT * 2;
@@ -118,6 +173,7 @@ public BufferedImage renderDeck(CardInstance[] deck, String s) {
118173
}
119174
120175
return img;
176+
*/
121177
}
122178

123179
public BufferedImage renderDeck(Deck d) {
@@ -257,28 +313,25 @@ public BufferedImage render(CardInstance c) {
257313
}
258314

259315
public BufferedImage render(CardInstance c, String[] txts, String file, CardType type) {
316+
if(file == null || "".equals(file)) {
317+
return render(c, txts, (File)null, type);
318+
}
319+
else {
320+
return render(c, txts,new File(file) , type);
321+
}
322+
}
323+
324+
public BufferedImage render(CardInstance c, String[] txts, File file, CardType type) {
260325
Image bi = null;
261326
try {
262-
263-
if (file == null || "".equals(file)) {
327+
if (file == null) {
264328
bi = getCardImage(c.getCard().getAssetBundle(), c.getCard().getPicture());
265329
} else {
266-
bi = ImageIO.read(new File(file));
330+
bi = ImageIO.read(file);
267331
bi = bi.getScaledInstance(150, 125, Image.SCALE_SMOOTH);
268332
}
269333
} catch (Exception e) {
270334
e.printStackTrace();
271-
} // TODO FINALLY RM FOLDER
272-
return render(c, txts, bi, type);
273-
}
274-
275-
public BufferedImage render(CardInstance c, String[] txts, File file, CardType type) {
276-
Image bi = null;
277-
try {
278-
bi = ImageIO.read(file);
279-
bi = bi.getScaledInstance(150, 125, Image.SCALE_SMOOTH);
280-
} catch (Exception e) {
281-
e.printStackTrace();
282335
}
283336
return render(c, txts, bi, type);
284337
}

src/main/java/de/neuwirthinformatik/Alexander/TU/TUR/Test/RandomDeckTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import java.awt.FontFormatException;
44
import java.awt.image.BufferedImage;
5+
import java.io.File;
56
import java.io.IOException;
67

8+
79
import de.neuwirthinformatik.Alexander.TU.Basic.Card;
810
import de.neuwirthinformatik.Alexander.TU.Basic.Card.CardInstance;
911
import de.neuwirthinformatik.Alexander.TU.Basic.Card.CardType;
@@ -17,10 +19,10 @@ public static void main(String[] args) throws FontFormatException, IOException {
1719
String name = "DR_F3LL";
1820
int seed = name.hashCode();
1921
CardInstance com = Gen.genCardInstance(name,seed,true);
20-
CardInstance dom = Gen.genCardInstance(name,seed,(c) -> c.getCardType()==CardType.DOMINION);
21-
CardInstance ass = Gen.genCardInstance(name,seed,(c) -> c.getCardType()==CardType.ASSAULT);
22+
CardInstance dom = Gen.genCardInstance(name,seed,CardType.DOMINION);
23+
CardInstance ass = Gen.genCardInstance(name,seed,CardType.ASSAULT);
2224

23-
BufferedImage img = new Render().renderDeck(new CardInstance[] {com,dom,ass,ass,ass,ass,ass,ass,ass,ass,ass,ass},"in.png");
25+
BufferedImage img = new Render().renderDeck(com,dom,ass,new File("in.png"));
2426
if(img == null)
2527
System.out.println("null img");
2628
else
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.neuwirthinformatik.Alexander.TU.TUR.Test;
2+
3+
import java.awt.FontFormatException;
4+
import java.awt.image.BufferedImage;
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import de.neuwirthinformatik.Alexander.TU.Basic.Card.CardInstance;
9+
import de.neuwirthinformatik.Alexander.TU.Basic.Card.CardType;
10+
import de.neuwirthinformatik.Alexander.TU.Basic.Gen;
11+
import de.neuwirthinformatik.Alexander.TU.Basic.GlobalData;
12+
import de.neuwirthinformatik.Alexander.TU.Render.Render;
13+
14+
import de.neuwirthinformatik.Alexander.TU.TUO.TUO.Result;
15+
public class ScoreImageTest extends CardImageTest {
16+
public static void main(String[] args) throws FontFormatException, IOException {
17+
18+
GlobalData.init();
19+
String name = "DR_F3LL";
20+
int seed = name.hashCode();
21+
CardInstance com = Gen.genCardInstance(name,seed,true);
22+
CardInstance dom = Gen.genCardInstance(name,seed,CardType.DOMINION);
23+
CardInstance ass = Gen.genCardInstance(name,seed,CardType.ASSAULT);
24+
25+
BufferedImage img = new Render().renderScore(new Result(100,0,0,0),com,dom,ass,com,dom,ass,new File("in.png"),new File("in.png"));
26+
if(img == null)
27+
System.out.println("null img");
28+
else
29+
showImage(img);
30+
}
31+
}

0 commit comments

Comments
 (0)