Real rename

This commit is contained in:
joachimschmidt557 2019-02-12 19:22:33 +01:00
parent aa3dd87076
commit cafb36cb26
68 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,85 @@
package dice3d.main;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import dice3d.models.cuboids.Cuboid;
import dice3d.models.cuboids.Dice;
public class MainWindow extends JPanel {
World w = new World();
public MainWindow() {
addKeyListener(new KeyHandler());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
draw((Graphics2D) g);
}
public void draw(Graphics2D g) {
for(Cuboid c : w.cuboids) {
c.draw(g);
}
int yOffset = 0;
g.drawString("Press space key to roll again", 10, yOffset =+ 20);
for(int i = 0; i < w.cuboids.size(); i++) {
Cuboid c = w.cuboids.get(i);
if(c instanceof Dice) {
Dice d = (Dice) c;
g.drawString("dice[" + i + "]: moving=" + !d.notMoving() + " number_rolled=" + d.getNumberRolled(), 10, yOffset += 20);
} else {
g.drawString("cuboid[" + i + "]: moving=" + !c.notMoving(), 10, yOffset += 20);
}
}
repaint();
}
private class KeyHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if (e.getID() != KeyEvent.KEY_PRESSED) {
return;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
for(Cuboid c : w.cuboids) {
c.reset();
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainWindow view = new MainWindow();
JFrame frame = new JFrame();
frame.setTitle("Dice");
frame.getContentPane().add(view);
frame.setSize(1280, 720);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setResizable(false);
frame.setVisible(true);
view.requestFocus();
}
});
}
}

View file

@ -0,0 +1,43 @@
package dice3d.main;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import dice3d.models.cuboids.Cuboid;
import dice3d.models.cuboids.Dice;
public class World {
public static final double projectionDistance = 1000;
public ArrayList<Cuboid> cuboids;
public Cuboid floor;
public World() {
cuboids = new ArrayList<Cuboid>();
floor = new Cuboid(10, 400, 600, 700, 1000, 10);
cuboids.add(floor);
cuboids.add(new Dice(150, 100, 800, 80));
cuboids.add(new Dice(50, 120, 800, 80));
long delayInMS = 500; // start updating after 500ms
long intervalInMS = 15; // update every 15ms
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
for(Cuboid c : cuboids) {
if(c == floor) {
continue;
}
c.update();
}
}
}, delayInMS, intervalInMS);
}
}

View file

