home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / RunWindow.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  5.2 KB  |  169 lines

  1. /*
  2.  * @(#)RunWindow.java    1.16 98/09/13
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.Color;
  17. import java.awt.GridBagLayout;
  18. import java.awt.GridBagConstraints;
  19. import java.awt.Component;
  20. import java.awt.Dimension;
  21. import javax.swing.JPanel;
  22. import javax.swing.JSlider;
  23. import javax.swing.JButton;
  24. import javax.swing.JProgressBar;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.ActionEvent;
  27. import javax.swing.event.ChangeListener;
  28. import javax.swing.event.ChangeEvent;
  29. import javax.swing.border.*;
  30. import java.util.Date;
  31.  
  32.  
  33. /**
  34.  * A separate window for running the Java2Demo.  Go from tab to tab forever.
  35.  */
  36. public class RunWindow extends JPanel implements Runnable, ActionListener, ChangeListener {
  37.  
  38.     private Thread thread;
  39.     private JProgressBar pb;
  40.  
  41.     static JSlider jslider;
  42.     static JButton runB;
  43.     static int delay = 10;
  44.     static int numRuns = 20;
  45.     static boolean exit;
  46.  
  47.  
  48.     public RunWindow() {
  49.  
  50.         setLayout(new GridBagLayout());
  51.         EmptyBorder eb = new EmptyBorder(5,5,5,5);
  52.         setBorder(new CompoundBorder(eb, new BevelBorder(BevelBorder.LOWERED)));
  53.  
  54.         runB = new JButton("Run");
  55.         runB.setBackground(Color.green);
  56.         runB.addActionListener(this);
  57.         runB.setMinimumSize(new Dimension(66,35));
  58.         Java2Demo.addToGridBag(this, runB, 0, 0, 1, 1, 0.0, 0.0);
  59.  
  60.         jslider = new JSlider(JSlider.HORIZONTAL, 5, 60, delay);
  61.         jslider.setPaintTicks(true);
  62.         jslider.setMajorTickSpacing(10);
  63.         jslider.setMinorTickSpacing(5);
  64.         jslider.addChangeListener(this);
  65.         TitledBorder tb = new TitledBorder(new EtchedBorder());
  66.         tb.setTitle("Delay = " + String.valueOf(delay));
  67.         jslider.setBorder(tb);
  68.         jslider.setMinimumSize(new Dimension(60,35));
  69.         Java2Demo.addToGridBag(this, jslider, 1, 0, 1, 1, 1.0, 1.0);
  70.  
  71.         pb = new JProgressBar();
  72.         pb.setMinimum(0);
  73.         Java2Demo.addToGridBag(this, pb, 0, 1, 2, 1, 1.0, 1.0);
  74.     }
  75.  
  76.  
  77.     public void stateChanged(ChangeEvent e) {
  78.         setDelay(jslider.getValue());
  79.     }
  80.  
  81.  
  82.     static void setDelay(int delay) {
  83.         if (delay < jslider.getMinimum()) {
  84.             jslider.setMinimum(delay);
  85.         }
  86.         if (delay > jslider.getMaximum()) {
  87.             jslider.setMaximum(delay);
  88.         }
  89.         RunWindow.delay = delay;
  90.         TitledBorder tb = (TitledBorder) jslider.getBorder();
  91.         tb.setTitle("Delay = " + String.valueOf(delay));
  92.         jslider.validate();
  93.         jslider.repaint();
  94.     }
  95.  
  96.  
  97.     public void actionPerformed(ActionEvent e) {
  98.         if (e.getActionCommand() == "Run") {
  99.             runB.setText("Stop");
  100.             runB.setBackground(Color.red);
  101.             start();
  102.         } else if (e.getActionCommand() == "Stop") {
  103.             stop();
  104.         }
  105.     }
  106.  
  107.  
  108.     public void start() {
  109.         thread = new Thread(this);
  110.         thread.setPriority(Thread.MAX_PRIORITY);
  111.         thread.setName("RunWindow");
  112.         thread.start();
  113.     }
  114.  
  115.  
  116.     public synchronized void stop() {
  117.         thread = null;
  118.         notifyAll();
  119.     }
  120.  
  121.  
  122.     public void run() {
  123.  
  124.         System.out.println("\nJava2D Demo RunWindow : " + 
  125.                 numRuns + " Runs, " + 
  126.                 delay + " second delay between tabs\n" + 
  127.                 "java version: " + System.getProperty("java.version") + 
  128.                 "\n" + System.getProperty("os.name") + " " + 
  129.                 System.getProperty("os.version") + "\n");
  130.         Runtime r = Runtime.getRuntime();
  131.  
  132.         for (int runNum = 0; runNum < numRuns && thread != null; runNum++) {
  133.  
  134.             Date d = new Date();
  135.             System.out.print("#" + runNum + " " + d.toString() + ", ");
  136.             r.gc();
  137.             float freeMemory = (float) r.freeMemory();
  138.             float totalMemory = (float) r.totalMemory();
  139.             System.out.println(((totalMemory - freeMemory)/1024) + "K used");
  140.  
  141.             for (int i = 0; i < Java2Demo.tabbedPane.getTabCount() && thread != null; i++) {
  142.                 pb.setValue(0);
  143.                 pb.setMaximum(delay);
  144.                 Java2Demo.tabbedPane.setSelectedIndex(i);
  145.                 for (int j = 0; j < delay+1 && thread != null; j++) {
  146.                     for (int k = 0; k < 10 && thread != null; k++) {
  147.                         try {
  148.                             Thread.sleep(100);
  149.                         } catch (Exception e) { e.printStackTrace(); }
  150.                     }
  151.                     pb.setValue(pb.getValue() + 1);
  152.                     pb.repaint();
  153.                 }
  154.             }
  155.             if (runNum+1 == numRuns) {
  156.                 System.out.println("Finished.");
  157.                 if (exit && thread != null) {
  158.                     System.out.println("System.exit(0).");
  159.                     System.exit(0);
  160.                 }
  161.             }
  162.         }
  163.         thread = null;
  164.         runB.setText("Run");
  165.         runB.setBackground(Color.green);
  166.         pb.setValue(0);
  167.     }
  168. }
  169.