From b6a7998eab4416a58ee5819661f26d82ce36421d Mon Sep 17 00:00:00 2001 From: spjspj Date: Sat, 29 Jul 2017 23:55:58 +1000 Subject: [PATCH] Mana Circles for deck analysis --- .../java/mage/client/cards/DragCardGrid.java | 55 ++++++------ .../java/mage/client/cards/ManaPieChart.java | 84 +++++++++++++++++++ 2 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java diff --git a/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java b/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java index 2e611c93cb..6973fa4ea7 100644 --- a/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java +++ b/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java @@ -7,7 +7,6 @@ import mage.cards.decks.DeckCardLayout; import mage.cards.repository.CardCriteria; import mage.cards.repository.CardInfo; import mage.cards.repository.CardRepository; -import mage.client.MageFrame; import mage.client.constants.Constants; import mage.client.dialog.PreferencesDialog; import mage.client.plugins.impl.Plugins; @@ -1410,38 +1409,36 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } } - String finalInfo = "Found the following quantity of mana costs, mana sources and land types:
"; + JPanel panel4 = new JPanel(); + panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS)); + ManaPieChart chart3 = new ManaPieChart(manaCounts.get("{W}"), manaCounts.get("{U}"), manaCounts.get("{B}"), manaCounts.get("{R}"), manaCounts.get("{G}"), manaCounts.get("{C}")); + chart3.setMinimumSize(new Dimension(200, 200)); + panel4.add(new JLabel("Mana sources found:")); + panel4.add(chart3); - MageFrame.getInstance().showMessage(finalInfo); + panel.add(panel2); + panel.add(panel3); + panel.add(panel4); + + JFrame frame = new JFrame("JOptionPane showMessageDialog component example"); + JOptionPane.showMessageDialog(frame, panel, "This is the distribution of colors found", JOptionPane.INFORMATION_MESSAGE); } public void blingDeck() { diff --git a/Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java b/Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java new file mode 100644 index 0000000000..a8d447e700 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/cards/ManaPieChart.java @@ -0,0 +1,84 @@ +package mage.client.cards; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.util.ArrayList; + +import javax.swing.JComponent; + +class Slice { + + double value; + Color color; + + public Slice(double value, Color color) { + this.value = value; + this.color = color; + } +} + +public class ManaPieChart extends JComponent { + + ArrayList slices = new ArrayList(); + + ManaPieChart() { + } + + ManaPieChart(Integer w, Integer u, Integer b, Integer r, Integer g, Integer c) { + if (w != null && w > 0) { + slices.add(new Slice(w, Color.WHITE)); + } + if (u != null && u > 0) { + slices.add(new Slice(u, Color.BLUE)); + } + if (b != null && b > 0) { + slices.add(new Slice(b, Color.BLACK)); + } + if (r != null && r > 0) { + slices.add(new Slice(r, Color.RED)); + } + if (g != null && g > 0) { + slices.add(new Slice(g, Color.GREEN)); + } + if (c != null && c > 0) { + slices.add(new Slice(c, Color.LIGHT_GRAY)); + } + } + + @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) { + drawPie((Graphics2D) g, getBounds(), slices.toArray(new Slice[slices.size()])); + } + + void drawPie(Graphics2D g, Rectangle area, Slice[] slices) { + double total = 0.0D; + for (int i = 0; i < slices.length; i++) { + total += slices[i].value; + } + + double curValue = 0.0D; + int startAngle = 0; + int lastAngle = 0; + for (int i = 0; i < slices.length; i++) { + startAngle = lastAngle; + int arcAngle = (int) (slices[i].value * 360 / total); + + g.setColor(slices[i].color); + g.fillArc(area.x, area.y, area.width - 20, area.height - 20, startAngle, arcAngle); + curValue += slices[i].value; + lastAngle += arcAngle; + } + } +}