@ -0,0 +1,75 @@
package dice3d.math;
public class Vector {
public double x;
public double y;
public double z;
public Vector() {
}
public Vector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vector(Vector v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public void set(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void set(Vector v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public void add(Vector v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
}
public void sub(Vector v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
}
public void scale(double s) {
scale(s, s, s);
}
public void scale(double sx, double sy, double sz) {
this.x *= sx;
this.y *= sy;
this.z *= sz;
}
public double getSize() {
return Math.sqrt(x * x + y * y + z * z);
}
public void normalize() {
scale(1 / getSize());
}
@Override
public String toString() {
return "[" + x + ", " + y + ", " + z + "]";
}
public boolean equals(Vector v) {
return v.x == x && v.y == y && v.z == z;
}
}

View file

@ -0,0 +1,31 @@
package dice3d.models;
import java.awt.Graphics2D;
import dice3d.main.World;
public class Edge {
private Vertex a;
private Vertex b;
public Edge(Vertex a, Vertex b) {
this.a = a;
this.b = b;
}
public void update() {
}
public void draw(Graphics2D g) {
double ax = World.projectionDistance * a.position.x / a.position.z;
double ay = World.projectionDistance * a.position.y / a.position.z;
double bx = World.projectionDistance * b.position.x / b.position.z;
double by = World.projectionDistance * b.position.y / b.position.z;
g.drawLine((int) ax, (int) ay, (int) bx, (int) by);
}
}

View file

@ -0,0 +1,40 @@
package dice3d.models;
import java.awt.Graphics2D;
import dice3d.main.World;
import dice3d.math.Vector;
public class Vertex {
public Vector position = new Vector();
public Vector positionReset = new Vector();
public String id;
public Vertex(double x, double y, double z) {
position.set(x, y, z);
positionReset.set(position);
}
public void reset() {
position.set(positionReset);
}
public void update() {
}
public void draw(Graphics2D g) {
double px = World.projectionDistance * position.x / position.z;
double py = World.projectionDistance * position.y / position.z;
if (id == null) {
int size = 6;
g.fillOval((int) (px - (size / 2)), (int) (py - (size / 2)), size, size);
} else {
g.drawString(id, (int)px, (int)py);
}
}
}

View file

@ -0,0 +1,9 @@
package dice3d.models.cuboids;
public class Cube extends Cuboid {
public Cube(double x, double y, double z, int size) {
super(x, y, z, size, size, size);
}
}

View file

@ -0,0 +1,147 @@
package dice3d.models.cuboids;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import dice3d.math.Vector;
import dice3d.models.Edge;
import dice3d.models.Vertex;
public class Cuboid {
public ArrayList<Vertex> vertices = new ArrayList<Vertex>();
public ArrayList<Edge> edges = new ArrayList<Edge>();
public boolean collided = false;
public Cuboid(double x, double y, double z, int length, int width, int height) {
Vector corner = new Vector(x, y, z);
Vertex a = new Vertex(corner.x, corner.y, corner.z);
Vertex b = new Vertex(corner.x + length, corner.y, corner.z);
Vertex c = new Vertex(corner.x + length, corner.y + height, corner.z);
Vertex d = new Vertex(corner.x, corner.y + height, corner.z);
Vertex e = new Vertex(corner.x, corner.y, corner.z + width);
Vertex f = new Vertex(corner.x + length, corner.y, corner.z + width);
Vertex g = new Vertex(corner.x + length, corner.y + height, corner.z + width);
Vertex h = new Vertex(corner.x, corner.y + height, corner.z + width);
a.id = "A";
b.id = "B";
c.id = "C";
d.id = "D";
e.id = "E";
f.id = "F";
g.id = "G";
h.id = "H";
vertices.add(a);
vertices.add(b);
vertices.add(c);
vertices.add(d);
vertices.add(e);
vertices.add(f);
vertices.add(g);
vertices.add(h);
Edge edge01 = new Edge(a, b);
Edge edge02 = new Edge(b, c);
Edge edge03 = new Edge(c, d);
Edge edge04 = new Edge(d, a);
Edge edge05 = new Edge(e, f);
Edge edge06 = new Edge(f, g);
Edge edge07 = new Edge(g, h);
Edge edge08 = new Edge(h, e);
Edge edge09 = new Edge(a, e);
Edge edge10 = new Edge(b, f);
Edge edge11 = new Edge(c, g);
Edge edge12 = new Edge(d, h);
Edge edge13 = new Edge(a, g);
Edge edge14 = new Edge(b, h);
Edge edge15 = new Edge(c, e);
Edge edge16 = new Edge(d, f);
Edge edge17 = new Edge(a, c);
Edge edge18 = new Edge(b, g);
Edge edge19 = new Edge(f, h);
Edge edge20 = new Edge(d, e);
Edge edge21 = new Edge(d, g);
Edge edge22 = new Edge(a, f);
edges.add(edge01);
edges.add(edge02);
edges.add(edge03);
edges.add(edge04);
edges.add(edge05);
edges.add(edge06);
edges.add(edge07);
edges.add(edge08);
edges.add(edge09);
edges.add(edge10);
edges.add(edge11);
edges.add(edge12);
edges.add(edge13);
edges.add(edge14);
edges.add(edge15);
edges.add(edge16);
edges.add(edge17);
edges.add(edge18);
edges.add(edge19);
edges.add(edge20);
edges.add(edge21);
edges.add(edge22);
}
public void update() {
for (Vertex v : vertices) {
v.update();
}
for (Edge e : edges) {
e.update();
}
}
public void reset() {
for (Vertex v : vertices) {
v.reset();
}
}
public void draw(Graphics2D g) {
// draw all edges, even inner ones
g.setColor(Color.GREEN);
if(collided) {
g.setColor(Color.RED);
}
for(Edge e: edges) {
e.draw(g);
}
g.setColor(Color.BLACK);
for (Vertex point : vertices) {
point.draw(g);
}
}
public boolean notMoving() {
return false;
}
public void updateCollision(Cuboid d) {
collided = false;
}
}

View file

@ -0,0 +1,111 @@
package dice3d.models.cuboids;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import dice3d.main.World;
import dice3d.models.Vertex;
public class Dice extends Cube {
private Vertex[][] face = new Vertex[6][4];
public Dice(double x, double y, double z, int size) {
super(x,y,z, size);
face[0][0] = vertices.get(0); // a
face[0][1] = vertices.get(1); // b
face[0][2] = vertices.get(2); // c ...
face[0][3] = vertices.get(3);
face[1][0] = vertices.get(1);
face[1][1] = vertices.get(5);
face[1][2] = vertices.get(6);
face[1][3] = vertices.get(2);
face[2][0] = vertices.get(5);
face[2][1] = vertices.get(4);
face[2][2] = vertices.get(7);
face[2][3] = vertices.get(6);
face[3][0] = vertices.get(4);
face[3][1] = vertices.get(0);
face[3][2] = vertices.get(3);
face[3][3] = vertices.get(7);
face[4][0] = vertices.get(3);
face[4][1] = vertices.get(2);
face[4][2] = vertices.get(6);
face[4][3] = vertices.get(7);
face[5][0] = vertices.get(1);
face[5][1] = vertices.get(0);
face[5][2] = vertices.get(4);
face[5][3] = vertices.get(5);
}
public void draw(Graphics2D g) {
drawDice(g);
// draw all edges, even inner ones
// for(Edge e: edges) {
// e.draw(g);
// }
// for (Vertex point : vertices) {
// point.draw(g);
// }
}
public int getNumberRolled() {
return 1;
}
private void drawDice(Graphics2D g) {
for (int f = 0; f < 6; f++) {
// each face as polygon
Polygon polygon = new Polygon();
for (int v = 0; v < 4; v++) {
Vertex vertex = face[f][v];
double vx = World.projectionDistance * vertex.position.x / vertex.position.z;
double vy = World.projectionDistance * vertex.position.y / vertex.position.z;
polygon.addPoint((int) vx, (int) vy);
}
// back-face culling
double v1x = polygon.xpoints[0] - polygon.xpoints[1];
double v1y = polygon.ypoints[0] - polygon.ypoints[1];
double v2x = polygon.xpoints[2] - polygon.xpoints[1];
double v2y = polygon.ypoints[2] - polygon.ypoints[1];
double visible = v1x * v2y - v1y * v2x;
if (visible < 0) {
g.setColor(Color.WHITE);
if(collided) {
g.setColor(Color.RED);
}
g.fill(polygon);
g.setColor(Color.BLACK);
g.draw(polygon);
// draw number of each face (that is visible)
// calc center of each face/square
double xs = 0d;
double ys = 0d;
for (int i = 0; i < 4; i++) {
xs += polygon.xpoints[i];
ys += polygon.ypoints[i];
}
xs = xs / 4;
ys = ys / 4;
g.drawString(f + 1 + "", (int) xs, (int) ys);
}
}
}
}