aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/applet/SimpleGLAnimApplet1.java
blob: db7469010bb17b3e8f313f58017f9d90f26f4600 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
 * @(#) SimpleGLAnimApplet.java
 * @(#) author: Sven Goethel
 */

package gl4java.applet;

/* This program is licensed under the LGPL */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import gl4java.GLContext;
import gl4java.GLFunc;
import gl4java.GLUFunc;
import gl4java.awt.GLAnimCanvas;

public class SimpleGLAnimApplet1 extends Applet 
  implements MouseListener, WindowListener, ActionListener, ItemListener
{
    public GLAnimCanvas canvas = null;

    public Button buttonInfo = null;
    public Button buttonFps = null;
    public TextField textFps = null;
    public Checkbox checkUseRepaint = null;
    public Checkbox checkUseFpsSleep = null;
    public Button buttonReStart = null;
    

    Frame fInfo = null;

   /* Initialize the applet */

    public void init()
    {
        setLayout(new BorderLayout());

	Panel pan = new Panel();
	pan.setLayout(new GridLayout(2,3));

	buttonInfo = new Button("GL4Java");
	buttonInfo.addMouseListener(this);
	pan.add(buttonInfo);

	checkUseRepaint = new Checkbox("repaint", true);
	checkUseRepaint.addItemListener(this);
	pan.add(checkUseRepaint);

	checkUseFpsSleep = new Checkbox("fps-sleep", true);
	checkUseFpsSleep.addItemListener(this);
	pan.add(checkUseFpsSleep);

	buttonReStart = new Button("start/stop");
	buttonReStart.addMouseListener(this);
	pan.add(buttonReStart);

	buttonFps = new Button("fps: ");
	buttonFps.addMouseListener(this);
	pan.add(buttonFps);

	textFps=new TextField("0000000000");
	textFps.addActionListener(this);
	pan.add(textFps);

	add("South",pan);
    }


    public void start()
    {
        checkUseFpsSleep.setState(canvas.getUseFpsSleep());
        checkUseRepaint.setState(canvas.getUseRepaint());
        canvas.start();
    }


    public void stop()
    {
        canvas.stop();
    }


    public void destroy()
    {
	if(fInfo!=null)
	{
		fInfo.dispose();
		fInfo=null;
	}
        canvas.stop();
        canvas.cvsDispose();
    }


    // Methods required for the implementation of MouseListener
    public void mouseEntered( MouseEvent evt )
    {
    }
    
    public void mouseExited( MouseEvent evt )
    {
    }
    
    public void mousePressed( MouseEvent evt )
    {
    }
    
    public void mouseReleased( MouseEvent evt )
    {
    }
    
    public void mouseClicked( MouseEvent evt )
    {
	Component comp = evt.getComponent();

	if( canvas!=null && comp.equals(buttonFps) )
	{
		double fps = 0;
		int a1;

		canvas.stopFpsCounter();
		fps=canvas.getFps();
		a1=(int)(fps*100.0);
		fps=(double)a1/100.0;
		textFps.setText(String.valueOf(fps));
		canvas.resetFpsCounter();
	} else if( comp.equals(buttonInfo) )
	{
	  if(fInfo==null && canvas!=null && canvas.getGLContext()!=null)
	     fInfo = showGLInfo();
	}
	else if( comp.equals(buttonReStart) )
	{
		canvas.setSuspended(!canvas.isSuspended(),
				     evt.getClickCount()>1 // -> ReInit
				    ); 
	}
    }

    public void itemStateChanged( ItemEvent evt )
    {
	ItemSelectable comp = evt.getItemSelectable();

	if( comp.equals(checkUseRepaint ) )
	{
		if(canvas!=null)
		{
		  canvas.setUseRepaint(checkUseRepaint.getState());
		  System.out.println("canvas uses repaint: "+
			checkUseRepaint.getState());
		}
	}
	if( comp.equals(checkUseFpsSleep ) )
	{
		if(canvas!=null)
		{
		  canvas.setUseFpsSleep(checkUseFpsSleep.getState());
		  System.out.println("canvas uses fps-sleep: "+
			checkUseFpsSleep.getState());
		}
	}
    }

    public void actionPerformed(ActionEvent event) 
    {
	    Object source = event.getSource();

	    if ( source == textFps)
	    {
		      try {
			double FramesPerSec=
			  Double.valueOf(textFps.getText()).doubleValue();
			if(canvas!=null)
			{
			    canvas.setAnimateFps(FramesPerSec);
			    canvas.setSuspended(false, true);
			}
		      } catch (NumberFormatException s)   {
			 System.out.println("wrong fps format, use float ..");
		      }         

	    }
    }

	public void windowOpened(WindowEvent e)
	{
	}

	public void windowClosing(WindowEvent e)
	{
		Window w = e.getWindow();
		if(w == fInfo && fInfo!=null)
		{
			fInfo.dispose();
			fInfo=null;
		}
	}

	public void windowClosed(WindowEvent e)
	{
		Window w = e.getWindow();
		if(w == fInfo && fInfo!=null)
		{
			fInfo.dispose();
			fInfo=null;
		}
	}

	public void windowIconified(WindowEvent e)
	{
	}

	public void windowDeiconified(WindowEvent e)
	{
	}

	public void windowActivated(WindowEvent e)
	{
	}

	public void windowDeactivated(WindowEvent e)
	{
	}

	public Frame showGLInfo()
	{
		if(canvas==null) return null;

		GLContext glc =  canvas.getGLContext();
		if(glc==null) return null;

		GLFunc    gl  =  glc.getGLFunc();
		if(gl==null) return null;

		GLUFunc   glu =  glc.getGLUFunc();
		if(gl==null) return null;

		canvas.setSuspended(true, false);

		Frame f = new Frame("GL4Java Version");
		TextArea info= new TextArea(25, 80);
		info.setEditable(false);
		f.add(info);
		f.setSize(600, 400);

		String str = "null string";
		if( glc.gljMakeCurrent() == false ) 
		{
			str="problem in use() method\n";
		} else {
			str=canvas.getGLContext().gljGetVersions();
			if(str==null)
				str="could not get versions";
			System.out.println(str);
			glc.gljFree();
		}
		info.append(str);

		f.addWindowListener(this);

		canvas.setSuspended(false, false);

		f.pack();
		f.setVisible(true);

		return f;
	}
}