Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
xtra72 committed Jul 24, 2024
1 parent f7c069e commit b6151df
Show file tree
Hide file tree
Showing 30 changed files with 852 additions and 131 deletions.
2 changes: 1 addition & 1 deletion 06.new_object_box.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ image::./image/figure34.png"[title="exam_6_1_3_1",align=center]

==== 확인

* link:../src/main/java/com/nhnacademy/World.java[class World]
* link:./example/chapter06/src/main/java/com/nhnacademy/exam060401/World.java[class World]
* 새로운 종류 추가에 문제가 없는가?
* paint에서 Object class에 대해 처리가 가능한가?

Expand Down
2 changes: 1 addition & 1 deletion 07.simple_world.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ World class는 출력되는 오브젝트들은 모두 일정한 영역을 갖는

==== 확인

*link:../src/main/java/com/nhnacademy/World.java[class World]
* link:./example/chapter07/src/main/java/com/nhnacademy/exam070101/World.java[class World]
* 어떠한 문제점이 있는가?
* 해결 방법은? Regionable에 그리기 함수 추가?

Expand Down
9 changes: 5 additions & 4 deletions 09.external_effect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@
* 속도
+
[steam]
---
++++
\begin{align*}
v = {dx \over dt}
\end{align*}
---
++++


** 물체의 속도와 운동 방향으로 물체의 단위 시간당 위치 변화를 나타낸다.
** 시간이 지남에 따라 물체의 위치가 변경된다.

* 가속도
+
[steam]
---
++++
\begin{align*}
a={dv \over dt}
\end{align*}
---
++++

** 속도의 변화로써 물체의 단위 시간당 속도 변화를 나타낸다.
** 시간이 지남에 따라 물체의 속도가 변경된다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ActionListnerImpl implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'actionPerformed'");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nhnacademy.exam080101;

import java.awt.Color;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.WindowConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public Color getColor() {
return color;
}

