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;
23 import java.awt.BorderLayout;
24 import java.awt.Button;
25 import java.awt.Canvas;
26 import java.awt.Checkbox;
27 import java.awt.CheckboxGroup;
28 import java.awt.CheckboxMenuItem;
29 import java.awt.Choice;
30 import java.awt.Color;
31 import java.awt.Cursor;
32 import java.awt.Dialog;
33 import java.awt.Dimension;
34 import java.awt.DisplayMode;
35 import java.awt.FileDialog;
36 import java.awt.FlowLayout;
38 import java.awt.Frame;
39 import java.awt.Graphics;
40 import java.awt.GraphicsDevice;
41 import java.awt.GraphicsEnvironment;
42 import java.awt.GridLayout;
43 import java.awt.Image;
44 import java.awt.Insets;
45 import java.awt.Label;
48 import java.awt.MenuBar;
49 import java.awt.MenuItem;
50 import java.awt.MenuShortcut;
51 import java.awt.Panel;
52 import java.awt.ScrollPane;
53 import java.awt.TextField;
54 import java.awt.Toolkit;
55 import java.awt.Window;
56 import java.awt.datatransfer.DataFlavor;
57 import java.awt.datatransfer.StringSelection;
58 import java.awt.datatransfer.Transferable;
59 import java.awt.datatransfer.UnsupportedFlavorException;
60 import java.awt.dnd.DnDConstants;
61 import java.awt.dnd.DragGestureEvent;
62 import java.awt.dnd.DragGestureListener;
63 import java.awt.dnd.DragSource;
64 import java.awt.dnd.DragSourceContext;
65 import java.awt.dnd.DragSourceDragEvent;
66 import java.awt.dnd.DragSourceDropEvent;
67 import java.awt.dnd.DragSourceEvent;
68 import java.awt.dnd.DragSourceListener;
69 import java.awt.dnd.DropTarget;
70 import java.awt.dnd.DropTargetDragEvent;
71 import java.awt.dnd.DropTargetDropEvent;
72 import java.awt.dnd.DropTargetEvent;
73 import java.awt.dnd.DropTargetListener;
74 import java.awt.dnd.InvalidDnDOperationException;
75 import java.awt.event.ActionEvent;
76 import java.awt.event.ActionListener;
77 import java.awt.event.ItemEvent;
78 import java.awt.event.ItemListener;
79 import java.awt.event.MouseAdapter;
80 import java.awt.event.MouseEvent;
81 import java.awt.event.WindowAdapter;
82 import java.awt.event.WindowEvent;
84 import java.util.Enumeration;
85 import java.util.Hashtable;
86 import java.util.Vector;
90 public static void main(String args[])
92 MainWindow f = new MainWindow();
96 static interface SubWindow
101 static class PrettyPanel extends Panel
105 public PrettyPanel ()
107 myInsets = new Insets (10, 10, 10, 10);
109 public Insets getInsets ()
115 static abstract class PrettyFrame extends Frame
117 public PrettyFrame ()
119 ((BorderLayout) getLayout ()).setHgap (5);
120 ((BorderLayout) getLayout ()).setVgap (5);
123 public Insets getInsets()
125 Insets oldInsets = super.getInsets ();
126 return new Insets (oldInsets.top+10,
133 static abstract class SubFrame extends PrettyFrame implements SubWindow
135 boolean initted = false;
137 public void setVisible (boolean visible)
139 if (!initted && visible)
141 super.setVisible (visible);
145 static class MainWindow extends PrettyFrame implements ActionListener
152 void addSubWindow (String name, SubWindow w)
154 Button b = new Button (name);
155 b.addActionListener (this);
157 buttons.addElement (b);
163 MenuBar mb = new MenuBar ();
164 Menu menu = new Menu ("File");
165 Menu submenu = new Menu ("Testing", true);
166 submenu.add (new CheckboxMenuItem ("FooBar"));
167 submenu.add (new CheckboxMenuItem ("BarFoo"));
169 menu.add (new MenuItem("Orange"));
170 MenuItem quit = new MenuItem("Quit", new MenuShortcut('Q'));
171 quit.addActionListener(new ActionListener()
173 public void actionPerformed(ActionEvent e)
181 menu = new Menu("Edit", true);
182 menu.add(new MenuItem("Cut"));
183 menu.add(new MenuItem("Copy"));
184 menu.add(new MenuItem("Paste"));
187 Menu helpMenu = new Menu("Help");
188 helpMenu.add(new MenuItem("About"));
190 mb.setHelpMenu(helpMenu);
194 String version = System.getProperty("gnu.classpath.version");
195 add (new Label ("GNU Classpath " + version), "North");
197 closeButton = new Button ("Close");
198 closeButton.addActionListener (this);
199 closeButton.setFont (new Font ("Serif", Font.BOLD | Font.ITALIC, 18));
200 add (closeButton, "South");
202 windows = new Hashtable ();
203 buttons = new Vector ();
205 addSubWindow ("Buttons", new ButtonsWindow ());
206 addSubWindow ("Cursors", new CursorsWindow ());
207 addSubWindow ("Dialog", new DialogWindow (this));
208 addSubWindow ("File", new FileWindow (this));
209 addSubWindow ("Labels", new LabelWindow ());
210 addSubWindow ("List", new ListWindow ());
211 addSubWindow ("Radio Buttons", new RadioWindow ());
212 addSubWindow ("TextField", new TextFieldWindow ());
213 addSubWindow ("RandomTests", new TestWindow (this));
214 addSubWindow ("RoundRect", new RoundRectWindow ());
215 addSubWindow ("Animation", new AnimationWindow ());
216 addSubWindow ("Resolution", new ResolutionWindow ());
217 addSubWindow ("Fullscreen", new FullscreenWindow ());
218 addSubWindow ("Drag n' Drop", new DragDropWindow ());
220 Panel sp = new Panel();
221 PrettyPanel p = new PrettyPanel();
222 p.setLayout (new GridLayout (windows.size(), 1));
224 for (Enumeration e = buttons.elements (); e.hasMoreElements (); )
226 p.add ((Button) e.nextElement ());
232 setTitle ("AWT Demo");
236 public void actionPerformed (ActionEvent evt)
238 Button source = (Button) evt.getSource ();
240 if (source==closeButton)
246 Window w = (Window) windows.get (source);
251 if (w instanceof Dialog)
263 static class ButtonsWindow extends SubFrame implements ActionListener
265 Button b[] = new Button [9];
270 Panel p = new Panel ();
271 p.setLayout (new GridLayout (0, 3, 5, 5));
273 for (int i=0; i<9; i++)
275 b[i]=new Button ("button" + (i+1));
276 b[i].addActionListener (this);
291 Button cb = new Button ("close");
292 cb.addActionListener(new ActionListener () {
293 public void actionPerformed (ActionEvent e) {
298 setTitle ("Buttons");
302 public void actionPerformed (ActionEvent evt)
304 Button source = (Button) evt.getSource ();
306 for (int i = 0; i < 9; i++)
310 int i2 = ((i + 1) == 9) ? 0 : (i + 1);
311 if (b[i2].isVisible())
312 b[i2].setVisible(false);
314 b[i2].setVisible(true);
321 static class DialogWindow extends Dialog implements SubWindow
325 boolean initted = false;
327 public DialogWindow (Frame f)
333 addWindowListener (new WindowAdapter ()
335 public void windowClosing (WindowEvent e)
337 text.setVisible (false);
343 public void setVisible (boolean visible)
345 if (!initted && visible)
347 super.setVisible (visible);
359 text = new Label ("Dialog Test");
360 text.setAlignment (Label.CENTER);
363 text.setVisible (false);
365 Panel p = new PrettyPanel();
367 Button cb = new Button ("OK");
368 cb.addActionListener(new ActionListener () {
369 public void actionPerformed (ActionEvent e)
371 text.setVisible (false);
376 p.setLayout (new GridLayout (1, 3));
377 ((GridLayout) p.getLayout ()).setHgap (5);
378 ((GridLayout) p.getLayout ()).setVgap (5);
381 Button toggle = new Button ("Toggle");
384 toggle.addActionListener(new ActionListener () {
385 public void actionPerformed (ActionEvent e)
387 if (text.isVisible ())
388 text.setVisible (false);
390 text.setVisible (true);
395 Button subdlg = new Button ("SubDialog");
398 subdlg.addActionListener(new ActionListener () {
399 public void actionPerformed (ActionEvent e)
401 DialogWindow sw = new DialogWindow (parent);
412 static class CursorsWindow extends SubFrame implements ItemListener
419 cursorChoice = new Choice();
420 cursorChoice.add ("Default");
421 cursorChoice.add ("Crosshair");
422 cursorChoice.add ("Text");
423 cursorChoice.add ("Wait");
424 cursorChoice.add ("Southwest Resize");
425 cursorChoice.add ("Southeast Resize");
426 cursorChoice.add ("Northwest Resize");
427 cursorChoice.add ("Northeast Resize");
428 cursorChoice.add ("North Resize");
429 cursorChoice.add ("South Resize");
430 cursorChoice.add ("West Resize");
431 cursorChoice.add ("East Resize");
432 cursorChoice.add ("Hand");
433 cursorChoice.add ("Move");
435 cursorChoice.addItemListener(this);
437 add (cursorChoice, "North");
439 cursorCanvas = new Canvas ()
441 public void paint (Graphics g)
443 Dimension d = this.getSize();
444 g.setColor(Color.white);
445 g.fillRect(0, 0, d.width, d.height/2);
446 g.setColor(Color.black);
447 g.fillRect(0, d.height/2, d.width, d.height/2);
448 g.setColor(this.getBackground());
449 g.fillRect(d.width/3, d.height/3, d.width/3,
454 cursorCanvas.setSize (80,80);
456 add (cursorCanvas, "Center");
458 Button cb = new Button ("Close");
459 cb.addActionListener(new ActionListener () {
460 public void actionPerformed (ActionEvent e) {
466 setTitle ("Cursors");
470 public void itemStateChanged (ItemEvent e)
472 int index = cursorChoice.getSelectedIndex();
473 cursorCanvas.setCursor(Cursor.getPredefinedCursor(index));
477 static class TextFieldWindow extends SubFrame implements ItemListener
479 Checkbox editable, visible, sensitive;
485 text = new TextField ("hello world");
488 Panel p = new Panel();
489 p.setLayout (new GridLayout (3, 1));
490 ((GridLayout) p.getLayout ()).setHgap (5);
491 ((GridLayout) p.getLayout ()).setVgap (5);
493 editable = new Checkbox("Editable", true);
495 editable.addItemListener (this);
497 visible = new Checkbox("Visible", true);
499 visible.addItemListener (this);
501 sensitive = new Checkbox("Sensitive", true);
503 sensitive.addItemListener (this);
507 Button cb = new Button ("Close");
508 cb.addActionListener(new ActionListener () {
509 public void actionPerformed (ActionEvent e) {
515 setTitle ("TextField");
519 public void itemStateChanged (ItemEvent e)
523 if (e.getStateChange () == ItemEvent.DESELECTED)
525 if (e.getSource() == editable)
526 text.setEditable (on);
527 if (e.getSource() == visible)
529 text.setEchoChar ((char) 0);
531 text.setEchoChar ('*');
532 if (e.getSource() == sensitive)
533 text.setEnabled (on);
538 static class FileWindow extends FileDialog implements SubWindow
540 boolean initted = false;
542 public FileWindow (MainWindow mw)
547 public void setVisible (boolean visible)
549 if (!initted && visible)
551 super.setVisible (visible);
560 static class LabelWindow extends SubFrame
566 Panel p = new Panel();
567 p.setLayout (new GridLayout (3, 1));
568 ((GridLayout) p.getLayout ()).setHgap (5);
569 ((GridLayout) p.getLayout ()).setVgap (5);
571 p.add (new Label ("left justified label", Label.LEFT));
572 p.add (new Label ("center justified label", Label.CENTER));
573 p.add (new Label ("right justified label", Label.RIGHT));
577 Button cb = new Button ("Close");
578 cb.addActionListener(new ActionListener () {
579 public void actionPerformed (ActionEvent e) {
590 static class ListWindow extends SubFrame
596 Panel p = new Panel ();
597 p.setLayout (new GridLayout (3, 1));
599 List l = new List (5, true);
600 for (int i = 0; i < 10; i++)
601 l.add ("List item " + i);
607 Button cb = new Button ("Close");
608 cb.addActionListener(new ActionListener () {
609 public void actionPerformed (ActionEvent e) {
621 static class RadioWindow extends SubFrame
627 Panel p = new Panel();
628 p.setLayout (new GridLayout (3, 1));
629 ((GridLayout) p.getLayout ()).setHgap (5);
630 ((GridLayout) p.getLayout ()).setVgap (5);
632 final CheckboxGroup cg = new CheckboxGroup();
633 final Checkbox[] boxes = new Checkbox[3];
634 for (int i = 0; i < 3; ++i)
636 boxes[i] = new Checkbox("button" + i, cg, i == 0);
643 p.setLayout (new GridLayout (1, 3));
644 ((GridLayout) p.getLayout ()).setHgap (5);
645 ((GridLayout) p.getLayout ()).setVgap (5);
647 for (int i = 0; i < 3; ++i)
650 Button tweak = new Button ("Set " + i);
651 tweak.addActionListener(new ActionListener ()
653 public void actionPerformed (ActionEvent e)
655 cg.setSelectedCheckbox(boxes[val]);
663 Button cb = new Button ("Close");
664 cb.addActionListener(new ActionListener () {
665 public void actionPerformed (ActionEvent e) {
671 setTitle ("Radio Buttons");
676 static class TestWindow extends SubFrame
678 static int xs = 5, ys = 5;
681 public TestWindow(Frame f)
690 addWindowListener (new WindowAdapter ()
692 public void windowClosing (WindowEvent e)
698 Panel pan = new Panel();
700 final Label l = new Label ("Pithy Message:");
701 l.setCursor (Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR));
704 TextField tf = new TextField("Hello world!");
710 imageurl = this.getClass()
711 .getResource("/gnu/classpath/examples/icons/big-warning.png");
712 img = Toolkit.getDefaultToolkit().createImage(imageurl);
714 final Canvas ch = new Canvas()
716 public void paint (Graphics g)
718 g.drawImage(img, xs + 25, ys + 25, this);
720 Font font = new Font ("Serif", Font.PLAIN, 18);
722 g.setXORMode (Color.red);
724 g.drawString("Hi Red!", xs + 15, ys + 10);
725 g.setColor (Color.blue);
726 g.drawLine (xs, ys, xs + 100, ys + 100);
730 ch.setSize(150, 150);
733 final ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
734 final Panel p = new Panel();
735 p.add(new Button("Stop"));
736 p.add(new Button("evil"));
737 p.add(new Button("hoarders"));
738 p.add(new Button("use"));
739 p.add(new Button("GNU!"));
744 Panel east_panel = new Panel();
745 east_panel.setLayout(new GridLayout (0,1));
747 CheckboxGroup group = new CheckboxGroup();
748 Checkbox cb = new Checkbox("one", group, true);
750 cb = new Checkbox("two", group, false);
753 add(east_panel,"East");
755 final Button wb = new Button();
756 wb.setLabel("Hello World!");
757 wb.addActionListener(new ActionListener()
759 public void actionPerformed (ActionEvent e)
761 l.setText ("Hello World!");
763 final Dialog d = new Dialog(parent);
764 d.setLayout(new FlowLayout());
766 Button b = new Button("foobar");
767 b.addMouseListener(new MouseAdapter()
769 public void mousePressed (MouseEvent me)
776 List ch = new List();
781 ch.add("Pterodactyl");
789 wb.addMouseListener(new MouseAdapter()
791 public void mousePressed(MouseEvent e) {
803 sp.setScrollPosition (10,0);
805 Toolkit t = Toolkit.getDefaultToolkit();
810 static class ResolutionWindow extends SubFrame
812 GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
818 setTitle("Change Screen Resolution");
819 final List list = new List();
820 DisplayMode[] modes = gd.getDisplayModes();
822 for (int i=0;i<modes.length;i++ )
823 list.add(modes[i].getWidth() + "x"
824 + modes[i].getHeight()
825 + ((modes[i].getBitDepth() != DisplayMode.BIT_DEPTH_MULTI)
826 ? "x" + modes[i].getBitDepth() + "bpp"
828 + ((modes[i].getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN)
829 ? "@" + modes[i].getRefreshRate() + "Hz"
832 ActionListener al = new ActionListener()
834 public void actionPerformed(ActionEvent ae)
836 int i = list.getSelectedIndex();
837 gd.setDisplayMode(gd.getDisplayModes()[i]);
841 Button b = new Button("Switch");
842 Button c = new Button("Close");
844 list.addActionListener(al);
845 b.addActionListener(al);
847 c.addActionListener(new ActionListener () {
848 public void actionPerformed (ActionEvent e) {
853 setLayout(new GridLayout(3, 1, 5, 5));
862 static class DragDropWindow
864 implements ActionListener, DropTargetListener
866 DragLabel source = new DragLabel("Drag and drop me to the following Button",
869 Button target = new Button();
873 source.setForeground(Color.red);
874 add(source, BorderLayout.NORTH);
876 target.addActionListener(this);
877 add(target, BorderLayout.SOUTH);
879 new DropTarget(target, DnDConstants.ACTION_COPY_OR_MOVE, this);
886 public void actionPerformed(ActionEvent e)
888 Button b = (Button) e.getSource();
892 public void dragEnter(DropTargetDragEvent e)
896 public void dragExit(DropTargetEvent e)
900 public void dragOver(DropTargetDragEvent e)
904 public void drop(DropTargetDropEvent e)
908 Transferable t = e.getTransferable();
910 if (e.isDataFlavorSupported(DataFlavor.stringFlavor))
912 e.acceptDrop(e.getDropAction());
915 s = (String) t.getTransferData(DataFlavor.stringFlavor);
919 e.dropComplete(true);
924 catch (java.io.IOException e2)
927 catch (UnsupportedFlavorException e2)
932 public void dropActionChanged(DropTargetDragEvent e)
938 implements DragGestureListener, DragSourceListener
940 private DragSource ds = DragSource.getDefaultDragSource();
942 public DragLabel(String s, int alignment)
945 int action = DnDConstants.ACTION_COPY_OR_MOVE;
946 ds.createDefaultDragGestureRecognizer(this, action, this);
949 public void dragGestureRecognized(DragGestureEvent e)
953 Transferable t = new StringSelection(getText());
954 e.startDrag(DragSource.DefaultCopyNoDrop, t, this);
956 catch (InvalidDnDOperationException e2)
958 System.out.println(e2);
962 public void dragDropEnd(DragSourceDropEvent e)
964 if (e.getDropSuccess() == false)
967 int action = e.getDropAction();
968 if ((action & DnDConstants.ACTION_MOVE) != 0)
972 public void dragEnter(DragSourceDragEvent e)
974 DragSourceContext ctx = e.getDragSourceContext();
976 int action = e.getDropAction();
977 if ((action & DnDConstants.ACTION_COPY) != 0)
978 ctx.setCursor(DragSource.DefaultCopyDrop);
980 ctx.setCursor(DragSource.DefaultCopyNoDrop);
983 public void dragExit(DragSourceEvent e)
987 public void dragOver(DragSourceDragEvent e)
991 public void dropActionChanged(DragSourceDragEvent e)
997 static class FullscreenWindow extends SubFrame
999 GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
1005 setTitle("Fullscreen Exclusive Mode");
1007 ActionListener al = new ActionListener()
1009 public void actionPerformed(ActionEvent ae)
1011 if (gd.getFullScreenWindow() == FullscreenWindow.this)
1012 gd.setFullScreenWindow(null);
1014 gd.setFullScreenWindow(FullscreenWindow.this);
1018 Button b = new Button("Toggle Fullscreen");
1019 Button c = new Button("Close");
1021 b.addActionListener(al);
1023 c.addActionListener(new ActionListener () {
1024 public void actionPerformed (ActionEvent e) {
1025 gd.setFullScreenWindow(null);
1030 setLayout(new GridLayout(3, 1, 5, 5));
1038 static class RoundRectWindow extends SubFrame
1043 setTitle("RoundRect");
1044 setLayout(new BorderLayout());
1045 add(new DrawRoundRect(), "West");
1046 Button cb = new Button ("Close");
1047 cb.addActionListener(new ActionListener () {
1048 public void actionPerformed (ActionEvent e) {
1053 add(new FillRoundRect(), "East");
1057 static class DrawRoundRect extends Panel
1060 public Dimension getPreferredSize()
1062 return new Dimension(500, 500);
1065 public void paint( Graphics g )
1069 // rectangles should be identical
1070 g.setColor(Color.red);
1071 g.drawRect(50, 50, 300, 100);
1072 g.setColor(Color.black);
1073 g.drawRoundRect(50, 50, 300, 100, 0, 0);
1075 // small round corners
1076 g.setColor(Color.red);
1077 g.drawRect(50, 200, 300, 100);
1078 g.setColor(Color.black);
1079 g.drawRoundRect(50, 200, 300, 100, 25, 25);
1082 g.setColor(Color.red);
1083 g.drawRect(50, 350, 300, 100);
1084 g.setColor(Color.black);
1085 g.drawRoundRect(50, 350, 300, 100, 25, 100);
1090 g.setColor(Color.blue);
1091 g.drawOval(375, 50, 100, 100);
1093 // round rectangle should exactly cover circle
1094 g.setColor(Color.blue);
1095 g.drawOval(375, 200, 100, 100);
1096 g.setColor(Color.black);
1097 g.drawRoundRect(375, 200, 100, 100, 100, 100);
1099 // round rectangle should look like a circle
1100 g.setColor(Color.red);
1101 g.drawRect(375, 350, 100, 100);
1102 g.setColor(Color.black);
1103 g.drawRoundRect(375, 350, 100, 100, 100, 100);
1107 static class FillRoundRect extends Panel
1110 public Dimension getPreferredSize()
1112 return new Dimension(500, 500);
1115 public void paint( Graphics g )
1119 // rectangles should be identical
1120 g.setColor(Color.red);
1121 g.fillRect(50, 50, 300, 100);
1122 g.setColor(Color.black);
1123 g.fillRoundRect(50, 50, 300, 100, 0, 0);
1125 // small round corners
1126 g.setColor(Color.red);
1127 g.fillRect(50, 200, 300, 100);
1128 g.setColor(Color.black);
1129 g.fillRoundRect(50, 200, 300, 100, 25, 25);
1132 g.setColor(Color.red);
1133 g.fillRect(50, 350, 300, 100);
1134 g.setColor(Color.black);
1135 g.fillRoundRect(50, 350, 300, 100, 25, 100);
1140 g.setColor(Color.blue);
1141 g.fillOval(375, 50, 100, 100);
1143 // round rectangle should exactly cover circle
1144 g.setColor(Color.blue);
1145 g.fillOval(375, 200, 100, 100);
1146 g.setColor(Color.black);
1147 g.fillRoundRect(375, 200, 100, 100, 100, 100);
1149 // round rectangle should look like a circle
1150 g.setColor(Color.red);
1151 g.fillRect(375, 350, 100, 100);
1152 g.setColor(Color.black);
1153 g.fillRoundRect(375, 350, 100, 100, 100, 100);
1158 static class AnimationWindow extends SubFrame
1164 setTitle("Animation");
1165 Button cb = new Button ("Close");
1166 cb.addActionListener(new ActionListener () {
1167 public void actionPerformed (ActionEvent e)
1176 a = new AnimationApplet();