Code:
import java.awt.Color;
import java.awt.Graphics;
public class Wuerfel {
private static class D3Vector {
private double[] r;
private final int X = 0;
private final int Y = 1;
private final int Z = 2;
public D3Vector(double x, double y, double z) {
this.r = new double[3];
this.r[this.X] = x;
this.r[this.Y] = y;
this.r[this.Z] = z;
}
public double getLength() {
return (Math
.sqrt(Math.pow(this.r[this.X], 2)
+ Math.pow(this.r[this.Y], 2)
+ Math.pow(this.r[this.Z], 2)));
}
public double getX() {
return this.r[this.X];
}
public double getY() {
return this.r[this.Y];
}
public double getZ() {
return this.r[this.Z];
}
public static int[] D3ToD2(int x, int y, int z) {
int[] ret = new int[2];
ret[0] = y;
ret[1] = z;
return ret;
}
}
private int a;
private int[] pos;
private int[] vanishingPoint;
private D3Vector[] vectors;
public Wuerfel(int a, int[] pos, int[] vanishingPoint) {
this.a = a;
this.pos = pos;
this.vanishingPoint = vanishingPoint;
this.vectors = new D3Vector[6];
this.createVectors();
}
private void createVectors() {
// Waagerecht und horizontal sind trivial
this.vectors[0] = new D3Vector(0, this.a, 0);
this.vectors[1] = new D3Vector(0, 0, this.a);
// Schräg
int vX = this.vanishingPoint[0] - this.pos[0];
int vY = this.vanishingPoint[1] - this.pos[1];
int vZ = this.pos[2] - this.vanishingPoint[2];
this.vectors[2] = new D3Vector(vX, vY, vZ);
vX = this.vanishingPoint[0] - this.pos[0];
vY = this.vanishingPoint[1] - (this.pos[1] + this.a);
vZ = this.pos[2] - this.vanishingPoint[2];
this.vectors[3] = new D3Vector(vX, vY, vZ);
vX = this.vanishingPoint[0] - this.pos[0];
vY = this.vanishingPoint[1] - this.pos[1];
vZ = (this.pos[2] - this.a) - this.vanishingPoint[2];
this.vectors[4] = new D3Vector(vX, vY, vZ);
vX = this.vanishingPoint[0] - this.pos[0];
vY = this.vanishingPoint[1] - (this.pos[1] + this.a);
vZ = (this.pos[2] - this.a) - this.vanishingPoint[2];
this.vectors[5] = new D3Vector(vX, vY, vZ);
}
private void drawVector(Graphics g, D3Vector vector, D3Vector pos) {
int[] start = D3Vector.D3ToD2((int) Math.round(pos.getX()),
(int) Math.round(pos.getY()), (int) Math.round(pos.getZ()));
int[] end = D3Vector.D3ToD2((int) Math.round(vector.getX()),
(int) Math.round(vector.getY()),
(int) Math.round(vector.getZ()));
g.drawLine(start[0], start[1], start[0] + end[0], start[1] - end[1]);
}
public void drawCube(Graphics g) {
int[] vpPos = D3Vector.D3ToD2(this.vanishingPoint[0],
this.vanishingPoint[1], this.vanishingPoint[2]);
g.setColor(Color.black);
g.fillOval(vpPos[0] - 6, vpPos[1] - 6, 10, 10);
g.drawString("VP", vpPos[0] + 10, vpPos[1] - 10);
this.drawVector(g, this.vectors[0], new D3Vector(this.pos[0],
this.pos[1], this.pos[2]));
this.drawVector(g, this.vectors[0], new D3Vector(this.pos[0],
this.pos[1], this.pos[2] - this.a));
this.drawVector(g, this.vectors[1], new D3Vector(this.pos[0],
this.pos[1], this.pos[2]));
this.drawVector(g, this.vectors[1], new D3Vector(this.pos[0],
this.pos[1] + this.a, this.pos[2]));
this.drawVector(g, this.vectors[2], new D3Vector(this.pos[0],
this.pos[1], this.pos[2]));
this.drawVector(g, this.vectors[3], new D3Vector(this.pos[0],
this.pos[1] + this.a, this.pos[2]));
this.drawVector(g, this.vectors[4], new D3Vector(this.pos[0],
this.pos[1], this.pos[2] - this.a));
this.drawVector(g, this.vectors[5], new D3Vector(this.pos[0],
this.pos[1] + this.a, this.pos[2] - this.a));
}
public void setVanishingPoint(int[] vanishingPoint) {
this.vanishingPoint = vanishingPoint;
this.createVectors();
}
}
Code:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JApplet;
public class DreiD extends JApplet implements MouseListener {
private Wuerfel wuerfel;
@Override
public void init() {
int[] pos = { 0, 250, 250 };
int[] vanishingPoint = { 20, 500, 100 };
this.wuerfel = new Wuerfel(80, pos, vanishingPoint);
this.setSize(600, 400);
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
this.wuerfel.drawCube(g);
}
@Override
public void mouseClicked(MouseEvent e) {
int[] vanishingPoint = { 0, e.getX(), e.getY() };
this.wuerfel.setVanishingPoint(vanishingPoint);
this.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
}
}