-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
612 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.class | ||
target/ | ||
log/ | ||
log/ | ||
practice/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.nhnacademy</groupId> | ||
<artifactId>breakout</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-api</artifactId> | ||
<version>2.22.1</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>2.22.1</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.nhnacademy; | ||
|
||
import java.awt.Rectangle; | ||
|
||
public class Ball implements Bounded { | ||
int x; | ||
int y; | ||
int radius; | ||
|
||
public Ball(int x, int y, int radius) { | ||
if (((long) x + radius > Integer.MAX_VALUE) | ||
|| ((long) x - radius < Integer.MIN_VALUE) | ||
|| ((long) y + radius > Integer.MAX_VALUE) | ||
|| ((long) y - radius < Integer.MIN_VALUE)) { | ||
throw new IllegalArgumentException("볼이 공간을 벗어 납니다."); | ||
} | ||
|
||
if (radius < 0) { | ||
throw new IllegalArgumentException("반지름은 음수가 될 수 없습니다."); | ||
} | ||
|
||
this.x = x; | ||
this.y = y; | ||
this.radius = radius; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
void setY(int y) { | ||
this.y = y; | ||
} | ||
|
||
public int getRadius() { | ||
return radius; | ||
} | ||
|
||
public int getMinX() { | ||
return x - radius; | ||
} | ||
|
||
public int getMaxX() { | ||
return x + radius; | ||
} | ||
|
||
public int getMinY() { | ||
return y - radius; | ||
} | ||
|
||
public int getMaxY() { | ||
return y + radius; | ||
} | ||
|
||
public int getWidth() { | ||
return 2 * radius; | ||
} | ||
|
||
public int getHeight() { | ||
return 2 * radius; | ||
} | ||
|
||
public Rectangle getBounds() { | ||
return new Rectangle(getMinX(), getMinY(), getWidth(), getHeight()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
sb.append("[") | ||
.append(x).append(",") | ||
.append(y).append(",") | ||
.append(radius) | ||
.append("]"); | ||
|
||
return sb.toString(); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
example/breakout/src/main/java/com/nhnacademy/Bounded.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.nhnacademy; | ||
|
||
import java.awt.Rectangle; | ||
|
||
public interface Bounded { | ||
Rectangle getBounds(); | ||
|
||
int getMinX(); | ||
|
||
int getMaxX(); | ||
|
||
int getMinY(); | ||
|
||
int getMaxY(); | ||
|
||
int getWidth(); | ||
|
||
int getHeight(); | ||
} |
54 changes: 54 additions & 0 deletions
54
example/breakout/src/main/java/com/nhnacademy/BoundedBall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.nhnacademy; | ||
|
||
import java.awt.Color; | ||
import java.awt.Rectangle; | ||
|
||
public class BoundedBall extends MovableBall { | ||
|
||
Rectangle bounds; | ||
|
||
public BoundedBall(int x, int y, int radius) { | ||
super(x, y, radius); | ||
|
||
bounds = new Rectangle(getMinX(), getMinY(), getWidth(), getHeight()); | ||
} | ||
|
||
public BoundedBall(int x, int y, int radius, Color color) { | ||
super(x, y, radius, color); | ||
|
||
bounds = new Rectangle(getMinX(), getMinY(), getWidth(), getHeight()); | ||
} | ||
|
||
public Rectangle getBounds() { | ||
return bounds; | ||
} | ||
|
||
public void setBounds(Rectangle bounds) { | ||
this.bounds = new Rectangle(bounds); | ||
} | ||
|
||
public boolean isOutOfBounds() { | ||
return (getMinX() < bounds.getMinX()) | ||
|| (bounds.getMaxX() < getMaxX()) | ||
|| (getMinY() < bounds.getMinY()) | ||
|| (bounds.getMaxY() < getMaxY()); | ||
} | ||
|
||
@Override | ||
public void move() { | ||
super.move(); | ||
if (isOutOfBounds()) { | ||
bounce(); | ||
} | ||
} | ||
|
||
public void bounce() { | ||
if ((getMinX() < bounds.getMinX()) || (bounds.getMaxX() < getMaxX())) { | ||
setDX(-getDX()); | ||
} | ||
|
||
if ((getMinY() < bounds.getMinY()) || (bounds.getMaxY() < getMaxY())) { | ||
setDY(-getDY()); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
example/breakout/src/main/java/com/nhnacademy/BoundedWorld.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.nhnacademy; | ||
|
||
public class BoundedWorld extends MovableWorld { | ||
|
||
public boolean outOfBounds(Bounded bounded) { | ||
return bounded.getMinX() < getBounds().getMinX() | ||
|| bounded.getMaxX() > getBounds().getMaxX() | ||
|| bounded.getMinY() < getBounds().getMinY() | ||
|| bounded.getMaxY() > getBounds().getMaxY(); | ||
} | ||
|
||
public void bounce(Bounded bounded) { | ||
if (bounded instanceof MovableBall) { | ||
if (bounded.getMinX() < getBounds().getMinX() | ||
|| bounded.getMaxX() > getBounds().getMaxX()) { | ||
((MovableBall) bounded).setDX(-((MovableBall) bounded).getDX()); | ||
} | ||
if (bounded.getMinY() < getBounds().getMinY() | ||
|| bounded.getMaxY() > getBounds().getMaxY()) { | ||
((MovableBall) bounded).setDY(-((MovableBall) bounded).getDY()); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void move() { | ||
super.move(); | ||
|
||
for (int i = 0; i < getCount(); i++) { | ||
Bounded bounded = get(i); | ||
|
||
if (bounded instanceof MovableBall) { | ||
if (outOfBounds(bounded)) { | ||
bounce(bounded); | ||
} | ||
|
||
for (int j = 0; j < getCount(); j++) { | ||
if (i != j) { | ||
Bounded other = get(j); | ||
|
||
if (bounded.getBounds().intersects(other.getBounds())) { | ||
((Movable) bounded).bounce(other); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.nhnacademy; | ||
|
||
import java.awt.Color; | ||
import java.util.Random; | ||
|
||
import javax.swing.JFrame; | ||
|
||
public class Main { | ||
static final int FRAME_WIDTH = 500; | ||
static final int FRAME_HEIGHT = 400; | ||
static final int BALL_COUNT = 2; | ||
static final int MOVE_COUNT = 0; | ||
static final Color[] colors = new Color[] { | ||
Color.BLUE, Color.RED, Color.WHITE, Color.BLACK, Color.GREEN | ||
}; | ||
|
||
public static void main(String[] args) { | ||
Random random = new Random(); | ||
JFrame frame = new JFrame(); | ||
|
||
BoundedWorld world = new BoundedWorld(); | ||
|
||
frame.add(world); | ||
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); | ||
frame.setVisible(true); | ||
|
||
world.add(new PaintableBall(FRAME_WIDTH / 2, FRAME_HEIGHT / 2, FRAME_HEIGHT / 4, Color.YELLOW)); | ||
while (world.getCount() < BALL_COUNT) { | ||
try { | ||
MovableBall ball = new MovableBall( | ||
random.nextInt(FRAME_WIDTH), | ||
random.nextInt(FRAME_HEIGHT), | ||
10 + random.nextInt(41), | ||
colors[random.nextInt(colors.length)]); | ||
|
||
ball.setDX((random.nextInt() < 0) ? -(random.nextInt(5) + 1) : (random.nextInt(5) + 1)); | ||
ball.setDY((random.nextInt() < 0) ? -(random.nextInt(5) + 1) : (random.nextInt(5) + 1)); | ||
|
||
world.add(ball); | ||
} catch (IllegalArgumentException e) { | ||
System.err.println(e.getMessage()); | ||
} | ||
} | ||
|
||
frame.repaint(); | ||
|
||
world.setMaxMoveVount(MOVE_COUNT); | ||
world.run(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
example/breakout/src/main/java/com/nhnacademy/Movable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.nhnacademy; | ||
|
||
public interface Movable { | ||
|
||
public int getDX(); | ||
|
||
public int getDY(); | ||
|
||
public void setDX(int dx); | ||
|
||
public void setDY(int dy); | ||
|
||
public void move(); | ||
|
||
public void moveTo(int x, int y); | ||
|
||
public void bounce(Bounded bounded); | ||
} |
60 changes: 60 additions & 0 deletions
60
example/breakout/src/main/java/com/nhnacademy/MovableBall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.nhnacademy; | ||
|
||
import java.awt.Color; | ||
import java.awt.Rectangle; | ||
|
||
public class MovableBall extends PaintableBall implements Movable { | ||
int dx; | ||
int dy; | ||
|
||
public MovableBall(int x, int y, int radius) { | ||
super(x, y, radius); | ||
} | ||
|
||
public MovableBall(int x, int y, int radius, Color color) { | ||
super(x, y, radius, color); | ||
} | ||
|
||
public int getDX() { | ||
return dx; | ||
} | ||
|
||
public int getDY() { | ||
return dy; | ||
} | ||
|
||
public void setDX(int dx) { | ||
this.dx = dx; | ||
} | ||
|
||
public void setDY(int dy) { | ||
this.dy = dy; | ||
} | ||
|
||
public void move() { | ||
// System.out.printf("(%d, %d) -> ", getX(), getY()); | ||
moveTo(getX() + dx, getY() + dy); | ||
// System.out.printf("(%d, %d)%n", getX(), getY()); | ||
} | ||
|
||
public void moveTo(int x, int y) { | ||
setX(x); | ||
setY(y); | ||
} | ||
|
||
public void bounce(Bounded bounded) { | ||
if (getBounds().intersects(bounded.getBounds())) { | ||
Rectangle intersection = getBounds().intersection(bounded.getBounds()); | ||
|
||
if (intersection.getWidth() != getBounds().getWidth() && intersection.getWidth() != bounded.getWidth()) { | ||
setDX(-getDX()); | ||
} | ||
|
||
if (intersection.getHeight() != getBounds().getHeight() | ||
&& intersection.getHeight() != bounded.getHeight()) { | ||
setDY(-getDY()); | ||
} | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.