/* * @(#)RunWindow.java 1.11 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import java.awt.Color; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Component; import java.awt.Dimension; import com.sun.java.swing.JPanel; import com.sun.java.swing.JSlider; import com.sun.java.swing.JButton; import com.sun.java.swing.JProgressBar; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import com.sun.java.swing.event.ChangeListener; import com.sun.java.swing.event.ChangeEvent; import com.sun.java.swing.border.*; import java.util.Date; /** * A separate window for running the Java2Demo. Go from tab to tab forever. */ public class RunWindow extends JPanel implements Runnable, ActionListener, ChangeListener { private Thread thread; private JProgressBar pb; private boolean userStop; static JSlider jslider; static JButton runB; static int theDelay = 20; static int numRuns = 20; static TitledBorder titledborder; public RunWindow() { setLayout(new GridBagLayout()); EmptyBorder eb = new EmptyBorder(5,5,5,5); setBorder(new CompoundBorder(eb, new BevelBorder(BevelBorder.LOWERED))); runB = new JButton("Run"); runB.setBackground(Color.green); runB.addActionListener(this); runB.setMinimumSize(new Dimension(60,35)); Java2Demo.addToGridBag(this, runB, 0, 0, 1, 1, 0.0, 0.0); jslider = new JSlider(JSlider.HORIZONTAL, 10, 60, theDelay); jslider.setPaintTicks(true); jslider.setMajorTickSpacing(10); jslider.setMinorTickSpacing(5); jslider.addChangeListener(this); titledborder = new TitledBorder(new EtchedBorder()); titledborder.setTitle("Delay = " + String.valueOf(theDelay)); jslider.setBorder(titledborder); jslider.setMinimumSize(new Dimension(60,35)); Java2Demo.addToGridBag(this, jslider, 1, 0, 1, 1, 1.0, 1.0); pb = new JProgressBar(); pb.setMinimum(0); Java2Demo.addToGridBag(this, pb, 0, 1, 2, 1, 1.0, 1.0); } public void stateChanged(ChangeEvent e) { setDelay(jslider.getValue()); } static void setDelay(int delay) { if (delay < 10) jslider.setMinimum(delay); if (delay > 60) jslider.setMaximum(delay); theDelay = delay; titledborder.setTitle("Delay = " + String.valueOf(theDelay)); jslider.validate(); jslider.repaint(); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "Run") { runB.setText("Stop"); runB.setBackground(Color.red); start(); } else if (e.getActionCommand() == "Stop") { stop(); } } public void start() { thread = new Thread(this); thread.setPriority(Thread.MAX_PRIORITY); thread.setName("RunWindow"); thread.start(); userStop = false; } public synchronized void stop() { thread = null; userStop = true; notifyAll(); } public void run() { System.out.println("\nJava2D Demo RunWindow : " + numRuns + " Runs, " + theDelay + " second delay between tabs\n" + "java version: " + System.getProperty("java.version") + "\n" + System.getProperty("os.name") + " " + System.getProperty("os.version") + "\n"); Runtime r = Runtime.getRuntime(); for (int runNum = 0; runNum < numRuns && thread != null; runNum++) { Date d = new Date(); System.out.print("#" + runNum + " " + d.toString() + ", "); r.gc(); float freeMemory = (float) r.freeMemory(); float totalMemory = (float) r.totalMemory(); System.out.println(((totalMemory - freeMemory)/1024) + "K used"); for (int i = 0; i < Java2Demo.tabbedPane.getTabCount() && thread != null; i++) { pb.setValue(0); pb.setMaximum(theDelay); Java2Demo.tabbedPane.setSelectedIndex(i); for (int j = 0; j < theDelay+1 && thread != null; j++) { for (int k = 0; k < 10 && thread != null; k++) { try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } pb.setValue(pb.getValue() + 1); pb.repaint(); } } if (runNum+1 == numRuns && !userStop) { System.out.println("Finished, system.exit."); System.exit(0); } } thread = null; runB.setText("Run"); runB.setBackground(Color.green); pb.setValue(0); } }