Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,30 +1,92 @@
|
|||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import static java.lang.Math.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class Cuboid extends JFrame {
|
||||
public class Cuboid extends JPanel {
|
||||
double[][] nodes = {{-1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}, {-1, 1, 1},
|
||||
{1, -1, -1}, {1, -1, 1}, {1, 1, -1}, {1, 1, 1}};
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame f = new Cuboid();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setVisible(true);
|
||||
}
|
||||
int[][] edges = {{0, 1}, {1, 3}, {3, 2}, {2, 0}, {4, 5}, {5, 7}, {7, 6},
|
||||
{6, 4}, {0, 4}, {1, 5}, {2, 6}, {3, 7}};
|
||||
|
||||
int mouseX, prevMouseX, mouseY, prevMouseY;
|
||||
|
||||
public Cuboid() {
|
||||
Container content = getContentPane();
|
||||
content.setLayout(new BorderLayout());
|
||||
content.add(new CuboidPanel(), BorderLayout.CENTER);
|
||||
setTitle("Cuboid");
|
||||
setResizable(false);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
}
|
||||
}
|
||||
|
||||
class CuboidPanel extends JPanel {
|
||||
|
||||
public CuboidPanel() {
|
||||
setPreferredSize(new Dimension(600, 500));
|
||||
setPreferredSize(new Dimension(640, 640));
|
||||
setBackground(Color.white);
|
||||
|
||||
scale(80, 120, 160);
|
||||
rotateCube(PI / 5, PI / 9);
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
mouseX = e.getX();
|
||||
mouseY = e.getY();
|
||||
}
|
||||
});
|
||||
|
||||
addMouseMotionListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
prevMouseX = mouseX;
|
||||
prevMouseY = mouseY;
|
||||
mouseX = e.getX();
|
||||
mouseY = e.getY();
|
||||
|
||||
double incrX = (mouseX - prevMouseX) * 0.01;
|
||||
double incrY = (mouseY - prevMouseY) * 0.01;
|
||||
|
||||
rotateCube(incrX, incrY);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void scale(double sx, double sy, double sz) {
|
||||
for (double[] node : nodes) {
|
||||
node[0] *= sx;
|
||||
node[1] *= sy;
|
||||
node[2] *= sz;
|
||||
}
|
||||
}
|
||||
|
||||
private void rotateCube(double angleX, double angleY) {
|
||||
double sinX = sin(angleX);
|
||||
double cosX = cos(angleX);
|
||||
|
||||
double sinY = sin(angleY);
|
||||
double cosY = cos(angleY);
|
||||
|
||||
for (double[] node : nodes) {
|
||||
double x = node[0];
|
||||
double y = node[1];
|
||||
double z = node[2];
|
||||
|
||||
node[0] = x * cosX - z * sinX;
|
||||
node[2] = z * cosX + x * sinX;
|
||||
|
||||
z = node[2];
|
||||
|
||||
node[1] = y * cosY - z * sinY;
|
||||
node[2] = z * cosY + y * sinY;
|
||||
}
|
||||
}
|
||||
|
||||
void drawCube(Graphics2D g) {
|
||||
g.translate(getWidth() / 2, getHeight() / 2);
|
||||
|
||||
for (int[] edge : edges) {
|
||||
double[] xy1 = nodes[edge[0]];
|
||||
double[] xy2 = nodes[edge[1]];
|
||||
g.drawLine((int) round(xy1[0]), (int) round(xy1[1]),
|
||||
(int) round(xy2[0]), (int) round(xy2[1]));
|
||||
}
|
||||
|
||||
for (double[] node : nodes) {
|
||||
g.fillOval((int) round(node[0]) - 4, (int) round(node[1]) - 4, 8, 8);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -34,13 +96,19 @@ class CuboidPanel extends JPanel {
|
|||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
g.translate(165, -80);
|
||||
drawCube(g);
|
||||
}
|
||||
|
||||
g.drawRect(50, 275, 100, 100);
|
||||
g.drawLine(50, 275, 130, 240);
|
||||
g.drawLine(150, 275, 210, 240);
|
||||
g.drawLine(130, 240, 210, 240);
|
||||
g.drawLine(210, 240, 210, 340);
|
||||
g.drawLine(150, 375, 210, 340);
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JFrame f = new JFrame();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setTitle("Cuboid");
|
||||
f.setResizable(false);
|
||||
f.add(new Cuboid(), BorderLayout.CENTER);
|
||||
f.pack();
|
||||
f.setLocationRelativeTo(null);
|
||||
f.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue