1
0
Fork 0

Aufgabe 4.2 - drawStones

This commit is contained in:
rxbn_ 2019-12-31 15:41:10 +01:00
parent 669c37029a
commit 0fdd579b41
2 changed files with 48 additions and 1 deletions

View File

@ -235,7 +235,10 @@ public class Level extends Thread implements ILevel {
* @return stones The stones of the level
*/
public ArrayList<Stone> getStones() {
return stones;
// hacky workaround for ConcurrentModificationExceptions
ArrayList<Stone> copy = new ArrayList<>();
copy.addAll(stones);
return copy;
}
}

View File

@ -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<Stone> 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);
}
}
}
}