Skip to content

Commit 624cb10

Browse files
committed
Polygons...
I'm so done with these weird tasks and polygons.
1 parent e2fb5aa commit 624cb10

8 files changed

Lines changed: 277 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
5+
public interface Actor {
6+
void draw(Graphics g);
7+
8+
void step();
9+
10+
default void handleCollision(Actor other) {
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
5+
public class BlinkingPolygon extends PolygonActor {
6+
private boolean visible = true;
7+
private int count = 0;
8+
9+
public BlinkingPolygon(Polygon shape, Color color) {
10+
super(shape, color);
11+
}
12+
13+
@Override
14+
public void draw(Graphics g) {
15+
if (visible) {
16+
super.draw(g);
17+
}
18+
}
19+
20+
@Override
21+
public void step() {
22+
count++;
23+
if (count >= 10) {
24+
visible = !visible;
25+
count = 0;
26+
}
27+
}
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
import java.util.*;
5+
import javax.swing.*;
6+
7+
public class Drawing extends Canvas {
8+
private java.util.List<Actor> actors = new ArrayList<>();
9+
10+
public void addActor(Actor actor) {
11+
actors.add(actor);
12+
}
13+
14+
public java.util.List<Actor> getActors() {
15+
return actors;
16+
}
17+
18+
@Override
19+
public void paint(Graphics g) {
20+
for (Actor actor : actors) {
21+
actor.draw(g);
22+
}
23+
}
24+
25+
public void step() {
26+
for (Actor actor : actors) {
27+
actor.step();
28+
}
29+
detectCollisions();
30+
repaint();
31+
}
32+
33+
private void detectCollisions() {
34+
for (int i = 0; i < actors.size(); i++) {
35+
for (int j = i + 1; j < actors.size(); j++) {
36+
Actor a = actors.get(i);
37+
Actor b = actors.get(j);
38+
39+
if (a instanceof PolygonActor pa && b instanceof PolygonActor pb) {
40+
if (pa.shape.getBounds().intersects(pb.shape.getBounds())) {
41+
a.handleCollision(b);
42+
b.handleCollision(a);
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
import java.awt.event.*;
5+
6+
public class MouseActor implements Actor, MouseListener {
7+
private int x = 0, y = 0;
8+
private boolean clicked = false;
9+
10+
@Override
11+
public void draw(Graphics g) {
12+
g.setColor(clicked ? Color.RED : Color.BLUE);
13+
g.fillOval(x - 15, y - 15, 30, 30);
14+
}
15+
16+
@Override
17+
public void step() {
18+
}
19+
20+
@Override
21+
public void mouseClicked(MouseEvent e) {
22+
x = e.getX();
23+
y = e.getY();
24+
clicked = true;
25+
}
26+
27+
@Override
28+
public void mousePressed(MouseEvent e) {
29+
}
30+
31+
@Override
32+
public void mouseReleased(MouseEvent e) {
33+
}
34+
35+
@Override
36+
public void mouseEntered(MouseEvent e) {
37+
}
38+
39+
@Override
40+
public void mouseExited(MouseEvent e) {
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
5+
public class MovingPolygon extends PolygonActor {
6+
private int boundsWidth, boundsHeight;
7+
8+
public MovingPolygon(Polygon shape, Color color, int boundsWidth, int boundsHeight) {
9+
super(shape, color);
10+
this.boundsWidth = boundsWidth;
11+
this.boundsHeight = boundsHeight;
12+
}
13+
14+
@Override
15+
public void step() {
16+
super.step();
17+
Rectangle bounds = shape.getBounds();
18+
19+
if (bounds.x < 0 || bounds.x + bounds.width > boundsWidth) {
20+
dx = -dx;
21+
}
22+
if (bounds.y < 0 || bounds.y + bounds.height > boundsHeight) {
23+
dy = -dy;
24+
}
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
5+
public class PolygonActor implements Actor {
6+
protected Polygon shape;
7+
protected Color color;
8+
protected int dx = 0, dy = 0;
9+
10+
public PolygonActor(Polygon shape, Color color) {
11+
this.shape = shape;
12+
this.color = color;
13+
}
14+
15+
public void setVelocity(int dx, int dy) {
16+
this.dx = dx;
17+
this.dy = dy;
18+
}
19+
20+
@Override
21+
public void draw(Graphics g) {
22+
g.setColor(color);
23+
g.fillPolygon(shape);
24+
}
25+
26+
@Override
27+
public void step() {
28+
shape.translate(dx, dy);
29+
}
30+
31+
@Override
32+
public void handleCollision(Actor other) {
33+
dx = -dx;
34+
dy = -dy;
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
import java.awt.event.*;
5+
6+
public class Sprite implements Actor, KeyListener {
7+
private int x, y, dx = 0, dy = 0;
8+
private Image image;
9+
10+
public Sprite(String imagePath, int x, int y) {
11+
this.x = x;
12+
this.y = y;
13+
try {
14+
image = Toolkit.getDefaultToolkit().getImage(imagePath);
15+
} catch (Exception e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
@Override
21+
public void draw(Graphics g) {
22+
g.drawImage(image, x, y, null);
23+
}
24+
25+
@Override
26+
public void step() {
27+
x += dx;
28+
y += dy;
29+
}
30+
31+
@Override
32+
public void keyPressed(KeyEvent e) {
33+
switch (e.getKeyCode()) {
34+
case KeyEvent.VK_UP -> dy = -5;
35+
case KeyEvent.VK_DOWN -> dy = 5;
36+
case KeyEvent.VK_LEFT -> dx = -5;
37+
case KeyEvent.VK_RIGHT -> dx = 5;
38+
}
39+
}
40+
41+
@Override
42+
public void keyReleased(KeyEvent e) {
43+
switch (e.getKeyCode()) {
44+
case KeyEvent.VK_UP, KeyEvent.VK_DOWN -> dy = 0;
45+
case KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT -> dx = 0;
46+
}
47+
}
48+
49+
@Override
50+
public void keyTyped(KeyEvent e) {
51+
}
52+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package chapter17exercises;
2+
3+
import java.awt.*;
4+
import javax.swing.*;
5+
6+
public class VideoGame {
7+
public static void main(String[] args) {
8+
JFrame frame = new JFrame("Video Game");
9+
Drawing drawing = new Drawing();
10+
11+
Polygon triangle = new Polygon(new int[] { 0, 50, 25 }, new int[] { 0, 0, 50 }, 3);
12+
Polygon hexagon = new Polygon(new int[] { 0, 40, 80, 80, 40, 0 }, new int[] { 40, 0, 40, 80, 120, 80 }, 6);
13+
14+
MovingPolygon movingPolygon = new MovingPolygon(triangle, Color.RED, 800, 600);
15+
movingPolygon.setVelocity(3, 2);
16+
17+
BlinkingPolygon blinkingPolygon = new BlinkingPolygon(hexagon, Color.GREEN);
18+
Sprite sprite = new Sprite("face-smile.png", 100, 100);
19+
20+
drawing.addActor(movingPolygon);
21+
drawing.addActor(blinkingPolygon);
22+
drawing.addActor(sprite);
23+
24+
frame.addKeyListener(sprite);
25+
frame.add(drawing);
26+
frame.setSize(800, 600);
27+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28+
frame.setVisible(true);
29+
30+
Timer timer = new Timer(33, e -> drawing.step());
31+
timer.start();
32+
}
33+
}

0 commit comments

Comments
 (0)