Hallo, ich will einen Würfel drehen und hab nun angefangen erst mal ein Quadrat im Raum zu zeichnen.
Leider weiß ich nicht ob das so stimmt und vor allem wie ich View und Cam am besten setze.
Es geht hier nicht um kluge java-Programmierung sonder um die Umrechnung von 3D nach 2D und die Drehmatrizen.
Leider weiß ich nicht ob das so stimmt und vor allem wie ich View und Cam am besten setze.
Es geht hier nicht um kluge java-Programmierung sonder um die Umrechnung von 3D nach 2D und die Drehmatrizen.
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Hauptklasse.java
*
* Created on 25.06.2010, 17:09:22
*/
package drehwuerfel;
import java.awt.Graphics;
/**
*
* @author Weiti
*/
public class Hauptklasse extends javax.swing.JFrame {
/** Creates new form Hauptklasse */
public Hauptklasse() {
initComponents();
}
double s=5;
// double faktor = 1;
// double Bildschirmmitte_x = 300;
// double Bildschirmmitte_y = 300;
final double[] Cam = {300,300,300};
final double[] View = {300,300,300};
double[] Vektor1 = {0, 0, 20};
double[] Vektor2 = {0, 20, 0};
double[] Vektor3 = {0, 0, 20};
double[] AB={0,s,0};
double[] BC={s,0,0};
double[] DC={0,s,0};
double[] AD={s,0,0};
public double[] drehX(double[] Vektor, double d) {
double[][] MatrixX = {{1, 0, 0}, {0, Math.cos(d), -1 * Math.cos(d)}, {0, Math.sin(d), Math.cos(d)}};
double[] Ausgabe = MatrixmalVektor(MatrixX, Vektor);
return Ausgabe;
}
public double[] drehY(double[] Vektor, double d) {
double[][] MatrixY = {{Math.cos(d), 0, Math.sin(d)}, {0, 1, 0,}, { -1*Math.sin(d), 0, Math.cos(d)}};
double[] Ausgabe = MatrixmalVektor(MatrixY, Vektor);
return Ausgabe;
}
public double[] drehZ(double[] Vektor, double d) {
double[][] MatrixZ = {{Math.cos(d), -1*Math.sin(d), 0}, {Math.sin(d), Math.cos(d), 0}, {0, 0, 1}};
double[] Ausgabe = MatrixmalVektor(MatrixZ, Vektor);
return Ausgabe;
}
public double[] MatrixmalVektor(double[][] M, double[] V) {
double[] y = new double[3];
for (int i = 0; i < 3; ++i)
{
y[i] = 0;
for (int j = 0; j < 3; ++j)
{
y[i] += M[i][j] * V[j];
}
}
return y;
}
public double[] dim2of(double[] V) {
/*
double punktx = (Vektor[0] * faktor / Vektor[2]) + Bildschirmmitte_x;
double punkty = (Vektor[1] * faktor / Vektor[2]) + Bildschirmmitte_y;
*
*/
double dx = (V[1]-Cam[1])+(V[0]-Cam[0])-(V[2]-Cam[2]);
double dy = (V[2]-Cam[2])+(V[1]-Cam[1])-(V[0]-Cam[0])+(V[1]-Cam[1])-(V[0]-Cam[0]);
double dz = (V[2]-Cam[2])+(V[1]-Cam[1])-(V[0]-Cam[0])-(V[1]-Cam[1])-(V[0]-Cam[0]);
double bx = (dx-View[0])/(View[2]/dz);
double by = (dy-View[1])/(View[2]/dz);
double[] Ausgabe = {bx, by};
return Ausgabe;
}
@Override
public void paint(Graphics g) {
int NN = 400;
super.paint(g);
double[] V1 = dim2of(Vektor1);
double[] V2 = dim2of(Vektor2);
double[] V3 = dim2of(Vektor3);
double[] AB2 = dim2of(AB);
double[] BC2 = dim2of(BC);
double[] DC2 = dim2of(DC);
double[] AD2 = dim2of(AD);
//g.drawLine(NN, NN, NN+(int) (V1[0]), NN+(int) (V1[1]));
// g.drawLine(NN, NN, NN+(int) (V2[0]), NN+(int) (V2[1]));
//g.drawLine(NN, NN, (int) (V3[0]), (int) (V3[1]));
g.drawLine(NN, NN, NN+(int) (AB2[0]), NN+(int) (AB2[1]));
g.drawLine(NN+(int) (AB2[0]), NN+(int) (AB2[1]), NN+(int) (BC2[0]), NN+(int) (BC2[1]));
g.drawLine(NN+(int) (AD2[0]), NN+(int) (AD2[1]), NN+(int) (DC2[0]), NN+(int) (DC2[1]));
g.drawLine(NN, NN, NN+(int) (AD2[0]), NN+(int) (AD2[1]));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(100, 100));
jButton1.setText("drehX");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("drehY");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("drehZ");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(536, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(37, 37, 37))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addContainerGap(436, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Vektor1=drehX(Vektor1,0.1*Math.PI/4);
Vektor2=drehX(Vektor2,0.1*Math.PI/4);
Vektor3=drehX(Vektor3,0.1*Math.PI/4);
AB=drehX(AB,0.1*Math.PI/4);
BC=drehX(BC,0.1*Math.PI/4);
DC=drehX(DC,0.1*Math.PI/4);
AD=drehX(AD,0.1*Math.PI/4);
repaint();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Vektor1=drehY(Vektor1,0.1*Math.PI/4);
Vektor2=drehY(Vektor2,0.1*Math.PI/4);
Vektor3=drehY(Vektor3,0.1*Math.PI/4);
AB=drehY(AB,0.1*Math.PI/4);
BC=drehY(BC,0.1*Math.PI/4);
DC=drehY(DC,0.1*Math.PI/4);
AD=drehY(AD,0.1*Math.PI/4);
repaint();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Vektor1=drehZ(Vektor1,0.1*Math.PI/4);
Vektor2=drehZ(Vektor2,0.1*Math.PI/4);
Vektor3=drehZ(Vektor3,0.1*Math.PI/4);
AB=drehZ(AB,0.1*Math.PI/4);
BC=drehZ(BC,0.1*Math.PI/4);
DC=drehZ(DC,0.1*Math.PI/4);
AD=drehZ(AD,0.1*Math.PI/4);
repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Hauptklasse().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration
}
Zuletzt bearbeitet: