Merge pull request #5123 from spjspj/master

Add in new bar chart distribution for the 'M' mana analyse button.
This commit is contained in:
spjspj 2018-07-08 20:04:05 +10:00 committed by GitHub
commit d10e5b720b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 162 additions and 3 deletions

View file

@ -1337,6 +1337,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg
public void analyseDeck() {
HashMap<String, Integer> qtys = new HashMap<>();
HashMap<String, Integer> pips = new HashMap<>();
HashMap<String, Integer> pips_at_cmcs = new HashMap<>();
HashMap<String, Integer> sourcePips = new HashMap<>();
HashMap<String, Integer> manaCounts = new HashMap<>();
pips.put("#w}", 0);
@ -1397,10 +1398,31 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg
mc = mc.replaceAll("\\{([WUBRG]).([WUBRG])\\}", "{$1}{$2}");
mc = mc.replaceAll("\\{", "#");
mc = mc.toLowerCase(Locale.ENGLISH);
int cmc = card.getConvertedManaCost();
// Do colorless mana pips
Pattern regex = Pattern.compile("#([0-9]+)}");
Matcher regexMatcher = regex.matcher(mc);
while (regexMatcher.find()) {
String val = regexMatcher.group(1);
int colorless_val = Integer.parseInt(val);
int pip_value = 0;
if (pips_at_cmcs.get(cmc + "##c}") != null) {
pip_value = pips.get("#c}");
}
pips_at_cmcs.put(cmc + "##c}", colorless_val + pip_value);
pips.put("#c}", colorless_val + pip_value);
}
for (String pip : pips.keySet()) {
int value = pips.get(pip);
while (mc.toLowerCase(Locale.ENGLISH).contains(pip)) {
pips.put(pip, ++value);
int pip_value = 0;
if (pips_at_cmcs.get(cmc + "#" + pip) != null) {
pip_value = pips_at_cmcs.get(cmc + "#" + pip);
}
pips_at_cmcs.put(cmc + "#" + pip, ++pip_value);
mc = mc.replaceFirst(pip, "");
}
}
@ -1448,9 +1470,17 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg
panel4.add(new JLabel("Mana sources found:"));
panel4.add(chart3);
JPanel panel5 = new JPanel();
panel5.setLayout(new BoxLayout(panel5, BoxLayout.Y_AXIS));
ManaBarChart chart4 = new ManaBarChart(pips_at_cmcs);
chart4.setMinimumSize(new Dimension(200, 200));
panel5.add(new JLabel("Mana distribution:"));
panel5.add(chart4);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
panel.add(panel5);
JFrame frame = new JFrame("JOptionPane showMessageDialog component example");
JOptionPane.showMessageDialog(frame, panel, "This is the distribution of colors found", JOptionPane.INFORMATION_MESSAGE);

View file

@ -0,0 +1,129 @@
package mage.client.cards;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JComponent;
public class ManaBarChart extends JComponent {
HashMap<String, Integer> pips_at_cmcs = new HashMap<String, Integer>();
ManaBarChart() {
}
ManaBarChart(HashMap<String, Integer> pips_at_cmcs) {
this.pips_at_cmcs = pips_at_cmcs;
}
@Override
public Dimension getPreferredSize() {
Dimension preferred = super.getPreferredSize();
Dimension minimum = getMinimumSize();
Dimension maximum = getMaximumSize();
preferred.width = Math.min(Math.max(preferred.width, minimum.width), maximum.width);
preferred.height = Math.min(Math.max(preferred.height, minimum.height), maximum.height);
return preferred;
}
public void paint(Graphics g) {
drawBar((Graphics2D) g, getBounds());
}
void drawBar(Graphics2D g, Rectangle area) {
Pattern regex = Pattern.compile("^([0-9]+)##(.)}");
HashMap<Integer, Integer> totals_at_cmcs = new HashMap<Integer, Integer>();
int max_num_pips = 0;
int max_cmc = 0;
for (String key : pips_at_cmcs.keySet()) {
Matcher regexMatcher = regex.matcher(key);
int num_pips = pips_at_cmcs.get(key);
while (regexMatcher.find()) {
String cmc = regexMatcher.group(1);
int cmc_num = Integer.parseInt(cmc);
if (max_cmc < cmc_num) {
max_cmc = cmc_num;
}
int total_at_cmc = 0;
if (totals_at_cmcs.get(cmc_num) != null) {
total_at_cmc = totals_at_cmcs.get(cmc_num);
}
totals_at_cmcs.put(cmc_num, total_at_cmc + num_pips);
if (max_num_pips < total_at_cmc + num_pips) {
max_num_pips = total_at_cmc + num_pips;
}
}
}
int height_factor = 200 / max_num_pips;
int width_factor = 200 / (max_cmc + 2);
if (width_factor > 20) {
width_factor = 20;
}
if (width_factor < 11) {
width_factor = 11;
}
g.setColor(Color.LIGHT_GRAY);
for (int i = 0; i < max_num_pips; i++) {
if (i % 10 == 0) {
g.drawLine(0, 200 - 1 - i * height_factor, 400, 200 - 1 - i * height_factor);
} else if (i % 10 == 5) {
Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{5}, 0);
Stroke oldstroke = g.getStroke();
g.setStroke(dashed);
g.drawLine(0, 200 - 1 - i * height_factor, 400, 200 - 1 - i * height_factor);
g.setStroke(oldstroke);
}
}
HashMap<Integer, Integer> running_totals_at_cmcs = new HashMap<Integer, Integer>();
for (String key : pips_at_cmcs.keySet()) {
Matcher regexMatcher = regex.matcher(key);
int num_pips = pips_at_cmcs.get(key);
while (regexMatcher.find()) {
String cmc = regexMatcher.group(1);
int cmc_num = Integer.parseInt(cmc);
String color = regexMatcher.group(2);
int total_at_cmc = 0;
if (running_totals_at_cmcs.get(cmc_num) != null) {
total_at_cmc = running_totals_at_cmcs.get(cmc_num);
}
if (color.equalsIgnoreCase("w")) {
g.setColor(Color.WHITE);
}
if (color.equalsIgnoreCase("u")) {
g.setColor(Color.BLUE);
}
if (color.equalsIgnoreCase("b")) {
g.setColor(Color.BLACK);
}
if (color.equalsIgnoreCase("r")) {
g.setColor(Color.RED);
}
if (color.equalsIgnoreCase("g")) {
g.setColor(Color.GREEN);
}
if (color.equalsIgnoreCase("c")) {
g.setColor(Color.DARK_GRAY);
}
g.fill(new Rectangle2D.Double(cmc_num * width_factor, 200 - 1 - total_at_cmc - num_pips * height_factor, 10, num_pips * height_factor));
running_totals_at_cmcs.put(cmc_num, total_at_cmc + num_pips * height_factor);
}
}
}
}