diff --git a/Mage.Client/src/main/java/mage/client/util/gui/ArrowBuilder.java b/Mage.Client/src/main/java/mage/client/util/gui/ArrowBuilder.java index f8760c5165..be0d4329f2 100644 --- a/Mage.Client/src/main/java/mage/client/util/gui/ArrowBuilder.java +++ b/Mage.Client/src/main/java/mage/client/util/gui/ArrowBuilder.java @@ -1,5 +1,7 @@ package mage.client.util.gui; +import com.google.common.collect.MapMaker; + import javax.swing.*; import java.awt.*; import java.util.*; @@ -7,7 +9,7 @@ import java.util.List; /** * Class for dealing with arrows in the game. - * + * * @author nantuko, noxx */ public class ArrowBuilder { @@ -18,31 +20,25 @@ public class ArrowBuilder { instance = new ArrowBuilder(); } + /** + * Stores arrow panels per game + */ + private final Map arrowPanels = new HashMap<>(); + private final Map>> map = new MapMaker().weakKeys().weakValues().makeMap(); + /** + * The top panel where arrow panels are added to. + */ + private JPanel arrowsManagerPanel; + private int currentWidth; + private int currentHeight; + public static ArrowBuilder getBuilder() { return instance; } - /** - * The top panel where arrow panels are added to. - */ - private JPanel arrowsManagerPanel; - - /** - * Stores arrow panels per game - */ - private final Map arrowPanels = new HashMap(); - - private final Map>> map = new HashMap>>(); - - private int currentWidth; - private int currentHeight; - - public enum Type { - PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS - } - /** * Get the panel where all arrows are being drawn. + * * @return */ public JPanel getArrowsManagerPanel() { @@ -58,7 +54,7 @@ public class ArrowBuilder { } return arrowsManagerPanel; } - + private JPanel getArrowsPanel(UUID gameId) { if (!arrowPanels.containsKey(gameId)) { JPanel arrowPanel = new JPanel(); @@ -73,20 +69,9 @@ public class ArrowBuilder { return arrowPanels.get(gameId); } - /** - * Not synchronized method for arrows panel. - * Doesn't create JPanel in case the panel doesn't exist. - * Works faster. - * - * @return - */ - /*public JPanel getPanelRef() { - return arrowsManagerPanel; - }*/ - /** * Adds new arrow. - * + * * @param startX * @param startY * @param endX @@ -100,18 +85,25 @@ public class ArrowBuilder { arrow.setColor(color); arrow.setArrowLocation(startX, startY, endX, endY); arrow.setBounds(0, 0, Math.max(startX, endX) + 40, Math.max(startY, endY) + 30); // 30 is offset for arrow heads (being cut otherwise) - - synchronized (map) { - p.add(arrow); - Map> innerMap = map.computeIfAbsent(gameId, k -> new HashMap>()); - java.util.List arrows = innerMap.computeIfAbsent(type, k -> new ArrayList()); - arrows.add(arrow); - } - + p.add(arrow); + Map> innerMap = map.computeIfAbsent(gameId, k -> new HashMap<>()); + List arrows = innerMap.computeIfAbsent(type, k -> new ArrayList<>()); + arrows.add(arrow); p.revalidate(); p.repaint(); } + /** + * Not synchronized method for arrows panel. + * Doesn't create JPanel in case the panel doesn't exist. + * Works faster. + * + * @return + */ + /*public JPanel getPanelRef() { + return arrowsManagerPanel; + }*/ + /** * Removes all arrows from the screen. */ @@ -119,15 +111,13 @@ public class ArrowBuilder { if (map.containsKey(gameId)) { Map> innerMap = map.get(gameId); JPanel p = getArrowsPanel(gameId); - synchronized (map) { - if (p != null && p.getComponentCount() > 0) { - p.removeAll(); - p.revalidate(); - p.repaint(); - } - innerMap.clear(); - map.remove(gameId); + if (p != null && p.getComponentCount() > 0) { + p.removeAll(); + p.revalidate(); + p.repaint(); } + innerMap.clear(); + map.remove(gameId); } } @@ -137,18 +127,16 @@ public class ArrowBuilder { java.util.List arrows = innerMap.get(type); if (arrows != null && !arrows.isEmpty()) { JPanel p = getArrowsPanel(gameId); - synchronized (map) { - for (Arrow arrow : arrows) { - p.remove(arrow); - } - innerMap.put(type, new ArrayList()); + for (Arrow arrow : arrows) { + p.remove(arrow); } + innerMap.put(type, new ArrayList<>()); p.revalidate(); p.repaint(); } } } - + public void setSize(int width, int height) { this.currentWidth = width; this.currentHeight = height; @@ -173,4 +161,8 @@ public class ArrowBuilder { } } + public enum Type { + PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS + } + }