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)
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
21 package gnu
.classpath
.examples
.awt
;
25 import java
.awt
.event
.*;
31 public static void main(String args
[])
33 MainWindow f
= new MainWindow();
37 static interface SubWindow
42 static class PrettyPanel
extends Panel
48 myInsets
= new Insets (10, 10, 10, 10);
50 public Insets
getInsets ()
56 static abstract class PrettyFrame
extends Frame
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,
74 static abstract class SubFrame
extends PrettyFrame
implements SubWindow
76 boolean initted
= false;
78 public void setVisible (boolean visible
)
80 if (!initted
&& visible
)
82 super.setVisible (visible
);
86 static class MainWindow
extends PrettyFrame
implements ActionListener
93 void addSubWindow (String name
, SubWindow w
)
95 Button b
= new Button (name
);
96 b
.addActionListener (this);
98 buttons
.addElement (b
);
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"));
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
)
122 menu
= new Menu("Edit", true);
123 menu
.add(new MenuItem("Cut"));
124 menu
.add(new MenuItem("Copy"));
125 menu
.add(new MenuItem("Paste"));
128 Menu helpMenu
= new Menu("Help");
129 helpMenu
.add(new MenuItem("About"));
131 mb
.setHelpMenu(helpMenu
);
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 ());
170 setTitle ("AWT Demo");
174 public void actionPerformed (ActionEvent evt
)
176 Button source
= (Button
) evt
.getSource ();
178 if (source
==closeButton
)
184 Window w
= (Window
) windows
.get (source
);
189 if (w
instanceof Dialog
)
201 static class ButtonsWindow
extends SubFrame
implements ActionListener
203 Button b
[] = new Button
[9];
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);
229 Button cb
= new Button ("close");
230 cb
.addActionListener(new ActionListener () {
231 public void actionPerformed (ActionEvent e
) {
236 setTitle ("Buttons");
240 public void actionPerformed (ActionEvent evt
)
242 Button source
= (Button
) evt
.getSource ();
244 for (int i
= 0; i
< 9; i
++)
248 int i2
= ((i
+ 1) == 9) ?
0 : (i
+ 1);
249 if (b
[i2
].isVisible())
250 b
[i2
].setVisible(false);
252 b
[i2
].setVisible(true);
259 static class DialogWindow
extends Dialog
implements SubWindow
263 boolean initted
= false;
265 public DialogWindow (Frame f
)
271 addWindowListener (new WindowAdapter ()
273 public void windowClosing (WindowEvent e
)
275 text
.setVisible (false);
281 public void setVisible (boolean visible
)
283 if (!initted
&& visible
)
285 super.setVisible (visible
);
297 text
= new Label ("Dialog Test");
298 text
.setAlignment (Label
.CENTER
);
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);
314 p
.setLayout (new GridLayout (1, 3));
315 ((GridLayout
) p
.getLayout ()).setHgap (5);
316 ((GridLayout
) p
.getLayout ()).setVgap (5);
319 Button toggle
= new Button ("Toggle");
322 toggle
.addActionListener(new ActionListener () {
323 public void actionPerformed (ActionEvent e
)
325 if (text
.isVisible ())
326 text
.setVisible (false);
328 text
.setVisible (true);
333 Button subdlg
= new Button ("SubDialog");
336 subdlg
.addActionListener(new ActionListener () {
337 public void actionPerformed (ActionEvent e
)
339 DialogWindow sw
= new DialogWindow (parent
);
350 static class CursorsWindow
extends SubFrame
implements ItemListener
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,
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
) {
404 setTitle ("Cursors");
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
;
423 text
= new TextField ("hello world");
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);
433 editable
.addItemListener (this);
435 visible
= new Checkbox("Visible", true);
437 visible
.addItemListener (this);
439 sensitive
= new Checkbox("Sensitive", true);
441 sensitive
.addItemListener (this);
445 Button cb
= new Button ("Close");
446 cb
.addActionListener(new ActionListener () {
447 public void actionPerformed (ActionEvent e
) {
453 setTitle ("TextField");
457 public void itemStateChanged (ItemEvent e
)
461 if (e
.getStateChange () == ItemEvent
.DESELECTED
)
463 if (e
.getSource() == editable
)
464 text
.setEditable (on
);
465 if (e
.getSource() == visible
)
467 text
.setEchoChar ((char) 0);
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
)
485 public void setVisible (boolean visible
)
487 if (!initted
&& visible
)
489 super.setVisible (visible
);
498 static class LabelWindow
extends SubFrame
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
));
515 Button cb
= new Button ("Close");
516 cb
.addActionListener(new ActionListener () {
517 public void actionPerformed (ActionEvent e
) {
528 static class ListWindow
extends SubFrame
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
);
545 Button cb
= new Button ("Close");
546 cb
.addActionListener(new ActionListener () {
547 public void actionPerformed (ActionEvent e
) {
559 static class RadioWindow
extends SubFrame
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);
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
)
588 Button tweak
= new Button ("Set " + i
);
589 tweak
.addActionListener(new ActionListener ()
591 public void actionPerformed (ActionEvent e
)
593 cg
.setSelectedCheckbox(boxes
[val
]);
601 Button cb
= new Button ("Close");
602 cb
.addActionListener(new ActionListener () {
603 public void actionPerformed (ActionEvent e
) {
609 setTitle ("Radio Buttons");
614 static class TestWindow
extends SubFrame
616 static int xs
= 5, ys
= 5;
619 public TestWindow(Frame f
)
628 addWindowListener (new WindowAdapter ()
630 public void windowClosing (WindowEvent e
)
636 Panel pan
= new Panel();
638 final Label l
= new Label ("Pithy Message:");
639 l
.setCursor (Cursor
.getPredefinedCursor (Cursor
.WAIT_CURSOR
));
642 TextField tf
= new TextField("Hello world!");
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);
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);
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!"));
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);
688 cb
= new Checkbox("two", group
, false);
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());
704 Button b
= new Button("foobar");
705 b
.addMouseListener(new MouseAdapter()
707 public void mousePressed (MouseEvent me
)
714 List ch
= new List();
719 ch
.add("Pterodactyl");
727 wb
.addMouseListener(new MouseAdapter()
729 public void mousePressed(MouseEvent e
) {
741 sp
.setScrollPosition (10,0);
743 Toolkit t
= Toolkit
.getDefaultToolkit();
748 static class RoundRectWindow
extends SubFrame
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
) {
763 add(new FillRoundRect(), "East");
767 static class DrawRoundRect
extends Panel
770 public Dimension
getPreferredSize()
772 return new Dimension(500, 500);
775 public void paint( Graphics g
)
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);
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);
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
)
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);
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);
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
874 setTitle("Animation");
875 Button cb
= new Button ("Close");
876 cb
.addActionListener(new ActionListener () {
877 public void actionPerformed (ActionEvent e
)
886 a
= new AnimationApplet();