
Опытный
 
Профиль
Группа: Участник
Сообщений: 877
Регистрация: 9.4.2005
Где: JDK
Репутация: 2 Всего: 25
|
Reptor, Можно, держи... Это класс фигуры: | Код | package ru.tcde.is022das;
import java.awt.Color; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.geom.Line2D;
/** * * @author Degtiarev * Created on 24 Январь 2007 г., 14:27 */ public class Hexahedron { public float[][] coords, normals; private float[][] matrix = new float[4][4]; //матрица для преобразований private void eraseMatrix(float[][] m) { // заполнение матрицы нулями for (int i = 0; i < m.length; i++) for (int j = 0; j < m[1].length; j++) m[i][j] = 0; } private float[][] multiplyMatrix(float[][] cor, float[][] mat) { // перемножение двух матриц для преобразования координат float[][] mt = new float[cor.length][mat[1].length]; int i, j, k; for (i = 0; i < cor.length; i++) { for (j = 0; j < cor[1].length; j++) { mt[i][j] = 0; for (k = 0; k < mat.length; k++) { mt[i][j] += cor[i][k] * mat[k][j]; } } } return mt; } public void rotate(float rx, float ry, float rz) { // вращение if (rx != 0) { // вращение, параллельное оси y eraseMatrix(matrix); matrix[0][0] = (float)Math.cos(rx); matrix[0][2] = (float)-Math.sin(rx); matrix[1][1] = 1; matrix[2][0] = (float)Math.sin(rx); matrix[2][2] = (float)Math.cos(rx); matrix[3][3] = 1; coords = multiplyMatrix(coords, matrix); normals = multiplyMatrix(normals, matrix); } if (ry != 0) { // вращение, параллельное оси x eraseMatrix(matrix); matrix[0][0] = 1; matrix[1][1] = (float)Math.cos(ry); matrix[1][2] = (float)Math.sin(ry); matrix[2][1] = (float)-Math.sin(ry); matrix[2][2] = (float)Math.cos(ry); matrix[3][3] = 1; coords = multiplyMatrix(coords, matrix); normals = multiplyMatrix(normals, matrix); } if (rz != 0) { // вращение, параллельное оси z eraseMatrix(matrix); matrix[0][0] = (float)Math.cos(rz); matrix[0][1] = (float)Math.sin(rz); matrix[1][0] = (float)-Math.sin(rz); matrix[1][1] = (float)Math.cos(rz); matrix[2][2] = 1; matrix[3][3] = 1; coords = multiplyMatrix(coords, matrix); normals = multiplyMatrix(normals, matrix); } } public void move(float mx, float my) { // перемещение eraseMatrix(matrix); for (int i = 0; i < 4; i++) matrix[i][i] = 1; matrix[3][0] = mx; matrix[3][1] = my; coords = multiplyMatrix(coords, matrix); } public void Draw(Graphics2D rgr) { //прорисовка тела final float c = 305; final float d = 195; Polygon p1 = new Polygon(); p1.addPoint((int)(coords[1][0] + c),(int)(coords[1][1] + d)); p1.addPoint((int)(coords[6][0] + c),(int)(coords[6][1] + d)); p1.addPoint((int)(coords[2][0] + c),(int)(coords[2][1] + d)); p1.addPoint((int)(coords[5][0] + c),(int)(coords[5][1] + d)); Polygon p2 = new Polygon(); p2.addPoint((int)(coords[3][0] + c),(int)(coords[3][1] + d)); p2.addPoint((int)(coords[7][0] + c),(int)(coords[7][1] + d)); p2.addPoint((int)(coords[0][0] + c),(int)(coords[0][1] + d)); p2.addPoint((int)(coords[4][0] + c),(int)(coords[4][1] + d)); Polygon p3 = new Polygon(); p3.addPoint((int)(coords[0][0] + c),(int)(coords[0][1] + d)); p3.addPoint((int)(coords[7][0] + c),(int)(coords[7][1] + d)); p3.addPoint((int)(coords[1][0] + c),(int)(coords[1][1] + d)); p3.addPoint((int)(coords[6][0] + c),(int)(coords[6][1] + d)); Polygon p4 = new Polygon(); p4.addPoint((int)(coords[5][0] + c),(int)(coords[5][1] + d)); p4.addPoint((int)(coords[2][0] + c),(int)(coords[2][1] + d)); p4.addPoint((int)(coords[4][0] + c),(int)(coords[4][1] + d)); p4.addPoint((int)(coords[3][0] + c),(int)(coords[3][1] + d)); Polygon p5 = new Polygon(); p5.addPoint((int)(coords[1][0] + c),(int)(coords[1][1] + d)); p5.addPoint((int)(coords[5][0] + c),(int)(coords[5][1] + d)); p5.addPoint((int)(coords[3][0] + c),(int)(coords[3][1] + d)); p5.addPoint((int)(coords[7][0] + c),(int)(coords[7][1] + d)); Polygon p6 = new Polygon(); p6.addPoint((int)(coords[0][0] + c),(int)(coords[0][1] + d)); p6.addPoint((int)(coords[4][0] + c),(int)(coords[4][1] + d)); p6.addPoint((int)(coords[2][0] + c),(int)(coords[2][1] + d)); p6.addPoint((int)(coords[6][0] + c),(int)(coords[6][1] + d)); Polygon[] arrP = new Polygon[] {p1,p2,p3,p4,p5,p6}; Color[] arrC = new Color[] {Color.PINK,Color.GRAY,Color.GREEN,Color.RED,Color.BLUE,Color.YELLOW}; for (int i=0; i<6;i++) { rgr.setColor(arrC[i]); if (normals[i][2]<0) { rgr.fillPolygon(arrP[i]); } } } public Hexahedron() { coords = new float[][] { {50, 50, 50, 1}, //0 {-50, 50, -50, 1}, //1 {50, -50, -50, 1}, //2 {-50, -50, 50, 1}, //3 {50, -50, 50, 1}, //4 {-50, -50, -50, 1},//5 {50, 50, -50, 1}, //6 {-50, 50, 50, 1} //7 }; normals = new float[][] { //вектора нормали {0,0,-1,1}, {0,0,1,1}, {0,1,0,1}, {0,-1,0,1}, {-1,0,0,1}, {1,0,0,1} }; } }
|
Огромную помощь в написании этого класса мне оказал участник этого форума $taticЗтем в создаешь класс, наследуешь его от JFrame и юзаешь так: | Код | package ru.tcde.is022das;
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.*; import javax.swing.Timer;
/** * * @author Degtiarev * Created on 18 Январь 2007 г., 17:56 */ public class MainFrame extends javax.swing.JFrame { private Graphics2D gr; private Hexahedron cF;
public MainFrame() { initComponents(); } // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents private void initComponents() { graphPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("\u0413\u0435\u043a\u0441\u0430\u044d\u0434\u0440"); setResizable(false); graphPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); graphPanel.setPreferredSize(new java.awt.Dimension(610, 390)); org.jdesktop.layout.GroupLayout graphPanelLayout = new org.jdesktop.layout.GroupLayout(graphPanel); graphPanel.setLayout(graphPanelLayout); graphPanelLayout.setHorizontalGroup( graphPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 608, Short.MAX_VALUE) ); graphPanelLayout.setVerticalGroup( graphPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 388, Short.MAX_VALUE) );
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(10, 10, 10) .add(graphPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(graphPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-638)/2, (screenSize.height-439)/2, 638, 439); }// </editor-fold>//GEN-END:initComponents private void init() { gr = (Graphics2D)graphPanel.getGraphics(); cF = new Hexahedron(); cF.move(-150,0); cF.rotate(0,0.25F,0); cF.Draw(gr); Timer t = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { gr.clearRect(1,1,608,388); cF.rotate(0.2F, 0, 0); cF.Draw(gr); } }); t.start(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { MainFrame mf = new MainFrame(); mf.setVisible(true); mf.init(); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel graphPanel; // End of variables declaration//GEN-END:variables }
|
Здесь метод private void initComponents() { был сгенерен гуевым редактором нетбинса. Если что не понятно, спрашивай...
|