home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / VisualTest.java < prev    next >
Text File  |  1996-05-03  |  13KB  |  445 lines

  1. /*
  2.  * @(#)VisualTest.java    1.10 95/11/07 Sami Shaio
  3.  *
  4.  * Modified: Vijay Srinivasan
  5.  * Date: 11/06/95
  6.  * Comment: app to applet conversion.
  7.  *
  8.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  9.  *
  10.  * Permission to use, copy, modify, and distribute this software
  11.  * and its documentation for NON-COMMERCIAL purposes and without
  12.  * fee is hereby granted provided that this copyright notice
  13.  * appears in all copies. Please refer to the file "copyright.html"
  14.  * for further important copyright and licensing information.
  15.  *
  16.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  17.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  18.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  19.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  20.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  21.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  22.  */
  23. import java.awt.*;
  24. import java.applet.*;
  25.  
  26. class VTest extends Frame {
  27.     boolean    inReshape = false;
  28.     Menu    componentMenu;
  29.     Menu    backgroundMenu;
  30.     Menu    foregroundMenu;
  31.     Menu    shapeMenu;
  32.     Menu    sizeMenu;
  33.     Menu    fontMenu;
  34.     Menu    enableMenu;
  35.     Menu    familyMenu;
  36.     Menu    containerMenu;
  37.  
  38.     int        currentSize = 10;
  39.     Font    currentFont;
  40.     boolean    enableComponents = true;
  41.     Color    currentForeground;
  42.     Color    currentBackground;
  43.     Container    currentContainer;
  44.     Component    component;
  45.     Font    font10;
  46.     Font    font14;
  47.     Font    font24;
  48.     Font    font36;
  49.  
  50.     public VTest() {
  51.     super("VTest");
  52.  
  53.     MenuBar mb = new MenuBar();
  54.     currentContainer = this;
  55.     componentMenu = new Menu("Component");
  56.     componentMenu.add(new MenuItem("Button"));
  57.     componentMenu.add(new MenuItem("Checkbox"));
  58.     componentMenu.add(new MenuItem("Choice"));
  59.     componentMenu.add(new MenuItem("Label"));
  60.     componentMenu.add(new MenuItem("List"));
  61.     componentMenu.add(new MenuItem("Panel"));
  62.     componentMenu.add(new MenuItem("TextArea"));
  63.     componentMenu.add(new MenuItem("TextField"));
  64.     componentMenu.add(new MenuItem("HScrollbar"));
  65.     componentMenu.add(new MenuItem("VScrollbar"));
  66.     mb.add(componentMenu);
  67.  
  68.     enableMenu = new Menu("Enable/Disable");
  69.     enableMenu.add(new MenuItem("Enable"));
  70.     enableMenu.add(new MenuItem("Disable"));
  71.     mb.add(enableMenu);
  72.  
  73.     fontMenu = new Menu("Font");
  74.     familyMenu = new Menu("Family");
  75.     familyMenu.add(new MenuItem("Courier"));
  76.     familyMenu.add(new MenuItem("Dialog"));
  77.     familyMenu.add(new MenuItem("TimesRoman"));
  78.     familyMenu.add(new MenuItem("Helvetica"));
  79.     familyMenu.add(new MenuItem("Symbol"));
  80.     fontMenu.add(familyMenu);
  81.  
  82.     sizeMenu = new Menu("Size");
  83.     sizeMenu.add(new MenuItem("10"));
  84.     font10 = new Font("Helvetica", Font.PLAIN, 10);
  85.     sizeMenu.add(new MenuItem("14"));
  86.     font14 = new Font("Helvetica", Font.PLAIN, 14);
  87.     sizeMenu.add(new MenuItem("24"));
  88.     font24 = new Font("Helvetica", Font.PLAIN, 24);
  89.     sizeMenu.add(new MenuItem("36"));
  90.     font36 = new Font("Helvetica", Font.PLAIN, 36);
  91.     fontMenu.add(sizeMenu);
  92.  
  93.     mb.add(fontMenu);
  94.  
  95.     shapeMenu = new Menu("Move/Reshape");
  96.     shapeMenu.add(new CheckboxMenuItem("Move"));
  97.     shapeMenu.add(new CheckboxMenuItem("Reshape"));
  98.     mb.add(shapeMenu);
  99.  
  100.     foregroundMenu = new Menu("Foreground");
  101.     foregroundMenu.add(new CheckboxMenuItem("default"));
  102.     foregroundMenu.add(new CheckboxMenuItem("red"));
  103.     foregroundMenu.add(new CheckboxMenuItem("green"));
  104.     foregroundMenu.add(new CheckboxMenuItem("blue"));
  105.     mb.add(foregroundMenu);
  106.  
  107.     backgroundMenu = new Menu("Background");
  108.     backgroundMenu.add(new CheckboxMenuItem("default"));
  109.     backgroundMenu.add(new CheckboxMenuItem("red"));
  110.     backgroundMenu.add(new CheckboxMenuItem("green"));
  111.     backgroundMenu.add(new CheckboxMenuItem("blue"));
  112.     mb.add(backgroundMenu);
  113.  
  114.     containerMenu = new Menu("Container");
  115.     containerMenu.add(new MenuItem("FlowLayout"));
  116.     containerMenu.add(new MenuItem("GridLayout02"));
  117.     containerMenu.add(new MenuItem("GridLayout20"));
  118.     containerMenu.add(new MenuItem("GridLayout03"));
  119.     containerMenu.add(new MenuItem("GridLayout30"));
  120.     containerMenu.add(new MenuItem("BorderLayout"));
  121.     mb.add(containerMenu);
  122.  
  123.     setMenuBar(mb);
  124.     setLayout(null);
  125.  
  126.     currentFont = font10;
  127.     currentForeground = getForeground();
  128.     currentBackground = getBackground();
  129.     enableComponents = true;
  130.     resize(500, 300);
  131.     show();
  132.     }
  133.  
  134.     public boolean handleEvent(Event e) {
  135.     switch (e.id) {
  136.       case Event.WINDOW_DESTROY:
  137.         System.exit(0);
  138.         return true;
  139.       case Event.MOUSE_DOWN:
  140.         currentContainer = this;
  141.         setCurrentComponent(e.x, e.y);
  142.         /* fall into next case */
  143.       case Event.MOUSE_DRAG:
  144.         if (component != null) {
  145.         if (inReshape) {
  146.             Rectangle bounds = component.bounds();
  147.             component.resize(Math.abs(e.x-bounds.x), Math.abs(e.y-bounds.y));
  148.             component.validate();
  149.         } else {
  150.             component.move(e.x, e.y);
  151.         }
  152.         }
  153.         return true;
  154.       case Event.MOUSE_UP:
  155.         currentContainer.validate();
  156.         return true;
  157.       default:
  158.         return super.handleEvent(e);
  159.     }
  160.     }
  161.  
  162.     void setAttributes(Component c) {
  163.     if (c instanceof Container) {
  164.         return;
  165.     }
  166.     c.setForeground(currentForeground);
  167.     c.setBackground(currentBackground);
  168.     c.setFont(currentFont);
  169.     if (enableComponents) {
  170.         c.enable();
  171.     } else {
  172.         c.disable();
  173.     }
  174.     }
  175.  
  176.     int computeDistance(int x, int y, Rectangle r) {
  177.     int mx;
  178.     int my;
  179.  
  180.     mx = x - (r.x + (r.width / 2));
  181.     my = y - (r.y + (r.height / 2));
  182.  
  183.     return (mx*mx) + (my*my);
  184.     }
  185.  
  186.     void setCurrentComponent(int x, int y) {
  187.     int n = countComponents();
  188.     int distance = -1;
  189.  
  190.     for (int i=0; i<n; i++) {
  191.         Component c = getComponent(i);
  192.         Rectangle b = c.bounds();
  193.         int       d;
  194.         
  195.         d = computeDistance(x, y, b);
  196.         if (distance == -1 || d < distance) {
  197.         distance = d;
  198.         component = c;
  199.         }
  200.     }
  201.     }
  202.  
  203.     void setAttributes() {
  204.     int n = countComponents();
  205.  
  206.     for (int i=0; i < n; i++) {
  207.         setAttributes(getComponent(i));
  208.     }
  209.     }
  210.  
  211.     public boolean action(Event e, Object arg) {
  212.     if (e.target instanceof MenuItem) {
  213.         Menu menu = (Menu)(((MenuItem)e.target).getParent());
  214.         String label = (String)arg;
  215.  
  216.         if (menu == backgroundMenu) {
  217.         if (label.equals("red")) {
  218.             currentBackground = Color.red;
  219.         } else if (label.equals("green")) {
  220.             currentBackground = Color.green;
  221.         } else if (label.equals("blue")) {
  222.             currentBackground = Color.blue;
  223.         } else if (label.equals("default")) {
  224.             currentBackground = Color.lightGray;
  225.         }
  226.         } else if (menu == foregroundMenu) {
  227.         if (label.equals("red")) {
  228.             currentForeground = Color.red.darker();
  229.         } else if (label.equals("green")) {
  230.             currentForeground = Color.green.darker();
  231.         } else if (label.equals("blue")) {
  232.             currentForeground = Color.blue.darker();
  233.         } else if (label.equals("default")) {
  234.             currentForeground = Color.black;
  235.         }
  236.         } else if (menu == shapeMenu) {
  237.         if (label.equals("Move")) {
  238.             inReshape = false;
  239.         } else if (label.equals("Reshape")) {
  240.             inReshape = true;
  241.         }
  242.         } else if (menu == sizeMenu) {
  243.         if (label.equals("10")) {
  244.             currentFont = font10;
  245.         } else if (label.equals("14")) {
  246.             currentFont = font14;
  247.         } else if (label.equals("24")) {
  248.             currentFont = font24;
  249.         } else if (label.equals("36")) {
  250.             currentFont = font36;
  251.         }
  252.         } else if (menu == familyMenu) {
  253.         font10 = new Font(label, Font.PLAIN, 10);
  254.         font14 = new Font(label, Font.PLAIN, 14);
  255.         font24 = new Font(label, Font.PLAIN, 24);
  256.         font36 = new Font(label, Font.PLAIN, 36);
  257.         switch (currentSize) {
  258.           case 10:
  259.           default:
  260.             currentFont = font10;
  261.             break;
  262.           case 14:
  263.             currentFont = font14;
  264.             break;
  265.           case 24:
  266.             currentFont = font24;
  267.             break;
  268.           case 36:
  269.             currentFont = font36;
  270.             break;
  271.         }
  272.         } else if (menu == enableMenu) {
  273.         if (label.equals("Enable")) {
  274.             enableComponents = true;
  275.         } else if (label.equals("Disable")) {
  276.             enableComponents = false;
  277.         }
  278.         } else if (menu == componentMenu) {
  279.         Component component;
  280.  
  281.         if (label.equalsIgnoreCase("Button")) {
  282.             component = new Button("Button");
  283.         } else if (label.equalsIgnoreCase("Label")) {
  284.             component = new Label("label");
  285.         } else if (label.equalsIgnoreCase("TextField")) {
  286.             component = new TextField("textfield");
  287.         } else if (label.equalsIgnoreCase("Choice")) {
  288.             component = new Choice();
  289.             ((Choice)component).addItem("Choice");
  290.         } else if (label.equalsIgnoreCase("List")) {
  291.             component = new List(4, false);
  292.             ((List)component).addItem("List1");
  293.             ((List)component).addItem("List2");
  294.             ((List)component).addItem("List3");
  295.             ((List)component).addItem("List4");
  296.             ((List)component).addItem("List5");
  297.             currentContainer.add(component);
  298.         } else if (label.equalsIgnoreCase("TextArea")) {
  299.             component = new TextArea(5, 15);
  300.             ((TextArea)component).setText("TextArea");
  301.         } else if (label.equalsIgnoreCase("Checkbox")) {
  302.             component = new Checkbox("Checkbox");
  303.         } else if (label.equalsIgnoreCase("Panel")) {
  304.             component = new VPanel(this);
  305.         } else if (label.equalsIgnoreCase("HScrollbar")) {
  306.             component = new Scrollbar(Scrollbar.HORIZONTAL);
  307.         } else if (label.equalsIgnoreCase("VScrollbar")) {
  308.             component = new Scrollbar(Scrollbar.VERTICAL);
  309.         } else {
  310.             component = new Button("Button");
  311.         }
  312.         if (! (component instanceof Container)) {
  313.             Dimension d = component.preferredSize();
  314.             component.reshape(10, 10, d.width, d.height);
  315.         }
  316.         currentContainer.add(component);
  317.         currentContainer.validate();
  318.         } else if (menu == containerMenu) {
  319.         if (currentContainer != this) {
  320.             if (label.equalsIgnoreCase("FlowLayout")) {
  321.             currentContainer.setLayout(new FlowLayout());
  322.             } else if (label.equalsIgnoreCase("GridLayout02")) {
  323.             currentContainer.setLayout(new GridLayout(0,2));
  324.             } else if (label.equalsIgnoreCase("GridLayout20")) {
  325.             currentContainer.setLayout(new GridLayout(2,0));
  326.             } else if (label.equalsIgnoreCase("GridLayout03")) {
  327.             currentContainer.setLayout(new GridLayout(0,3));
  328.             } else if (label.equalsIgnoreCase("GridLayout30")) {
  329.             currentContainer.setLayout(new GridLayout(3, 0));
  330.             } else if (label.equalsIgnoreCase("BorderLayout")) {
  331.             currentContainer.setLayout(new BorderLayout());
  332.             Component comp1;
  333.             Component comp2;
  334.             Component comp3;
  335.             Component comp4;
  336.             Component comp5;
  337.             switch (currentContainer.countComponents()) {
  338.               case 1:
  339.                 comp1 = currentContainer.getComponent(0);
  340.                 currentContainer.remove(comp1);
  341.                 currentContainer.add("Center", comp1);
  342.                 break;
  343.               case 2:
  344.                 comp1 = currentContainer.getComponent(0);
  345.                 comp2 = currentContainer.getComponent(1);
  346.  
  347.                 currentContainer.remove(comp1);
  348.                 currentContainer.remove(comp2);
  349.  
  350.                 currentContainer.add("North", comp1);
  351.                 currentContainer.add("Center", comp2);
  352.                 break;
  353.               case 3:
  354.                 comp1 = currentContainer.getComponent(0);
  355.                 comp2 = currentContainer.getComponent(1);
  356.                 comp3 = currentContainer.getComponent(2);
  357.                 currentContainer.remove(comp1);
  358.                 currentContainer.remove(comp2);
  359.                 currentContainer.remove(comp3);
  360.  
  361.                 currentContainer.add("North", comp1);
  362.                 currentContainer.add("South", comp2);
  363.                 currentContainer.add("Center", comp3);
  364.                 break;
  365.               case 4:
  366.                 comp1 = currentContainer.getComponent(0);
  367.                 comp2 = currentContainer.getComponent(1);
  368.                 comp3 = currentContainer.getComponent(2);
  369.                 comp4 = currentContainer.getComponent(3);
  370.  
  371.                 currentContainer.remove(comp1);
  372.                 currentContainer.remove(comp2);
  373.                 currentContainer.remove(comp3);
  374.                 currentContainer.remove(comp4);
  375.  
  376.                 currentContainer.add("North", comp1);
  377.                 currentContainer.add("South", comp2);
  378.                 currentContainer.add("East", comp3);
  379.                 currentContainer.add("Center", comp4);
  380.                 break;
  381.               case 5:
  382.               default:
  383.                 comp1 = currentContainer.getComponent(0);
  384.                 comp2 = currentContainer.getComponent(1);
  385.                 comp3 = currentContainer.getComponent(2);
  386.                 comp4 = currentContainer.getComponent(3);
  387.                 comp5 = currentContainer.getComponent(4);
  388.  
  389.                 currentContainer.remove(comp1);
  390.                 currentContainer.remove(comp2);
  391.                 currentContainer.remove(comp3);
  392.                 currentContainer.remove(comp4);
  393.                 currentContainer.remove(comp5);
  394.  
  395.                 currentContainer.add("North", comp1);
  396.                 currentContainer.add("South", comp2);
  397.                 currentContainer.add("East", comp3);
  398.                 currentContainer.add("West", comp4);
  399.                 currentContainer.add("Center", comp5);
  400.                 break;
  401.             }
  402.             }
  403.             currentContainer.validate();
  404.         }
  405.         }
  406.         setAttributes();
  407.         return true;
  408.     }
  409.     return false;
  410.     }
  411. }
  412.  
  413. public class VisualTest extends Applet {
  414.     public void init() {
  415.     new VTest();
  416.     }
  417.  
  418.     public static void main(String args[]) {
  419.     Frame f = new Frame("VisualTest");
  420.     VisualTest visualtest = new VisualTest();
  421.  
  422.     visualtest.init();
  423.     visualtest.start();
  424.  
  425.     f.add("Center", visualtest);
  426.     }
  427. }
  428.  
  429. class VPanel extends Panel {
  430.     VisualTest    target;
  431.  
  432.     public VPanel(VisualTest target) {
  433.     this.target = target;
  434.     setBackground(target.getBackground().darker());
  435.     resize(100, 100);
  436.     }
  437.  
  438.     public boolean mouseDown(Event evt, int x, int y) {
  439.     target.currentContainer = this;
  440.     target.containerMenu.enable();
  441.     return true;
  442.     }
  443. }
  444.  
  445.