aboutsummaryrefslogtreecommitdiffstats
path: root/demos/SwingDemos/GLSwingDemoApplet.java
blob: 33b0d95d9e0cd0f8d449f26f3cca0785ca43309a (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
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.*;
import gl4java.*;
import gl4java.swing.*;
import gl4java.utils.glut.*;
import gl4java.utils.glut.fonts.*;

public class GLSwingDemoApplet extends SimpleGLJApplet1 
  implements GLEnum, GLUEnum, ActionListener, ItemListener
{
    String[] dataSize = {"offscreen-size: component",
		         "offscreen-size: 10x10",
		         "offscreen-size: 50x50",
		         "offscreen-size: 100x100",
		         "offscreen-size: 500x500",
		         "offscreen-size: 1000x1000"};

    Dimension[] dimSize = { null,
    			 new Dimension(10,10),
    			 new Dimension(50,50),
    			 new Dimension(100,100),
    			 new Dimension(500,500),
    			 new Dimension(1000,1000)};


    String[] data = {"CrossColorJPanel",
		     "GearsJPanel",
                     "DrawColoredPrimitivesJPanel", 
		     "TessJPanel"};

    JPanel   masterPanel  = null;
    JComboBox glChoice = null;
    JComboBox glOffScrnSize = null;
    boolean isAnApplet = true;

    public void init()
    {
	super.init();
	if(isAnApplet==false)
	{
	    GLContext.gljNativeDebug = true;
	    GLContext.gljClassDebug  = true;
	}

	masterPanel = new JPanel (new BorderLayout());
	getContentPane().add("Center",masterPanel);
	canvas = new CrossColorJPanel();
	masterPanel.add("Center", canvas);

	    JPanel np = new JPanel();
	    masterPanel.add("North", np);
	    glChoice = new JComboBox(data);
	    glChoice.addItemListener(this);
	    np.add(glChoice);
	    glOffScrnSize = new JComboBox(dataSize);
	    glOffScrnSize.addItemListener(this);
	    np.add(glOffScrnSize);

	    JButton b1, b2;
	    masterPanel.add("East", (b1 = new JButton("E")));
	    b1.setActionCommand("east");
	    b1.addActionListener(this);
	    masterPanel.add("West", (b2 = new JButton("W")));
	    b2.setActionCommand("west");
	    b2.addActionListener(this);

	    JCheckBox c1;
	    masterPanel.add("South", (c1 = new JCheckBox("SouthButton", true)));
	    c1.addItemListener(this);
    }

    public static void main(java.lang.String[] args) {
	try {
		boolean withAWT=false;

		if(args.length>0 && args[0].equals("withAWT"))
			withAWT=true;
		Frame mainFrame = new Frame("test1");

	        GLSwingDemoApplet applet=new GLSwingDemoApplet();

		applet.isAnApplet=false;
		applet.init();
		applet.start();

		mainFrame.add(applet);
		applet.setSize(480, 480);

		if(withAWT)
		{
			gears applet2 = new gears();
			applet2.setSize(300, 300);
			applet2.init();
			applet2.start();
			mainFrame.add(applet2);
			applet2.setSize(300, 300);
		}
		mainFrame.pack();
		mainFrame.setVisible(true);

	} catch (Throwable exception) {
	    	System.err.println("Exception occurred in main()");
	    	exception.printStackTrace(System.out);
	}
    }

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("east")) {
			System.out.println("East pressed");
		} else if (e.getActionCommand().equals("west")) {
			System.out.println("West pressed");
		}
        }

	public void itemStateChanged(ItemEvent e) {
		Object source = e.getItemSelectable();
		Object item = e.getItem();
		System.out.println("item changed: "+source);

		if(glChoice!=null &&
		   glChoice.equals(e.getItemSelectable()) &&
		   e.getStateChange()==ItemEvent.SELECTED &&
		   (item instanceof String)==true
		  )
                {
			String clazzName = (String)item;
			try {
				Object obj = 
					Class.forName(clazzName).newInstance();
				if(obj instanceof GLJPanel)
				{
				    if(canvas!=null)
				    {
					    masterPanel.remove(canvas);
					    canvas.cvsDispose();
			 	    }
				    canvas = (GLJPanel)obj;
				    canvas.setVisible(true);
				    masterPanel.add("Center", canvas);
				    masterPanel.invalidate();
				    masterPanel.validate();
				    masterPanel.repaint();
				    glOffScrnSize.setSelectedIndex(0);
				}
				if(obj instanceof GLAnimJPanel)
				{
				    ((GLAnimJPanel)obj).start();
				}
			} catch (Exception ex) {
			     System.out.println("Exception during loading: "+
				  clazzName);
			     System.out.println(ex);
			}
		} else
		if(glOffScrnSize!=null && canvas!=null &&
		   glOffScrnSize.equals(e.getItemSelectable()) &&
		   e.getStateChange()==ItemEvent.SELECTED &&
		   (item instanceof String)==true
		  )
                {
			int idx = glOffScrnSize.getSelectedIndex();
			canvas.setOffScreenSize(dimSize[idx]);
		}
	}
}