-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnilamp.java
More file actions
176 lines (151 loc) · 5.3 KB
/
Anilamp.java
File metadata and controls
176 lines (151 loc) · 5.3 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import gmaths.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JSlider;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import java.util.Hashtable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Anilamp extends JFrame implements ActionListener {
private static final int WIDTH = 1024;
private static final int HEIGHT = 768;
private static final Dimension dimension = new Dimension(WIDTH, HEIGHT);
private GLCanvas canvas;
private Anilamp_GLEventListener glEventListener;
private final FPSAnimator animator;
public static void main(String[] args) {
Anilamp b1 = new Anilamp("Anilamp");
b1.getContentPane().setPreferredSize(dimension);
b1.pack();
b1.setVisible(true);
}
public Anilamp(String textForTitleBar) {
super(textForTitleBar);
GLCapabilities glcapabilities = new GLCapabilities(GLProfile.get(GLProfile.GL3));
canvas = new GLCanvas(glcapabilities);
Camera camera = new Camera(Camera.DEFAULT_POSITION, Camera.DEFAULT_TARGET, Camera.DEFAULT_UP);
glEventListener = new Anilamp_GLEventListener(camera);
canvas.addGLEventListener(glEventListener);
canvas.addMouseMotionListener(new MyMouseInput(camera));
canvas.addKeyListener(new MyKeyboardInput(camera));
getContentPane().add(canvas, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenuItem quitItem = new JMenuItem("Quit");
quitItem.addActionListener(this);
fileMenu.add(quitItem);
menuBar.add(fileMenu);
JPanel p = new JPanel();
JButton b = new JButton("Toggle Room Light");
b.addActionListener(this);
p.add(b);
b = new JButton("Toggle Lamp Light");
b.addActionListener(this);
p.add(b);
b = new JButton("Retract Lamp");
b.addActionListener(this);
p.add(b);
b = new JButton("Default Lamp Position");
b.addActionListener(this);
p.add(b);
b = new JButton("Random Lamp Position");
b.addActionListener(this);
p.add(b);
b = new JButton("Helicopter Toggle");
b.addActionListener(this);
p.add(b);
JSlider s= new JSlider(1,100,1);
s.setPaintLabels(true);
Hashtable<Integer, JLabel> pos = new Hashtable<Integer, JLabel>();
pos.put(1, new JLabel("Min"));
pos.put(50, new JLabel("50%"));
pos.put(100, new JLabel ("Max"));
s.setLabelTable(pos);
p.add(s);
s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
glEventListener.setHeliHeight(((JSlider)e.getSource()).getValue()) ;
}
});
this.add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
remove(canvas);
dispose();
System.exit(0);
}
});
animator = new FPSAnimator(canvas, 60);
animator.start();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("Retract Lamp")) {
glEventListener.lampRetract();
}else if(e.getActionCommand().equalsIgnoreCase("Default Lamp Position")){
glEventListener.lampDefault();
}else if(e.getActionCommand().equalsIgnoreCase("Random Lamp Position")){
glEventListener.lampRandom();
}else if(e.getActionCommand().equalsIgnoreCase("Helicopter Toggle")){
glEventListener.heliToggle();
}else if(e.getActionCommand().equalsIgnoreCase("Toggle Room Light")){
glEventListener.lightToggle();
}else if(e.getActionCommand().equalsIgnoreCase("Toggle Lamp Light")){
glEventListener.lampToggle();
}else if(e.getActionCommand().equalsIgnoreCase("quit")){
System.exit(0);
}
}
class MyKeyboardInput extends KeyAdapter {
private Camera camera;
public MyKeyboardInput(Camera camera) {
this.camera = camera;
}
public void keyPressed(KeyEvent e) {
Camera.Movement m = Camera.Movement.NO_MOVEMENT;
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT: m = Camera.Movement.LEFT; break;
case KeyEvent.VK_RIGHT: m = Camera.Movement.RIGHT; break;
case KeyEvent.VK_UP: m = Camera.Movement.UP; break;
case KeyEvent.VK_DOWN: m = Camera.Movement.DOWN; break;
case KeyEvent.VK_A: m = Camera.Movement.FORWARD; break;
case KeyEvent.VK_Z: m = Camera.Movement.BACK; break;
}
camera.keyboardInput(m);
}
}
class MyMouseInput extends MouseMotionAdapter {
private Point lastpoint;
private Camera camera;
public MyMouseInput(Camera camera) {
this.camera = camera;
}
/**
* mouse is used to control camera position
*
* @param e instance of MouseEvent
*/
public void mouseDragged(MouseEvent e) {
Point ms = e.getPoint();
float sensitivity = 0.001f;
float dx=(float) (ms.x-lastpoint.x)*sensitivity;
float dy=(float) (ms.y-lastpoint.y)*sensitivity;
//System.out.println("dy,dy: "+dx+","+dy);
if (e.getModifiers()==MouseEvent.BUTTON1_MASK)
camera.updateYawPitch(dx, -dy);
lastpoint = ms;
}
/**
* mouse is used to control camera position
*
* @param e instance of MouseEvent
*/
public void mouseMoved(MouseEvent e) {
lastpoint = e.getPoint();
}
}
}