forked from anirudhgupta03/3D-Rotating-Cube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.java
More file actions
157 lines (139 loc) · 4.33 KB
/
Copy pathDisplay.java
File metadata and controls
157 lines (139 loc) · 4.33 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.miniproject;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
public class Display extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
private Thread thread;
private JFrame frame;
private static String title ="3DAnimation";
public static int WIDTH;
public static int HEIGHT;
private static boolean running =false;
private Object3D polyhedron;
private Mouse mouse;
public Display()
{
this.frame= new JFrame();
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setMinimumSize(new Dimension(600,600));
this.mouse= new Mouse();
this.addMouseListener(this.mouse);
this.addMouseMotionListener(this.mouse);
this.addMouseWheelListener(this.mouse);
}
public synchronized void start()
{
running = true;
this.thread=new Thread(this,"Display");
this.thread.start(); //Causes this thread to begin execution; the JVM calls the run method of this thread
}
public synchronized void stop()
{
running = false;
try
{
this.thread.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Display display=new Display();
display.frame.setTitle((title));
display.frame.add(display);
display.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display.frame.setLocationRelativeTo(null);
display.frame.setVisible(true);
display.start();
}
@Override
public void run()
{
init();
while(running)
{
render();
update();
//this.polyhedron.rotate(true, 0, 0.3, 0);
}
stop();
}
public void init()
{
int s = 100;
Point3D p1 = new Point3D(s/2,-s/2,-s/2);
Point3D p2 = new Point3D(s/2,s/2,-s/2);
Point3D p3 = new Point3D(s/2,s/2,s/2);
Point3D p4 = new Point3D(s/2,-s/2,s/2);
Point3D p5 = new Point3D(-s/2,-s/2,-s/2);
Point3D p6 = new Point3D(-s/2,s/2,-s/2);
Point3D p7 = new Point3D(-s/2,s/2,s/2);
Point3D p8 = new Point3D(-s/2,-s/2,s/2);
this.polyhedron = new Object3D(
new Polygon3D(Color.BLUE,p5,p6,p7,p8),
new Polygon3D(Color.WHITE,p1,p2,p6,p5 ),
new Polygon3D(Color.YELLOW,p1,p5,p8,p4),
new Polygon3D(Color.GREEN,p2,p6,p7,p3),
new Polygon3D(Color.PINK,p4,p3,p7,p8),
new Polygon3D(Color.RED,p1,p2,p3,p4));
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs==null)
{
this.createBufferStrategy(2);
return;
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Graphics g = bs.getDrawGraphics();
g.setColor(Color.GRAY);
g.fillRect(0, 0, screenSize.width, screenSize.height);
polyhedron.render(g);
g.dispose();
bs.show();
}
int initialX,initialY;
double mouseSensitivity=2;
public void update()
{
int x=this.mouse.getX();
int y=this.mouse.getY();
if(this.mouse.getButton() == ClickType.LeftClick)
{
int xDif=x-initialX;
int yDif=y-initialY;
this.polyhedron.rotate(
0,
yDif/mouseSensitivity,
xDif/mouseSensitivity);
}
else if(this.mouse.getButton() == ClickType.RightClick)
{
int xDif=x-initialX;
this.polyhedron.rotate(
xDif/mouseSensitivity,
0,
0);
}
if(this.mouse.isScrollingUp())
{
PointConverter.zoomIn();
}
else if(this.mouse.isScrollingDown())
{
PointConverter.zoomOut();
}
this.mouse.resetScroll();
initialX=x;
initialY=y;
Rectangle r = frame.getBounds();
WIDTH = r.width;
HEIGHT = r.height;
}
}