Skip to content

Commit

Permalink
Source code for 3D tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
javatutorials101 committed Jun 4, 2015
1 parent d74d226 commit 265dfa0
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 0 deletions.
39 changes: 39 additions & 0 deletions 3D - Tutorial 10/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

public class Calculator {
static double DrawX = 0, DrawY = 0;
static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
setStuff(ViewFrom, ViewTo, x, y, z);
return DrawX;
}

static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
setStuff(ViewFrom, ViewTo, x, y, z);
return DrawY;
}

static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]);
Vector DirectionVector = new Vector(1, 1, 1);
Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector);
Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1);

Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]);

double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2]
- (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2]))
/ (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z);

x = ViewFrom[0] + ViewToPoint.x * t;
y = ViewFrom[1] + ViewToPoint.y * t;
z = ViewFrom[2] + ViewToPoint.z * t;

if(t > 0)
{
DrawX = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z;
DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z;
}
}
}
20 changes: 20 additions & 0 deletions 3D - Tutorial 10/DDDTutorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.awt.Toolkit;
import javax.swing.JFrame;

public class DDDTutorial extends JFrame{
static JFrame F = new DDDTutorial();
Screen ScreenObject = new Screen();

public DDDTutorial()
{
add(ScreenObject);
setUndecorated(true);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setVisible(true);
}

public static void main(String[] args)
{

}
}
49 changes: 49 additions & 0 deletions 3D - Tutorial 10/DPolygon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.awt.Color;
import java.awt.Polygon;


public class DPolygon {
Color c;
double[] x, y, z;
int poly = 0;

public DPolygon(double[] x, double[] y, double[] z, Color c)
{
Screen.NumberOf3DPolygons++;
this.x = x;
this.y = y;
this.z = z;
this.c = c;
createPolygon();
}

void createPolygon()
{
double[] newX = new double[x.length];
double [] newY = new double[x.length];

for(int i = 0; i < x.length; i++)
{
newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
}

poly = Screen.NumberOfPolygons;
Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c);
}

void updatePolygon()
{
double[] newX = new double[x.length];
double [] newY = new double[x.length];

for(int i = 0; i < x.length; i++)
{
newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
}

Screen.DrawablePolygons[poly] = new PolygonObject(newX, newY, c);
Screen.NumberOfPolygons --;
}
}
24 changes: 24 additions & 0 deletions 3D - Tutorial 10/PolygonObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;


public class PolygonObject {
Polygon P;
Color c;

public PolygonObject(double[] x, double[] y, Color c)
{
Screen.NumberOfPolygons++;
P = new Polygon();
for(int i = 0; i < x.length; i++)
P.addPoint((int)x[i], (int)y[i]);
this.c = c;
}

void drawPolygon(Graphics g)
{
g.setColor(c);
g.fillPolygon(P);
}
}
77 changes: 77 additions & 0 deletions 3D - Tutorial 10/Screen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Screen extends JPanel implements KeyListener{
double SleepTime = 1000/30, lastRefresh = 0;
static double[] ViewFrom = new double[] {10, 10, 10};
static double[] ViewTo = new double[] {5, 0, 0};
static int NumberOfPolygons = 0, NumberOf3DPolygons = 0;
static PolygonObject[] DrawablePolygons = new PolygonObject[100];
static DPolygon[] DPolygons = new DPolygon[100];

public Screen()
{
addKeyListener(this);
setFocusable(true);
DPolygons[0] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{0, 0, 0, 0}, Color.black);
DPolygons[1] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{3, 3, 3, 3 }, Color.black);
}

public void paintComponent(Graphics g)
{
g.clearRect(0, 0, 2000, 1200);
g.drawString(System.currentTimeMillis() + "", 20, 20);

for(int i = 0; i < NumberOf3DPolygons; i++)
DPolygons[i].updatePolygon();

for(int i = 0; i < NumberOfPolygons; i++)
DrawablePolygons[i].drawPolygon(g);
SleepAndRefresh();
}

void SleepAndRefresh()
{
while(true)
{
if(System.currentTimeMillis() - lastRefresh > SleepTime)
{
lastRefresh = System.currentTimeMillis();
repaint();
break;
}
else
{
try
{
Thread.sleep((long)(System.currentTimeMillis() - lastRefresh));
}
catch (Exception e)
{

}
}
}
}

public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT)
ViewFrom[0] --;
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
ViewFrom[0] ++;
if(e.getKeyCode() == KeyEvent.VK_UP)
ViewFrom[1] --;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
ViewFrom[0] ++;
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}
}
23 changes: 23 additions & 0 deletions 3D - Tutorial 10/Vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

public class Vector {
double x = 0, y = 0, z = 0;
public Vector(double x, double y, double z)
{
double length = Math.sqrt(x * x + y * y + z * z);
if(length>0)
{
this.x = x/length;
this.y = y/length;
this.z = z/length;
}
}

Vector CrossProduct(Vector V)
{
Vector CrossVector = new Vector(
y * V.z - z * V.y,
z * V.x - x * V.z,
x * V.y - y * V.x);
return CrossVector;
}
}
39 changes: 39 additions & 0 deletions 3D - Tutorial 9/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

public class Calculator {
static double DrawX = 0, DrawY = 0;
static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
setStuff(ViewFrom, ViewTo, x, y, z);
return DrawX;
}

static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
setStuff(ViewFrom, ViewTo, x, y, z);
return DrawY;
}

static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]);
Vector DirectionVector = new Vector(1, 1, 1);
Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector);
Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1);

Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]);

double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2]
- (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2]))
/ (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z);

x = ViewFrom[0] + ViewToPoint.x * t;
y = ViewFrom[1] + ViewToPoint.y * t;
z = ViewFrom[2] + ViewToPoint.z * t;

if(t > 0)
{
DrawX = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z;
DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z;
}
}
}
20 changes: 20 additions & 0 deletions 3D - Tutorial 9/DDDTutorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.awt.Toolkit;
import javax.swing.JFrame;

public class DDDTutorial extends JFrame{
static JFrame F = new DDDTutorial();
Screen ScreenObject = new Screen();

public DDDTutorial()
{
add(ScreenObject);
setUndecorated(true);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setVisible(true);
}

public static void main(String[] args)
{

}
}
31 changes: 31 additions & 0 deletions 3D - Tutorial 9/DPolygon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.awt.Color;
import java.awt.Polygon;


public class DPolygon {
Color c;
double[] x, y, z;

public DPolygon(double[] x, double[] y, double[] z, Color c)
{
this.x = x;
this.y = y;
this.z = z;
this.c = c;
createPolygon();
}

void createPolygon()
{
double[] newX = new double[x.length];
double [] newY = new double[x.length];

for(int i = 0; i < x.length; i++)
{
newX[i] = 200 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
newY[i] = 200 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
}

Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c);
}
}
24 changes: 24 additions & 0 deletions 3D - Tutorial 9/PolygonObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;


public class PolygonObject {
Polygon P;
Color c;

public PolygonObject(double[] x, double[] y, Color c)
{
Screen.NumberOfPolygons++;
P = new Polygon();
for(int i = 0; i < x.length; i++)
P.addPoint((int)x[i], (int)y[i]);
this.c = c;
}

void drawPolygon(Graphics g)
{
g.setColor(c);
g.fillPolygon(P);
}
}
Loading

0 comments on commit 265dfa0

Please sign in to comment.