Hallo Leute,
ich bin im Moment dran ein xyz- Koordinatesystem zu zeichnen, in dem 3 Dreiecke entlang der Achsen aufgespannt sind.
Ich kann entlang der X, Y und Z Achsen ohne Problem rotieren.
Nur will ich jetzt noch eine Ansichtstranformation einbauen, die es mir erlaubt, die Szene von anderen Betrachtungswinkeln anzuschauen.
Im moment sieht man nur meine x und y Achsen.
Hier mal mein Zeichenflaechen Code. Falls jemand den vollständigen Code will, kann er mir ja seine Email Adresse geben und ich schick demjenigen alles zu
Danke im Vorraus auf eure Antworten
ich bin im Moment dran ein xyz- Koordinatesystem zu zeichnen, in dem 3 Dreiecke entlang der Achsen aufgespannt sind.
Ich kann entlang der X, Y und Z Achsen ohne Problem rotieren.
Nur will ich jetzt noch eine Ansichtstranformation einbauen, die es mir erlaubt, die Szene von anderen Betrachtungswinkeln anzuschauen.
Im moment sieht man nur meine x und y Achsen.
Hier mal mein Zeichenflaechen Code. Falls jemand den vollständigen Code will, kann er mir ja seine Email Adresse geben und ich schick demjenigen alles zu
Danke im Vorraus auf eure Antworten
Java:
package Koordinatensystem3D;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JPanel;
import LED.Matrix;
public class Zeichenflaeche3D extends JPanel implements MouseListener{
Fenster3D parent;
double[][] ansichtstransformation = {{0.71,0.71,0,0},{-0.5,0.5,0.71,0},{0.5,0.5,0.71,0},{0,0,0,1}};
double[][] viewport = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
double[][] faktor = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
double[][] alpha = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
double[][] beta = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
double[][] gamma = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
double[][] mxzx = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
double[][] megaMatrix = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
double triangleSize = 100;
ArrayList<double[][]> arr = new ArrayList<>();
public Zeichenflaeche3D(Fenster3D parent){
this.parent = parent;
this.addMouseListener(this);
initArrayList();
}
public void initArrayList(){
if(arr != null){
for(int i = 0; i < arr.size(); i++){
arr.remove(i);
}
}
arr.add(ansichtstransformation);
arr.add(viewport);
arr.add(faktor);
arr.add(alpha);
arr.add(beta);
arr.add(gamma);
arr.add(mxzx);
}
//Paint Methods-----------------------------------//
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
setViewport();
paintKoordinatensystem((Graphics2D) g);
drawTriangles((Graphics2D) g);
}
public void paintKoordinatensystem(Graphics2D g2d){
//PixelCoordinates
double[] CoordXEnd = {-this.getWidth()/2,0,0,1};
double[] CoordXBegin = {this.getWidth()/2,0,0,1};
double[] CoordYEnd = {0,-this.getHeight()/2,0,1};
double[] CoordYBegin = {0,this.getHeight()/2,0,1};
double[] CoordZEnd = {0,0,0,1};
double[] CoordZBegin = {0,0,0,1};
//Calculate new Origin (Viewport Matrix) e.g by scaling Window Size
double[] newCoordXEnd = Matrix.matMult(viewport, CoordXEnd);
double[] newCoordXBegin = Matrix.matMult(viewport, CoordXBegin);
double[] newCoordYEnd = Matrix.matMult(viewport, CoordYEnd);
double[] newCoordYBegin = Matrix.matMult(viewport, CoordYBegin);
double[] newCoordZEnd = Matrix.matMult(viewport, CoordZEnd);
double[] newCoordZBegin = Matrix.matMult(viewport, CoordZBegin);
//Drawing Coordinate System with new Origin
g2d.setStroke(new BasicStroke(0.4f));
g2d.setColor(Color.BLACK);
g2d.drawLine((int)newCoordXEnd[0],(int)newCoordXEnd[1], (int)newCoordXBegin[0], (int)newCoordXBegin[1]);
g2d.setColor(Color.RED);
g2d.drawLine((int)newCoordYEnd[0],(int)newCoordYEnd[1], (int)newCoordYBegin[0], (int)newCoordYBegin[1]);
g2d.setColor(Color.BLUE);
g2d.drawLine((int)newCoordZEnd[0],(int)newCoordZEnd[1], (int)newCoordZBegin[0], (int)newCoordZBegin[1]);
}
public void drawTriangles(Graphics2D g2d){
double[][] vertices = {{triangleSize,0,0,0},
{0,triangleSize,0,0},
{0,0,triangleSize,0},
{1,1,1,1}};
vertices = Matrix.matMult(alpha, vertices);
vertices = Matrix.matMult(beta, vertices);
vertices = Matrix.matMult(gamma, vertices);
double[] vertice1 = {vertices[0][0], vertices[1][0], vertices[2][0], vertices[3][0]};
double[] vertice2 = {vertices[0][1], vertices[1][1], vertices[2][1], vertices[3][1]};
double[] vertice3 = {vertices[0][2], vertices[1][2], vertices[2][2], vertices[3][2]};
double[] vertice4 = {vertices[0][3], vertices[1][3], vertices[2][3], vertices[3][3]};
double[] triangle1Vertex1 = vertice1;
double[] triangle1Vertex2 = vertice2;
double[] triangle1Vertex3 = vertice4;
double[] triangle2Vertex1 = vertice1;
double[] triangle2Vertex2 = vertice3;
double[] triangle2Vertex3 = vertice4;
double[] triangle3Vertex1 = vertice3;
double[] triangle3Vertex2 = vertice2;
double[] triangle3Vertex3 = vertice4;
//Calculate new Origin (Viewport Matrix) e.g by scaling Window Size
double[] newTriangle1Vertex1 = Matrix.matMult(viewport, triangle1Vertex1);
double[] newTriangle1Vertex2 = Matrix.matMult(viewport, triangle1Vertex2);
double[] newTriangle1Vertex3 = Matrix.matMult(viewport, triangle1Vertex3);
double[] newTriangle2Vertex1 = Matrix.matMult(viewport, triangle2Vertex1);
double[] newTriangle2Vertex2 = Matrix.matMult(viewport, triangle2Vertex2);
double[] newTriangle2Vertex3 = Matrix.matMult(viewport, triangle2Vertex3);
double[] newTriangle3Vertex1 = Matrix.matMult(viewport, triangle3Vertex1);
double[] newTriangle3Vertex2 = Matrix.matMult(viewport, triangle3Vertex2);
double[] newTriangle3Vertex3 = Matrix.matMult(viewport, triangle3Vertex3);
g2d.setStroke(new BasicStroke(3.0f));
g2d.setColor(Color.BLUE);
drawTriangle(g2d, newTriangle1Vertex1, newTriangle1Vertex2, newTriangle1Vertex3);
g2d.setColor(Color.RED);
drawTriangle(g2d, newTriangle2Vertex1, newTriangle2Vertex2, newTriangle2Vertex3);
g2d.setColor(Color.GREEN);
drawTriangle(g2d, newTriangle3Vertex1, newTriangle3Vertex2, newTriangle3Vertex3);
}
public void drawTriangle(Graphics2D g2d, double[] vertex1, double[] vertex2, double[] vertex3){
g2d.drawLine((int)vertex1[0], (int)vertex1[1], (int)vertex2[0], (int)vertex2[1]);
g2d.drawLine((int)vertex2[0], (int)vertex2[1], (int)vertex3[0], (int)vertex3[1]);
g2d.drawLine((int)vertex3[0], (int)vertex3[1], (int)vertex1[0], (int)vertex1[1]);
}
//--------------------------------------------------//
//Getters and Setters------------------------------//
public void setViewport(){
double[][] viewport2 = {{1,0,0,this.getWidth()/2},{0,-1,0,this.getHeight()/2},{0,0,0,0},{0,0,0,1}};
viewport = viewport2;
arr.set(1, viewport);
parent.mp.initLabels();
}
public double getTriangleSize() {
return triangleSize;
}
public void setTriangleSize(int triangleSize) {
this.triangleSize = triangleSize;
}
//--------------------------------------------------//
//MouseListener Methods-----------------------------//
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(e.getX() + " " + e.getY());
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
//--------------------------------------------------//
}