FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Experiments/Cuboid.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Experiments
/
Cuboid.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (102 loc) · 3.53 KB
Breadcrumbs
JavaExercises
/
Experiments
/
Cuboid.java
Copy path
File metadata and controls
127 lines (102 loc) · 3.53 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import
java
.
awt
.*;
import
java
.
awt
.
event
.*;
import
static
java
.
lang
.
Math
.*;
import
javax
.
swing
.*;
/**
* Java. Cube 3D
* based on https://rosettacode.org/wiki/Draw_a_cuboid#Java
* and http://compgraphics.info/3D/3d_affine_transformations.php
*
* @author Sergey Iryupin
* @version 0.0.3 dated Dec 28, 2018
*/
public
class
Cuboid
extends
JPanel
{
final
int
RD
=
10
;
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
}};
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
() {
setPreferredSize
(
new
Dimension
(
640
,
640
));
setBackground
(
Color
.
black
);
scale
(
100
,
100
,
100
);
//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
);
//g.setStroke(new BasicStroke(2));
g
.
setColor
(
Color
.
white
);
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
]) -
RD
/
2
, (
int
)
round
(
node
[
1
]) -
RD
/
2
,
RD
,
RD
);
}
@
Override
public
void
paintComponent
(
Graphics
gg
) {
super
.
paintComponent
(
gg
);
Graphics2D
g
= (
Graphics2D
)
gg
;
g
.
setRenderingHint
(
RenderingHints
.
KEY_ANTIALIASING
,
RenderingHints
.
VALUE_ANTIALIAS_ON
);
drawCube
(
g
);
}
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
);
});
}
}
Back
|
FazBrowse Home
|
New Git URL