Skip to content

Commit

Permalink
Revamped border detection and drawing
Browse files Browse the repository at this point in the history
Changed how borders were counted and drawn to be more in line with skyscrapers
  • Loading branch information
offline171 committed Oct 1, 2024
1 parent 1f2e980 commit 217f4e8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

public class StarBattleBorderView extends ElementView {
private static final Color Border_COLOR = Color.BLACK;
private StarBattleCellType type;

public StarBattleBorderView(StarBattleBorder border) {
super(border);
type = border.getType();
}

/**
Expand Down Expand Up @@ -38,9 +40,15 @@ public void drawElement(Graphics2D graphics2D) {

StarBattleBorder border= getPuzzleElement();

int xBorder = location.x + (size.width / 2);
int yBorder = location.y + (size.height / 2);
int xSize = size.width;
int ySize = size.height;
if(type == StarBattleCellType.HORIZ_BORDER){ //minimize ySize / height
ySize = ySize / 8;
}
else if(type == StarBattleCellType.VERT_BORDER){ //minimize xSize / width
xSize = xSize / 8;
}
graphics2D.draw(new Rectangle2D.Double(
location.x + 1.5f, location.y + 1.5f, size.width - 3, size.height - 3));
location.x, location.y, xSize, ySize));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public void drawElement(Graphics2D graphics2D) {
graphics2D.drawRect(location.x, location.y, size.width, size.height);
}
*/
}

}
}
117 changes: 67 additions & 50 deletions src/main/java/edu/rpi/legup/puzzle/starbattle/StarBattleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

