From 1a0cf14120b9854d4523b32ca86875c7222130bb Mon Sep 17 00:00:00 2001 From: jberclaz Date: Sat, 6 Jul 2024 10:10:27 -0700 Subject: [PATCH] Clean up --- .../java/com/leflat/jass/client/CanvasCenter.java | 4 ++-- .../java/com/leflat/jass/client/CanvasLastPlie.java | 2 +- src/main/java/com/leflat/jass/client/CardImages.java | 2 +- .../java/com/leflat/jass/client/ClientNetwork.java | 4 ++-- .../java/com/leflat/jass/client/ModernGamePanel.java | 11 ++++++++--- .../com/leflat/jass/client/ModernStatusPanel.java | 3 +++ .../java/com/leflat/jass/client/RemoteController.java | 7 +------ .../java/com/leflat/jass/client/SwissCardImages.java | 2 +- .../java/com/leflat/jass/common/Announcement.java | 4 ++-- src/main/java/com/leflat/jass/common/Card.java | 4 ++-- 10 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/leflat/jass/client/CanvasCenter.java b/src/main/java/com/leflat/jass/client/CanvasCenter.java index faf7d59..415ee1a 100644 --- a/src/main/java/com/leflat/jass/client/CanvasCenter.java +++ b/src/main/java/com/leflat/jass/client/CanvasCenter.java @@ -34,8 +34,8 @@ public class CanvasCenter extends JPanel { private int mode; /* 0 : rien, 1 : tirer les équipes * 2 : tirer les équipes et choisir une carte * 3 : jouer */ - private List drawnCards = new ArrayList<>(); - private Map shownCards = new HashMap<>(); + private final List drawnCards = new ArrayList<>(); + private final Map shownCards = new HashMap<>(); public CanvasCenter() { mode = MODE_PASSIVE; diff --git a/src/main/java/com/leflat/jass/client/CanvasLastPlie.java b/src/main/java/com/leflat/jass/client/CanvasLastPlie.java index fb19e77..0140e9a 100644 --- a/src/main/java/com/leflat/jass/client/CanvasLastPlie.java +++ b/src/main/java/com/leflat/jass/client/CanvasLastPlie.java @@ -21,7 +21,7 @@ import java.util.List; public class CanvasLastPlie extends JPanel { - private List lastPlie = new ArrayList<>(); + private final List lastPlie = new ArrayList<>(); private int atout; private int ourScore, theirScore; diff --git a/src/main/java/com/leflat/jass/client/CardImages.java b/src/main/java/com/leflat/jass/client/CardImages.java index 6919308..81ca0e1 100644 --- a/src/main/java/com/leflat/jass/client/CardImages.java +++ b/src/main/java/com/leflat/jass/client/CardImages.java @@ -60,7 +60,7 @@ private static String imagePath(int number) { } public static BufferedImage getImage(Card card) { - if (!card.isBack()) { + if (card.isFront()) { return images[card.getNumber()]; } return backImage; diff --git a/src/main/java/com/leflat/jass/client/ClientNetwork.java b/src/main/java/com/leflat/jass/client/ClientNetwork.java index ee8a2ba..0f34d30 100644 --- a/src/main/java/com/leflat/jass/client/ClientNetwork.java +++ b/src/main/java/com/leflat/jass/client/ClientNetwork.java @@ -50,10 +50,10 @@ public ClientConnectionInfo connect(String host, int requestedGameId, String nam return new ClientConnectionInfo(receivedGameId); } playerId = Integer.parseInt(receiveRawMessage()); - var encodedName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString()); + var encodedName = URLEncoder.encode(name, StandardCharsets.UTF_8); sendMessage(Collections.singletonList(encodedName)); return new ClientConnectionInfo(playerId, receivedGameId, ConnectionError.CONNECTION_SUCCESSFUL); - } catch (ServerDisconnectedException | UnsupportedEncodingException e) { + } catch (ServerDisconnectedException e) { LOGGER.log(Level.WARNING, "Server disconnected during connection", e); } return new ClientConnectionInfo(ConnectionError.SERVER_UNREACHABLE); diff --git a/src/main/java/com/leflat/jass/client/ModernGamePanel.java b/src/main/java/com/leflat/jass/client/ModernGamePanel.java index 33ab864..817b43c 100644 --- a/src/main/java/com/leflat/jass/client/ModernGamePanel.java +++ b/src/main/java/com/leflat/jass/client/ModernGamePanel.java @@ -19,6 +19,11 @@ import static java.lang.Math.*; +/** + * This class implements the central canvas on the player client, including the pad where cards are played, + * the players hands and the info area underneath the main player's cards. It also contains the logic for + * interactive cards selection. + */ public class ModernGamePanel extends JPanel implements MouseMotionListener { enum GameMode { @@ -336,7 +341,7 @@ private Rectangle2D.Float getHandArea(Rectangle2D.Float renderingArea) { return new Rectangle2D.Float(0, 0, 0, 0); } var hand = player.getHand(); - if (hand.size() == 0) { + if (hand.isEmpty()) { return new Rectangle2D.Float(0, 0, 0, 0); } var playerArea = getPlayerArea(renderingArea); @@ -355,7 +360,7 @@ private Rectangle getCardArea(int number, Rectangle2D.Float area) { float card_x_step = hand.size() < 2 ? 0 : (handArea.width - cardDimension.width) / (float) (hand.size() - 1); return new Rectangle(round(handArea.x + number * card_x_step), round(handArea.y), - round(cardDimension.width), round(cardDimension.height)); + cardDimension.width, cardDimension.height); } private Rectangle2D.Float getTeamDrawingArea(Rectangle2D.Float renderingArea) { @@ -748,7 +753,7 @@ protected void paintComponent(Graphics g) { var ia = toInt(getInfoArea(renderingArea)); var ha = toInt(getHandArea(renderingArea)); var da = toInt(getTeamDrawingArea(renderingArea)); - if (players.containsKey(PlayerPosition.MYSELF) && players.get(PlayerPosition.MYSELF).getHand().size() > 0) { + if (players.containsKey(PlayerPosition.MYSELF) && !players.get(PlayerPosition.MYSELF).getHand().isEmpty()) { var cca = getCardArea(0, renderingArea); g2.drawRect(cca.x, cca.y, cca.width, cca.height); } diff --git a/src/main/java/com/leflat/jass/client/ModernStatusPanel.java b/src/main/java/com/leflat/jass/client/ModernStatusPanel.java index db1d4f5..5206dad 100644 --- a/src/main/java/com/leflat/jass/client/ModernStatusPanel.java +++ b/src/main/java/com/leflat/jass/client/ModernStatusPanel.java @@ -4,6 +4,9 @@ import javax.swing.border.BevelBorder; import java.awt.*; +/** + * This class contains the information text box at the bottom of the UI. + */ public class ModernStatusPanel extends JPanel { private String text = ""; diff --git a/src/main/java/com/leflat/jass/client/RemoteController.java b/src/main/java/com/leflat/jass/client/RemoteController.java index e1e363e..56f8219 100644 --- a/src/main/java/com/leflat/jass/client/RemoteController.java +++ b/src/main/java/com/leflat/jass/client/RemoteController.java @@ -65,12 +65,7 @@ private void handleControllerMessage(String[] message) { switch (command) { case RemoteCommand.SET_PLAYER_INFO: int playerId = Integer.parseInt(message[1]); - String name = null; - try { - name = URLDecoder.decode(message[2], StandardCharsets.UTF_8.toString()); - } catch (UnsupportedEncodingException e) { - LOGGER.log(Level.SEVERE, "Unable to decode name", e); - } + String name = URLDecoder.decode(message[2], StandardCharsets.UTF_8); player.setPlayerInfo(new ClientPlayer(playerId, name)); break; case RemoteCommand.CHOOSE_TEAM_SELECTION_METHOD: diff --git a/src/main/java/com/leflat/jass/client/SwissCardImages.java b/src/main/java/com/leflat/jass/client/SwissCardImages.java index 1e0547d..892ce5e 100644 --- a/src/main/java/com/leflat/jass/client/SwissCardImages.java +++ b/src/main/java/com/leflat/jass/client/SwissCardImages.java @@ -59,7 +59,7 @@ private static String imagePath(int number) { } public static BufferedImage getImage(Card card) { - if (!card.isBack()) { + if (card.isFront()) { return images[card.getNumber()]; } return backImage; diff --git a/src/main/java/com/leflat/jass/common/Announcement.java b/src/main/java/com/leflat/jass/common/Announcement.java index eb5aa65..b90e454 100644 --- a/src/main/java/com/leflat/jass/common/Announcement.java +++ b/src/main/java/com/leflat/jass/common/Announcement.java @@ -26,8 +26,8 @@ public Announcement(int type, Card card) { this.card = card; } - private int type; // 0: stöck, 1: 3 cartes, 2: cinquante, 3: cent, 4: carré - private Card card; // plus haute carte de l'annonce + private final int type; // 0: stöck, 1: 3 cartes, 2: cinquante, 3: cent, 4: carré + private final Card card; // plus haute carte de l'annonce public int getType() { return type; diff --git a/src/main/java/com/leflat/jass/common/Card.java b/src/main/java/com/leflat/jass/common/Card.java index 98e3853..26ee306 100644 --- a/src/main/java/com/leflat/jass/common/Card.java +++ b/src/main/java/com/leflat/jass/common/Card.java @@ -74,8 +74,8 @@ public int getValue() { return getColor() == Card.atout ? VALUES_ATOUT[getRank()] : VALUES[getRank()]; } - public boolean isBack() { - return number == BACK_NUMBER; + public boolean isFront() { + return number != BACK_NUMBER; } public static Card getBack() {