home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Java2D / src / java2d / RunWindow.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  10.1 KB  |  275 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)RunWindow.java    1.24 02/06/13
  38.  */
  39.  
  40. package java2d;
  41.  
  42. import java.awt.*;
  43. import javax.swing.*;
  44. import java.awt.event.ActionListener;
  45. import java.awt.event.ActionEvent;
  46. import java.awt.event.MouseEvent;
  47. import javax.swing.border.*;
  48. import java.util.Date;
  49.  
  50.  
  51. /**
  52.  * A separate window for running the Java2Demo.  Go from tab to tab or demo to
  53.  * demo.
  54.  */
  55. public class RunWindow extends JPanel implements Runnable, ActionListener {
  56.  
  57.     static JButton runB;
  58.     static int delay = 10;
  59.     static int numRuns = 20;
  60.     static boolean exit;
  61.     static JCheckBox zoomCB = new JCheckBox("Zoom");
  62.     static JCheckBox printCB = new JCheckBox("Print");
  63.     static boolean buffersFlag;
  64.     static int bufBeg, bufEnd;
  65.  
  66.     private JTextField delayTextField, runsTextField;
  67.     private Thread thread;
  68.     private JProgressBar pb;
  69.  
  70.  
  71.  
  72.     public RunWindow() {
  73.  
  74.         setLayout(new GridBagLayout());
  75.         EmptyBorder eb = new EmptyBorder(5,5,5,5);
  76.         setBorder(new CompoundBorder(eb, new BevelBorder(BevelBorder.LOWERED)));
  77.  
  78.         Font font = new Font("serif", Font.PLAIN, 10);
  79.  
  80.         runB = new JButton("Run");
  81.         runB.setBackground(Color.green);
  82.         runB.addActionListener(this);
  83.         runB.setMinimumSize(new Dimension(70,30));
  84.         Java2Demo.addToGridBag(this, runB, 0, 0, 1, 1, 0.0, 0.0);
  85.  
  86.         pb = new JProgressBar();
  87.         pb.setPreferredSize(new Dimension(100,30));
  88.         pb.setMinimum(0);
  89.         Java2Demo.addToGridBag(this, pb, 1, 0, 2, 1, 1.0, 0.0);
  90.  
  91.         JPanel p1 = new JPanel(new GridLayout(2,2));
  92.  
  93.         JPanel p2 = new JPanel();
  94.         JLabel l = new JLabel("Runs:");
  95.         l.setFont(font);
  96.         l.setForeground(Color.black);
  97.         p2.add(l);
  98.         p2.add(runsTextField = new JTextField(String.valueOf(numRuns)));
  99.         runsTextField.setPreferredSize(new Dimension(30,20));
  100.         runsTextField.addActionListener(this);
  101.         p1.add(p2);
  102.         p2 = new JPanel();
  103.         l = new JLabel("Delay:");
  104.         l.setFont(font);
  105.         l.setForeground(Color.black);
  106.         p2.add(l);
  107.         p2.add(delayTextField = new JTextField(String.valueOf(delay)));
  108.         delayTextField.setPreferredSize(new Dimension(30,20));
  109.         delayTextField.addActionListener(this);
  110.         p1.add(p2);
  111.  
  112.         zoomCB.setHorizontalAlignment(JButton.CENTER);
  113.         zoomCB.setFont(font);
  114.         printCB.setFont(font);
  115.         p1.add(zoomCB); 
  116.         p1.add(printCB);
  117.         printCB.addActionListener(this);
  118.         Java2Demo.addToGridBag(this, p1, 0, 1, 3, 1, 1.0, 1.0);
  119.     }
  120.  
  121.  
  122.     public void actionPerformed(ActionEvent e) {
  123.         if (e.getSource().equals(printCB)) {
  124.             Java2Demo.printCB.setSelected(printCB.isSelected());
  125.         } else if (e.getSource().equals(delayTextField)) {
  126.             delay = Integer.parseInt(delayTextField.getText().trim());
  127.         } else if (e.getSource().equals(runsTextField)) {
  128.             numRuns = Integer.parseInt(runsTextField.getText().trim());
  129.         } else if (e.getActionCommand() == "Run") {
  130.             runB.setText("Stop");
  131.             runB.setBackground(Color.red);
  132.             start();
  133.         } else if (e.getActionCommand() == "Stop") {
  134.             stop();
  135.         }
  136.     }
  137.  
  138.  
  139.     public void start() {
  140.         thread = new Thread(this);
  141.         thread.setPriority(Thread.NORM_PRIORITY+1);
  142.         thread.setName("RunWindow");
  143.         thread.start();
  144.     }
  145.  
  146.  
  147.     public synchronized void stop() {
  148.         if (thread != null) {
  149.             thread.interrupt();
  150.         }
  151.         thread = null;
  152.         notifyAll();
  153.     }
  154.  
  155.  
  156.     public void sleepPerTab() {
  157.         for (int j = 0; j < delay+1 && thread != null; j++) {
  158.             for (int k = 0; k < 10 && thread != null; k++) {
  159.                 try {
  160.                     thread.sleep(100);
  161.                 } catch (Exception e) { }
  162.             }
  163.             pb.setValue(pb.getValue() + 1);
  164.             pb.repaint();
  165.         }
  166.     }
  167.  
  168.  
  169.     private void printDemo(DemoGroup dg) {
  170.         if (!Java2Demo.controls.toolBarCB.isSelected()) {
  171.             Java2Demo.controls.toolBarCB.setSelected(true);
  172.             dg.revalidate();
  173.             try { thread.sleep(2000); } catch (Exception e) { }
  174.         }
  175.         JPanel p = dg.getPanel();
  176.         for (int j = 0; j < p.getComponentCount(); j++) {
  177.             DemoPanel dp = (DemoPanel) p.getComponent(j);
  178.             if (dp.tools != null) {
  179.                if (dp.surface.animating != null) {
  180.                    if (dp.surface.animating.thread != null) {
  181.                        dp.tools.startStopB.doClick();
  182.                        try { thread.sleep(999); } catch (Exception e) {}
  183.                    }
  184.                }
  185.                dp.tools.printB.doClick();
  186.                try { thread.sleep(999); } catch (Exception e) {}
  187.             }
  188.         }
  189.     }
  190.  
  191.  
  192.     public void run() {
  193.  
  194.         System.out.println("\nJava2D Demo RunWindow : " + 
  195.             numRuns + " Runs, " + 
  196.             delay + " second delay between tabs\n" + 
  197.             "java version: " + System.getProperty("java.version") + 
  198.             "\n" + System.getProperty("os.name") + " " + 
  199.             System.getProperty("os.version") + "\n");
  200.         Runtime r = Runtime.getRuntime();
  201.  
  202.         for (int runNum = 0; runNum < numRuns && thread != null; runNum++) {
  203.  
  204.             Date d = new Date();
  205.             System.out.print("#" + runNum + " " + d.toString() + ", ");
  206.             r.gc();
  207.             float freeMemory = (float) r.freeMemory();
  208.             float totalMemory = (float) r.totalMemory();
  209.             System.out.println(((totalMemory - freeMemory)/1024) + "K used");
  210.  
  211.             for (int i = 0; i < Java2Demo.tabbedPane.getTabCount() && thread != null; i++) {
  212.                 pb.setValue(0);
  213.                 pb.setMaximum(delay);
  214.                 DemoGroup dg = null;
  215.                 if (i != 0) {
  216.                     dg = Java2Demo.group[i-1];
  217.                     dg.invalidate();
  218.                 }
  219.                 Java2Demo.tabbedPane.setSelectedIndex(i);
  220.                 if (i != 0 && (zoomCB.isSelected() || buffersFlag)) {
  221.                     DemoPanel dp = (DemoPanel) dg.getPanel().getComponent(0);
  222.                     if (dg.tabbedPane == null && dp.surface != null) {
  223.                         dg.mouseClicked(new MouseEvent(dp.surface, MouseEvent.MOUSE_CLICKED, 0, 0, 10, 10, 1, false));
  224.                         try {thread.sleep(999);} catch (Exception e) {}
  225.                     }
  226.                     for (int j = 1; j < dg.tabbedPane.getTabCount() && thread != null; j++) {
  227.                         pb.setValue(0);
  228.                         pb.setMaximum(delay);
  229.                         dg.tabbedPane.setSelectedIndex(j);
  230.                         JPanel p = dg.getPanel();
  231.                         if (buffersFlag && p.getComponentCount() == 1) {
  232.                             dp = (DemoPanel) p.getComponent(0);
  233.                             if (dp.surface.animating != null) {
  234.                                 dp.surface.animating.stop();
  235.                             }
  236.                             for (int k = bufBeg; k <= bufEnd && thread != null; k++) {
  237.                                  dp.tools.cloneB.doClick();
  238.                                  try {thread.sleep(500);} catch (Exception e) {}
  239.                                  int n = p.getComponentCount();
  240.                                  DemoPanel clone = (DemoPanel)p.getComponent(n-1);
  241.                                  if (clone.surface.animating != null) {
  242.                                      clone.surface.animating.stop();
  243.                                  }
  244.                                  clone.tools.issueRepaint = true;
  245.                                  clone.tools.screenCombo.setSelectedIndex(k);
  246.                                  clone.tools.issueRepaint = false;
  247.                             }
  248.                         }
  249.                         if (printCB.isSelected()) {
  250.                             printDemo(dg);
  251.                         }
  252.                         sleepPerTab();
  253.                     }
  254.                 } else if (i != 0 && printCB.isSelected()) {
  255.                     printDemo(dg);
  256.                     sleepPerTab();
  257.                 } else {
  258.                     sleepPerTab();
  259.                 }
  260.             }
  261.             if (runNum+1 == numRuns) {
  262.                 System.out.println("Finished.");
  263.                 if (exit && thread != null) {
  264.                     System.out.println("System.exit(0).");
  265.                     System.exit(0);
  266.                 }
  267.             }
  268.         }
  269.         thread = null;
  270.         runB.setText("Run");
  271.         runB.setBackground(Color.green);
  272.         pb.setValue(0);
  273.     }
  274. }
  275.