public class StarBattleView extends GridBoardView {
static Image STAR;
private ArrayList<ArrayList<Boolean>> horizontalBorders; //board.size * board.size+1 left-right up-down
private ArrayList<ArrayList<Boolean>> verticalBorders; //board.size+1 * board.size left-right up-down
private ArrayList<StarBattleBorderView> horizontalBorders; //board.size * board.size+1 left-right up-down
private ArrayList<StarBattleBorderView> verticalBorders; //board.size+1 * board.size left-right up-down

static {
try {
Expand Down Expand Up @@ -42,79 +42,96 @@ public StarBattleView(StarBattleBoard board) {
elementViews.add(elementView);
}

//Make borders by just making a list of border objects and saving their locations as they come
//then just draw all of them one at a time

//initialize horizontal borders, the ones that are between two cells along the y-axis, and look like -- not |
for(int i = 0; i < board.getWidth(); i++){
ArrayList<Boolean> temp = new ArrayList<>();
for(int j = 0; j < board.getHeight() + 1; j++){ //+1 to account for sides of board
if(j == 0 || j == board.getHeight()){ //set borders at the ends of the board
temp.add(Boolean.TRUE);
StarBattleBorderView temp = new StarBattleBorderView(new StarBattleBorder(StarBattleCellType.HORIZ_BORDER));
temp.setSize(elementSize);
if(j == 0){ //set borders at the ends of the board
temp.setLocation(endCell(board.getCell(i,j), 8, elementSize));
horizontalBorders.add(temp);
}
else if(board.getCell(i, j-1).getGroupIndex() != board.getCell(i, j).getGroupIndex()){ //general case
temp.add(Boolean.TRUE); //adds border when two adjacent cells aren't from the same region
else if(j == board.getHeight()){
temp.setLocation(endCell(board.getCell(i,j), 2, elementSize));
horizontalBorders.add(temp);
}
else{
temp.add(Boolean.FALSE);
else if(board.getCell(i, j-1).getGroupIndex() != board.getCell(i, j).getGroupIndex()){ //general case
//adds border when two adjacent cells aren't from the same region
temp.setLocation(betweenCells(board.getCell(i, j-1), board.getCell(i,j)));
horizontalBorders.add(temp);
}
//no else statement. If none of these ifs are met, then just don't add it to the list
}
horizontalBorders.add(temp);
}
//initialize vertical borders, the ones that are between two cells along the x-axis, and look like | not --
//largely the same code as horizontal border adder but i and j are flipped and general case checks cells adjacent
//along i (x) instead of j (y)
for(int j = 0; j < board.getHeight(); j++){ //initialize j (y) first since we're checking the opposite axis
ArrayList<Boolean> temp = new ArrayList<>();
for(int i = 0; i < board.getHeight() + 1; i++){ //+1 to account for sides of board
if(i == 0 || i == board.getWidth()){ //set borders at the ends of the board
temp.add(Boolean.TRUE);
StarBattleBorderView temp = new StarBattleBorderView(new StarBattleBorder(StarBattleCellType.VERT_BORDER));
temp.setSize(elementSize);
if(i == 0){ //set borders at the ends of the board
temp.setLocation(endCell(board.getCell(i,j), 4, elementSize));
verticalBorders.add(temp);
}
else if(board.getCell(i-1, j).getGroupIndex() != board.getCell(i, j).getGroupIndex()){ //general case
temp.add(Boolean.TRUE); //adds border when two adjacent cells aren't from the same region
else if(i == board.getHeight()){
temp.setLocation(endCell(board.getCell(i,j), 6, elementSize));
verticalBorders.add(temp);
}
else{
temp.add(Boolean.FALSE);
else if(board.getCell(i-1, j).getGroupIndex() != board.getCell(i, j).getGroupIndex()){ //general case
//adds border when two adjacent cells aren't from the same region
temp.setLocation(betweenCells(board.getCell(i-1, j), board.getCell(i,j)));
verticalBorders.add(temp);
}
}
verticalBorders.add(temp);
}
}
@Override
public void drawBoard(Graphics2D graphics2D){
super.drawBoard(graphics2D);
int x = 0;
int y = 0;

for(ArrayList<Boolean> border: horizontalBorders){
for(Boolean lines: border){
if(lines){
//draw a horizontal line
//Direction means which side of the cell in question should have a border on it
//Numpad rules. 2 is down, 4 is left, 6 is right, 8 is up (based on visuals not coordinates, since y is from up to down)
public Point endCell(StarBattleCell one, int direction, Dimension elementSize){
Point temp = new Point(
one.getLocation().x,
one.getLocation().y
);
if(direction == 2){
temp.y += elementSize.height / 2;
}
if(direction == 4){
temp.y -= elementSize.width / 2;
}
if(direction == 6){
temp.y += elementSize.width / 2;
}
if(direction == 8){
temp.y -= elementSize.height / 2;
}
return temp;
}

StarBattleBorder Horiz = new StarBattleBorder(StarBattleCellType.HORIZ_BORDER);
StarBattleBorderView horizontalBorder = new StarBattleBorderView(Horiz);
//finds average point location between two cells
public Point betweenCells(StarBattleCell one, StarBattleCell two){
return new Point(
(one.getLocation().x + two.getLocation().x) / 2,
(one.getLocation().y + two.getLocation().y) / 2
);
}

//offset by an amount that makes puts borders between two vertically adjacent borders
horizontalBorder.setLocation(new Point((x/ gridSize.width) * elementSize.width, (y/gridSize.height) * elementSize.height));
@Override
public void drawBoard(Graphics2D graphics2D){
super.drawBoard(graphics2D);

//still need to figure out how to translate over a location
}
y++;//keep track of y index as the program iterates
}
x++; //keep track of x index as the program iterates
for(StarBattleBorderView border: horizontalBorders){
//draw a horizontal line
border.draw(graphics2D);
}

for(ArrayList<Boolean> border: horizontalBorders){
for(Boolean lines: border){
if(lines){
//draw a vertical line according to the index

StarBattleBorder Vert = new StarBattleBorder(StarBattleCellType.VERT_BORDER);
StarBattleBorderView verticalBorder = new StarBattleBorderView(Vert);

//offset by an amount that makes puts borders between two horizontally adjacent borders
verticalBorder.setLocation(new Point((x/ gridSize.width) * elementSize.width, (y/gridSize.height) * elementSize.height));
}
x++;//keep track of x index, and yes I'm pretty sure the x and y axes are swapped
}
y++; //keep track of y index
for(StarBattleBorderView border: verticalBorders){
//draw a vertical line
border.draw(graphics2D);
}
}
}

0 comments on commit 217f4e8

Please sign in to comment.