From 0fdd579b41fb075b5bcf596857af4192432572d3 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Tue, 31 Dec 2019 15:41:10 +0100 Subject: [PATCH] Aufgabe 4.2 - drawStones --- src/break_out/model/Level.java | 5 +++- src/break_out/view/Field.java | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index 772f850..e65d072 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -235,7 +235,10 @@ public class Level extends Thread implements ILevel { * @return stones The stones of the level */ public ArrayList getStones() { - return stones; + // hacky workaround for ConcurrentModificationExceptions + ArrayList copy = new ArrayList<>(); + copy.addAll(stones); + return copy; } } diff --git a/src/break_out/view/Field.java b/src/break_out/view/Field.java index 67964cd..6c57969 100644 --- a/src/break_out/view/Field.java +++ b/src/break_out/view/Field.java @@ -5,10 +5,13 @@ import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; +import java.util.ArrayList; import javax.swing.JPanel; import break_out.Constants; +import break_out.model.Position; +import break_out.model.Stone; import net.miginfocom.swing.MigLayout; /** @@ -99,6 +102,9 @@ public class Field extends JPanel { // Calls the method for drawing the grid drawGrid(g2); + // Calls the method for drawing the stones + drawStones(g2); + // Calls the method for drawing the ball drawBall(g2); @@ -205,4 +211,42 @@ public class Field extends JPanel { g2.setColor(temp); } + /** + * Draws the stones + * + * @param g2 The graphics object + */ + private void drawStones(Graphics2D g2) { + // query stones + ArrayList stones = view.getGame().getLevel().getStones(); + + // foreach stone + for(Stone stone : stones) { + Position pos = stone.getPosition(); + + // size of grid blocks + int blockWidth = Constants.SCREEN_WIDTH / Constants.SQUARES_X; + int blockHeight = Constants.SCREEN_HEIGHT / Constants.SQUARES_Y; + + // if stone has a color, draw it + if(stone.getColor() != null) { + + // temporarily save default component color to draw stone in specific color + Color temp = g2.getColor(); + g2.setColor(stone.getColor()); + + // fillRoundRect(x, y, width, height, arcWidth, arcHeight) + g2.fillRoundRect((int) pos.getX()+1, + (int) pos.getY()+1, + (int) blockWidth-1, + (int) blockHeight-1, + 0, + 0); + + // reset color to default + g2.setColor(temp); + } + } + } + }