@Override
public void paint(Graphics g) {
if (g == null) {
throw new IllegalArgumentException("Graphics context g is null");
Expand Down
41 changes: 8 additions & 33 deletions practice/240723/cannonball/src/main/java/com/nhnacademy/Ball.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,43 @@
package com.nhnacademy;

import java.awt.Color;
import java.awt.Rectangle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Ball {
public class Ball implements Regionable {
public static final Color DEFAULT_COLOR = Color.BLACK;
static Logger log = LogManager.getLogger(Ball.class);

int x;
int y;
int radius;
Region region;
Color color;

public Ball(int x, int y, int radius, Color color) {
if (radius < 0) {
throw new IllegalArgumentException();
}

this.x = x;
this.y = y;
this.radius = radius;
this.region = new Region(x, y, 2 * radius, 2 * radius);
this.color = color;
}

public Ball(int x, int y, int radius) {
this(x, y, radius, DEFAULT_COLOR);
}

public int getX() {
return x;
}

public int getY() {
return y;
}

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 Ball(Location location, int radius, Color color) {
this(location.getX(), location.getY(), radius, color);
}

public int getRadius() {
return radius;
return region.getWidth() / 2;
}

public Color getColor() {
return color;
}

public Rectangle getBounds() {
return new Rectangle(x - radius, y - radius, 2 * radius, 2 * radius);
public Region getRegion() {
return region;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nhnacademy;

import java.awt.Rectangle;

public interface Bounded {
Rectangle getBounds();

void setBounds(Rectangle bounds);

boolean isOutOfBounds(Rectangle bounds);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package com.nhnacademy;

import java.awt.Rectangle;

public class BoundedWorld extends MovableWorld {
public boolean outOfBounds(Ball ball) {
return (ball.getMinX() < getBounds().getMinX())
|| ball.getMaxX() > getBounds().getMaxX()
|| ball.getMinY() < getBounds().getMinY()
|| ball.getMaxY() > getBounds().getMaxY();
public boolean outOfBounds(Regionable object) {
return (object.getMinX() < getBounds().getMinX())
|| object.getMaxX() > getBounds().getMaxX()
|| object.getMinY() < getBounds().getMinY()
|| object.getMaxY() > getBounds().getMaxY();
}

public void bounceBall(MovableBall ball) {
if (outOfBounds(ball)) {
if ((ball.getMinX() < getBounds().getMinX()) || (ball.getMaxX() > getBounds().getMaxX())) {
ball.setDX(-ball.getDX());
public void bounce(Movable object) {
if (outOfBounds(object)) {
if ((object.getMinX() < getBounds().getMinX()) || (object.getMaxX() > getBounds().getMaxX())) {
object.getMotion().turnDX();
}

if ((ball.getMinY() < getBounds().getMinY()) || (ball.getMaxY() > getBounds().getMaxY())) {
ball.setDY(-ball.getDY());
if ((object.getMinY() < getBounds().getMinY()) || (object.getMaxY() > getBounds().getMaxY())) {
object.getMotion().turnDY();
}
}
}
Expand All @@ -26,32 +24,32 @@ public void bounceBall(MovableBall ball) {
public void move() {
super.move();

for (int i = 0; i < getBallCount(); i++) {
Ball ball = getBall(i);
for (int i = 0; i < getCount(); i++) {
Regionable object = get(i);

if (ball instanceof MovableBall) {
for (int j = 0; j < getBallCount(); j++) {
Ball otherBall = getBall(j);
if (object instanceof Movable) {
for (int j = 0; j < getCount(); j++) {
Regionable other = get(j);

if ((ball != otherBall) && (ball.getBounds().intersects(otherBall.getBounds()))) {
Rectangle itersection = ball.getBounds().intersection(otherBall.getBounds());
if ((object != other) && (object.intersects(other))) {
Region itersection = object.intersection(other);

if (ball.getBounds().getWidth() < otherBall.getBounds().getWidth()) {
if (itersection.getWidth() < ball.getBounds().getWidth()) {
((MovableBall) ball).setDX(-((MovableBall) ball).getDX());
if (object.getWidth() < other.getWidth()) {
if (itersection.getWidth() < object.getWidth()) {
((Movable) object).getMotion().turnDX();
}

if (itersection.getHeight() < ball.getBounds().getHeight()) {
((MovableBall) ball).setDY(-((MovableBall) ball).getDY());
if (itersection.getHeight() < object.getHeight()) {
((Movable) object).getMotion().turnDY();
}

} else {
if (itersection.getWidth() < otherBall.getBounds().getWidth()) {
((MovableBall) ball).setDX(-((MovableBall) ball).getDX());
if (itersection.getWidth() < other.getWidth()) {
((Movable) object).getMotion().turnDX();
}

if (itersection.getHeight() < otherBall.getBounds().getHeight()) {
((MovableBall) ball).setDY(-((MovableBall) ball).getDY());
if (itersection.getHeight() < other.getHeight()) {
((Movable) object).getMotion().turnDY();
}

}
Expand All @@ -60,10 +58,10 @@ public void move() {
}
}

for (int i = 0; i < getBallCount(); i++) {
Ball ball = getBall(i);
if (ball instanceof MovableBall) {
bounceBall((MovableBall) ball);
for (int i = 0; i < getCount(); i++) {
Regionable object = get(i);
if (object instanceof Movable) {
bounce((Movable) object);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions practice/240723/cannonball/src/main/java/com/nhnacademy/Box.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.nhnacademy;

import java.awt.Color;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Box implements Regionable {
public static final Color DEFAULT_COLOR = Color.BLACK;
static Logger log = LogManager.getLogger(Box.class);

Region region;
Color color;

public Box(int x, int y, int width, int height, Color color) {
if ((width < 0) || (height < 0)) {
throw new IllegalArgumentException();
}

this.region = new Region(x, y, width, height);
this.color = color;
}

public Box(int x, int y, int width, int height) {
this(x, y, width, height, DEFAULT_COLOR);
}

public Color getColor() {
return color;
}

public Region getRegion() {
return region;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.nhnacademy;

public class Dimension {
int width;
int height;

public Dimension(int width, int height) {
this.width = width;
this.height = height;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

@Override
public boolean equals(Object o) {
return (o instanceof Dimension)
&& (getWidth() == ((Dimension) o).getWidth())
&& (getHeight() == ((Dimension) o).getHeight());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nhnacademy;

public class DisplacementVector extends Vector {

public DisplacementVector(int angle, int magnitude) {
super((int) (magnitude * Math.cos(Math.toRadians(angle))),
(int) (magnitude * Math.sin(Math.toRadians(angle))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nhnacademy;

import java.awt.Point;

public class Location {
int x;
int y;

public Location(int x, int y) {
this.x = x;
this.y = y;
}

public Location(Location location) {
x = location.getX();
y = location.getY();
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void translate(int dx, int dy) {
x += dx;
y += dy;
}

@Override
public boolean equals(Object o) {
return (o instanceof Location)
&& (getX() == ((Location) o).getX())
&& (getY() == ((Location) o).getY());
}
}
Loading

0 comments on commit b6151df

Please sign in to comment.