Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / examples / gnu / classpath / examples / awt / Demo.java
blob64594e47b1014ef7b6837ee1d8eab2a2a3adb5e9
1 /* Demo.java -- Shows examples of AWT components
2 Copyright (C) 1998, 1999, 2002, 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
21 package gnu.classpath.examples.awt;
23 import java.awt.*;
24 import java.awt.List;
25 import java.awt.event.*;
26 import java.net.URL;
27 import java.util.*;
29 class Demo
31 public static void main(String args[])
33 MainWindow f = new MainWindow();
34 f.show();
37 static interface SubWindow
39 public void init ();
42 static class PrettyPanel extends Panel
44 Insets myInsets;
46 public PrettyPanel ()
48 myInsets = new Insets (10, 10, 10, 10);
50 public Insets getInsets ()
52 return myInsets;
56 static abstract class PrettyFrame extends Frame
58 public PrettyFrame ()
60 ((BorderLayout) getLayout ()).setHgap (5);
61 ((BorderLayout) getLayout ()).setVgap (5);
64 public Insets getInsets()
66 Insets oldInsets = super.getInsets ();
67 return new Insets (oldInsets.top+10,
68 oldInsets.left+10,
69 oldInsets.bottom+10,
70 oldInsets.right+10);
74 static abstract class SubFrame extends PrettyFrame implements SubWindow
76 boolean initted = false;
78 public void setVisible (boolean visible)
80 if (!initted && visible)
81 init();
82 super.setVisible (visible);
86 static class MainWindow extends PrettyFrame implements ActionListener
88 Button closeButton;
90 Hashtable windows;
91 Vector buttons;
93 void addSubWindow (String name, SubWindow w)
95 Button b = new Button (name);
96 b.addActionListener (this);
98 buttons.addElement (b);
99 windows.put (b, w);
102 MainWindow ()
104 MenuBar mb = new MenuBar ();
105 Menu menu = new Menu ("File");
106 Menu submenu = new Menu ("Testing", true);
107 submenu.add (new CheckboxMenuItem ("FooBar"));
108 submenu.add (new CheckboxMenuItem ("BarFoo"));
109 menu.add (submenu);
110 menu.add (new MenuItem("Orange"));
111 MenuItem quit = new MenuItem("Quit", new MenuShortcut('Q'));
112 quit.addActionListener(new ActionListener()
114 public void actionPerformed(ActionEvent e)
116 System.exit(0);
119 menu.add(quit);
120 mb.add (menu);
122 menu = new Menu("Edit", true);
123 menu.add(new MenuItem("Cut"));
124 menu.add(new MenuItem("Copy"));
125 menu.add(new MenuItem("Paste"));
126 mb.add (menu);
128 Menu helpMenu = new Menu("Help");
129 helpMenu.add(new MenuItem("About"));
130 mb.add(helpMenu);
131 mb.setHelpMenu(helpMenu);
133 setMenuBar (mb);
135 String version = System.getProperty("gnu.classpath.version");
136 add (new Label ("GNU Classpath " + version), "North");
138 closeButton = new Button ("Close");
139 closeButton.addActionListener (this);
140 closeButton.setFont (new Font ("Serif", Font.BOLD | Font.ITALIC, 18));
141 add (closeButton, "South");
143 windows = new Hashtable ();
144 buttons = new Vector ();
146 addSubWindow ("Buttons", new ButtonsWindow ());
147 addSubWindow ("Cursors", new CursorsWindow ());
148 addSubWindow ("Dialog", new DialogWindow (this));
149 addSubWindow ("File", new FileWindow (this));
150 addSubWindow ("Labels", new LabelWindow ());
151 addSubWindow ("List", new ListWindow ());
152 addSubWindow ("Radio Buttons", new RadioWindow ());
153 addSubWindow ("TextField", new TextFieldWindow ());
154 addSubWindow ("RandomTests", new TestWindow (this));
155 addSubWindow ("RoundRect", new RoundRectWindow ());
156 addSubWindow ("Animation", new AnimationWindow ());
158 Panel sp = new Panel();
159 PrettyPanel p = new PrettyPanel();
160 p.setLayout (new GridLayout (windows.size(), 1));
162 for (Enumeration e = buttons.elements (); e.hasMoreElements (); )
164 p.add ((Button) e.nextElement ());
167 sp.add (p);
168 add (sp, "Center");
170 setTitle ("AWT Demo");
171 pack();
174 public void actionPerformed (ActionEvent evt)
176 Button source = (Button) evt.getSource ();
178 if (source==closeButton)
180 dispose();
181 System.exit (0);
184 Window w = (Window) windows.get (source);
185 if (w.isVisible ())
186 w.dispose ();
187 else
189 if (w instanceof Dialog)
191 w.show();
193 else
195 w.setVisible (true);
201 static class ButtonsWindow extends SubFrame implements ActionListener
203 Button b[] = new Button [9];
205 public void init ()
207 initted = true;
208 Panel p = new Panel ();
209 p.setLayout (new GridLayout (0, 3, 5, 5));
211 for (int i=0; i<9; i++)
213 b[i]=new Button ("button" + (i+1));
214 b[i].addActionListener (this);
217 p.add (b[0]);
218 p.add (b[6]);
219 p.add (b[4]);
220 p.add (b[8]);
221 p.add (b[1]);
222 p.add (b[7]);
223 p.add (b[3]);
224 p.add (b[5]);
225 p.add (b[2]);
227 add (p, "North");
229 Button cb = new Button ("close");
230 cb.addActionListener(new ActionListener () {
231 public void actionPerformed (ActionEvent e) {
232 dispose();
235 add (cb, "South");
236 setTitle ("Buttons");
237 pack();
240 public void actionPerformed (ActionEvent evt)
242 Button source = (Button) evt.getSource ();
244 for (int i = 0; i < 9; i++)
246 if (source == b[i])
248 int i2 = ((i + 1) == 9) ? 0 : (i + 1);
249 if (b[i2].isVisible())
250 b[i2].setVisible(false);
251 else
252 b[i2].setVisible(true);
259 static class DialogWindow extends Dialog implements SubWindow
261 Label text;
262 Frame parent;
263 boolean initted = false;
265 public DialogWindow (Frame f)
267 super (f, true);
269 this.parent = f;
271 addWindowListener (new WindowAdapter ()
273 public void windowClosing (WindowEvent e)
275 text.setVisible (false);
276 hide ();
281 public void setVisible (boolean visible)
283 if (!initted && visible)
284 init();
285 super.setVisible (visible);
288 public void show ()
290 if (!initted)
291 init();
292 super.show ();
295 public void init ()
297 text = new Label ("Dialog Test");
298 text.setAlignment (Label.CENTER);
300 add (text, "North");
301 text.setVisible (false);
303 Panel p = new PrettyPanel();
305 Button cb = new Button ("OK");
306 cb.addActionListener(new ActionListener () {
307 public void actionPerformed (ActionEvent e)
309 text.setVisible (false);
310 hide();
314 p.setLayout (new GridLayout (1, 3));
315 ((GridLayout) p.getLayout ()).setHgap (5);
316 ((GridLayout) p.getLayout ()).setVgap (5);
317 p.add (cb);
319 Button toggle = new Button ("Toggle");
320 p.add (toggle);
322 toggle.addActionListener(new ActionListener () {
323 public void actionPerformed (ActionEvent e)
325 if (text.isVisible ())
326 text.setVisible (false);
327 else
328 text.setVisible (true);
329 doLayout();
333 Button subdlg = new Button ("SubDialog");
334 p.add (subdlg);
336 subdlg.addActionListener(new ActionListener () {
337 public void actionPerformed (ActionEvent e)
339 DialogWindow sw = new DialogWindow (parent);
340 sw.show ();
344 add (p, "South");
345 setTitle ("Dialog");
346 pack();
350 static class CursorsWindow extends SubFrame implements ItemListener
352 Choice cursorChoice;
353 Canvas cursorCanvas;
355 public void init ()
357 cursorChoice = new Choice();
358 cursorChoice.add ("Default");
359 cursorChoice.add ("Crosshair");
360 cursorChoice.add ("Text");
361 cursorChoice.add ("Wait");
362 cursorChoice.add ("Southwest Resize");
363 cursorChoice.add ("Southeast Resize");
364 cursorChoice.add ("Northwest Resize");
365 cursorChoice.add ("Northeast Resize");
366 cursorChoice.add ("North Resize");
367 cursorChoice.add ("South Resize");
368 cursorChoice.add ("West Resize");
369 cursorChoice.add ("East Resize");
370 cursorChoice.add ("Hand");
371 cursorChoice.add ("Move");
373 cursorChoice.addItemListener(this);
375 add (cursorChoice, "North");
377 cursorCanvas = new Canvas ()
379 public void paint (Graphics g)
381 Dimension d = this.getSize();
382 g.setColor(Color.white);
383 g.fillRect(0, 0, d.width, d.height/2);
384 g.setColor(Color.black);
385 g.fillRect(0, d.height/2, d.width, d.height/2);
386 g.setColor(this.getBackground());
387 g.fillRect(d.width/3, d.height/3, d.width/3,
388 d.height/3);
392 cursorCanvas.setSize (80,80);
394 add (cursorCanvas, "Center");
396 Button cb = new Button ("Close");
397 cb.addActionListener(new ActionListener () {
398 public void actionPerformed (ActionEvent e) {
399 dispose();
403 add (cb, "South");
404 setTitle ("Cursors");
405 pack();
408 public void itemStateChanged (ItemEvent e)
410 int index = cursorChoice.getSelectedIndex();
411 cursorCanvas.setCursor(Cursor.getPredefinedCursor(index));
415 static class TextFieldWindow extends SubFrame implements ItemListener
417 Checkbox editable, visible, sensitive;
418 TextField text;
420 public void init ()
422 initted = true;
423 text = new TextField ("hello world");
424 add (text, "North");
426 Panel p = new Panel();
427 p.setLayout (new GridLayout (3, 1));
428 ((GridLayout) p.getLayout ()).setHgap (5);
429 ((GridLayout) p.getLayout ()).setVgap (5);
431 editable = new Checkbox("Editable", true);
432 p.add (editable);
433 editable.addItemListener (this);
435 visible = new Checkbox("Visible", true);
436 p.add (visible);
437 visible.addItemListener (this);
439 sensitive = new Checkbox("Sensitive", true);
440 p.add (sensitive);
441 sensitive.addItemListener (this);
443 add (p, "Center");
445 Button cb = new Button ("Close");
446 cb.addActionListener(new ActionListener () {
447 public void actionPerformed (ActionEvent e) {
448 dispose();
452 add (cb, "South");
453 setTitle ("TextField");
454 pack();
457 public void itemStateChanged (ItemEvent e)
459 boolean on=true;
461 if (e.getStateChange () == ItemEvent.DESELECTED)
462 on=false;
463 if (e.getSource() == editable)
464 text.setEditable (on);
465 if (e.getSource() == visible)
466 if (on)
467 text.setEchoChar ((char) 0);
468 else
469 text.setEchoChar ('*');
470 if (e.getSource() == sensitive)
471 text.setEnabled (on);
476 static class FileWindow extends FileDialog implements SubWindow
478 boolean initted = false;
480 public FileWindow (MainWindow mw)
482 super (mw);
485 public void setVisible (boolean visible)
487 if (!initted && visible)
488 init();
489 super.setVisible (visible);
492 public void init()
494 initted = true;
498 static class LabelWindow extends SubFrame
500 public void init ()
502 initted = true;
504 Panel p = new Panel();
505 p.setLayout (new GridLayout (3, 1));
506 ((GridLayout) p.getLayout ()).setHgap (5);
507 ((GridLayout) p.getLayout ()).setVgap (5);
509 p.add (new Label ("left justified label", Label.LEFT));
510 p.add (new Label ("center justified label", Label.CENTER));
511 p.add (new Label ("right justified label", Label.RIGHT));
513 add (p, "Center");
515 Button cb = new Button ("Close");
516 cb.addActionListener(new ActionListener () {
517 public void actionPerformed (ActionEvent e) {
518 dispose();
522 add (cb, "South");
523 setTitle ("Labels");
524 pack();
528 static class ListWindow extends SubFrame
530 public void init ()
532 initted = true;
534 Panel p = new Panel ();
535 p.setLayout (new GridLayout (3, 1));
537 List l = new List (5, true);
538 for (int i = 0; i < 10; i++)
539 l.add ("List item " + i);
541 p.add (l);
543 add (p, "Center");
545 Button cb = new Button ("Close");
546 cb.addActionListener(new ActionListener () {
547 public void actionPerformed (ActionEvent e) {
548 dispose();
552 add (cb, "South");
553 setTitle ("List");
554 pack();
559 static class RadioWindow extends SubFrame
561 public void init ()
563 initted = true;
565 Panel p = new Panel();
566 p.setLayout (new GridLayout (3, 1));
567 ((GridLayout) p.getLayout ()).setHgap (5);
568 ((GridLayout) p.getLayout ()).setVgap (5);
570 final CheckboxGroup cg = new CheckboxGroup();
571 final Checkbox[] boxes = new Checkbox[3];
572 for (int i = 0; i < 3; ++i)
574 boxes[i] = new Checkbox("button" + i, cg, i == 0);
575 p.add(boxes[i]);
578 add (p, "North");
580 p = new Panel();
581 p.setLayout (new GridLayout (1, 3));
582 ((GridLayout) p.getLayout ()).setHgap (5);
583 ((GridLayout) p.getLayout ()).setVgap (5);
585 for (int i = 0; i < 3; ++i)
587 final int val = i;
588 Button tweak = new Button ("Set " + i);
589 tweak.addActionListener(new ActionListener ()
591 public void actionPerformed (ActionEvent e)
593 cg.setSelectedCheckbox(boxes[val]);
596 p.add(tweak);
599 add (p, "Center");
601 Button cb = new Button ("Close");
602 cb.addActionListener(new ActionListener () {
603 public void actionPerformed (ActionEvent e) {
604 dispose();
608 add (cb, "South");
609 setTitle ("Radio Buttons");
610 pack();
614 static class TestWindow extends SubFrame
616 static int xs = 5, ys = 5;
617 final Frame parent;
619 public TestWindow(Frame f)
621 parent = f;
624 public void init()
626 initted = true;
628 addWindowListener (new WindowAdapter ()
630 public void windowClosing (WindowEvent e)
632 hide ();
636 Panel pan = new Panel();
638 final Label l = new Label ("Pithy Message:");
639 l.setCursor (Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR));
640 pan.add (l);
642 TextField tf = new TextField("Hello world!");
643 pan.add(tf);
644 add(pan,"North");
646 final Image img;
647 URL imageurl;
648 imageurl = this.getClass()
649 .getResource("/gnu/classpath/examples/icons/big-warning.png");
650 img = Toolkit.getDefaultToolkit().createImage(imageurl);
652 final Canvas ch = new Canvas()
654 public void paint (Graphics g)
656 g.drawImage(img, xs + 25, ys + 25, this);
658 Font font = new Font ("Serif", Font.PLAIN, 18);
659 g.setFont (font);
660 g.setXORMode (Color.red);
662 g.drawString("Hi Red!", xs + 15, ys + 10);
663 g.setColor (Color.blue);
664 g.drawLine (xs, ys, xs + 100, ys + 100);
668 ch.setSize(150, 150);
669 add(ch, "Center");
671 final ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
672 final Panel p = new Panel();
673 p.add(new Button("Stop"));
674 p.add(new Button("evil"));
675 p.add(new Button("hoarders"));
676 p.add(new Button("use"));
677 p.add(new Button("GNU!"));
679 sp.add(p);
680 add(sp, "South");
682 Panel east_panel = new Panel();
683 east_panel.setLayout(new GridLayout (0,1));
685 CheckboxGroup group = new CheckboxGroup();
686 Checkbox cb = new Checkbox("one", group, true);
687 east_panel.add(cb);
688 cb = new Checkbox("two", group, false);
689 east_panel.add(cb);
691 add(east_panel,"East");
693 final Button wb = new Button();
694 wb.setLabel("Hello World!");
695 wb.addActionListener(new ActionListener()
697 public void actionPerformed (ActionEvent e)
699 l.setText ("Hello World!");
701 final Dialog d = new Dialog(parent);
702 d.setLayout(new FlowLayout());
703 d.setModal(true);
704 Button b = new Button("foobar");
705 b.addMouseListener(new MouseAdapter()
707 public void mousePressed (MouseEvent me)
709 d.hide ();
712 d.add (b);
714 List ch = new List();
715 ch.add("Ding");
716 ch.add("September");
717 ch.add("Red");
718 ch.add("Quassia");
719 ch.add("Pterodactyl");
720 d.add(ch);
722 d.pack ();
723 d.show ();
727 wb.addMouseListener(new MouseAdapter()
729 public void mousePressed(MouseEvent e) {
730 xs++;
731 ys++;
732 ch.repaint ();
736 add(wb,"West");
738 pack();
739 show();
741 sp.setScrollPosition (10,0);
743 Toolkit t = Toolkit.getDefaultToolkit();
744 t.beep();
748 static class RoundRectWindow extends SubFrame
750 public void init ()
752 initted = true;
753 setTitle("RoundRect");
754 setLayout(new BorderLayout());
755 add(new DrawRoundRect(), "West");
756 Button cb = new Button ("Close");
757 cb.addActionListener(new ActionListener () {
758 public void actionPerformed (ActionEvent e) {
759 dispose();
762 add(cb, "Center");
763 add(new FillRoundRect(), "East");
764 pack();
767 static class DrawRoundRect extends Panel
770 public Dimension getPreferredSize()
772 return new Dimension(500, 500);
775 public void paint( Graphics g )
777 // left side
779 // rectangles should be identical
780 g.setColor(Color.red);
781 g.drawRect(50, 50, 300, 100);
782 g.setColor(Color.black);
783 g.drawRoundRect(50, 50, 300, 100, 0, 0);
785 // small round corners
786 g.setColor(Color.red);
787 g.drawRect(50, 200, 300, 100);
788 g.setColor(Color.black);
789 g.drawRoundRect(50, 200, 300, 100, 25, 25);
791 // round ends
792 g.setColor(Color.red);
793 g.drawRect(50, 350, 300, 100);
794 g.setColor(Color.black);
795 g.drawRoundRect(50, 350, 300, 100, 25, 100);
797 // right side
799 // circle only
800 g.setColor(Color.blue);
801 g.drawOval(375, 50, 100, 100);
803 // round rectangle should exactly cover circle
804 g.setColor(Color.blue);
805 g.drawOval(375, 200, 100, 100);
806 g.setColor(Color.black);
807 g.drawRoundRect(375, 200, 100, 100, 100, 100);
809 // round rectangle should look like a circle
810 g.setColor(Color.red);
811 g.drawRect(375, 350, 100, 100);
812 g.setColor(Color.black);
813 g.drawRoundRect(375, 350, 100, 100, 100, 100);
817 static class FillRoundRect extends Panel
820 public Dimension getPreferredSize()
822 return new Dimension(500, 500);
825 public void paint( Graphics g )
827 // left side
829 // rectangles should be identical
830 g.setColor(Color.red);
831 g.fillRect(50, 50, 300, 100);
832 g.setColor(Color.black);
833 g.fillRoundRect(50, 50, 300, 100, 0, 0);
835 // small round corners
836 g.setColor(Color.red);
837 g.fillRect(50, 200, 300, 100);
838 g.setColor(Color.black);
839 g.fillRoundRect(50, 200, 300, 100, 25, 25);
841 // round ends
842 g.setColor(Color.red);
843 g.fillRect(50, 350, 300, 100);
844 g.setColor(Color.black);
845 g.fillRoundRect(50, 350, 300, 100, 25, 100);
847 // right side
849 // circle only
850 g.setColor(Color.blue);
851 g.fillOval(375, 50, 100, 100);
853 // round rectangle should exactly cover circle
854 g.setColor(Color.blue);
855 g.fillOval(375, 200, 100, 100);
856 g.setColor(Color.black);
857 g.fillRoundRect(375, 200, 100, 100, 100, 100);
859 // round rectangle should look like a circle
860 g.setColor(Color.red);
861 g.fillRect(375, 350, 100, 100);
862 g.setColor(Color.black);
863 g.fillRoundRect(375, 350, 100, 100, 100, 100);
868 static class AnimationWindow extends SubFrame
870 AnimationApplet a;
871 public void init ()
873 initted = true;
874 setTitle("Animation");
875 Button cb = new Button ("Close");
876 cb.addActionListener(new ActionListener () {
877 public void actionPerformed (ActionEvent e)
879 if (a != null)
881 a.destroy();
882 dispose();
886 a = new AnimationApplet();
887 add(a, "Center");
888 add(cb, "South");
889 pack();
892 public void show()
894 super.show();
895 a.init();
896 a.run();