/* * @(#)ToolBar.java 1.12 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.*; import com.sun.java.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.print.PrinterJob; import java.net.URL; /** * ToolBar to control individual demo graphic attributes. Also, control for * start & stop on animated demos; control for cloning the demo; control for * printing the demo. */ public class ToolBar extends JPanel implements ActionListener, Runnable { private TheLayout tl; private CanvasPanel cp; private J2DCanvas canvas; private JButton printB; private Thread thread; private ImageIcon pauseIcon; private ImageIcon runIcon; private Font font = new Font("Dialog", Font.BOLD, 12); private boolean doRepaint = true; public JButton removeCloneB; public JButton cloneB; public JComboBox screenCombo; public JButton renderB, aliasB; public JButton textureB, compositeB; public JButton pauseResumeB; public JToolBar toolBar; public ToolBar(TheLayout tl, CanvasPanel cp) { this.tl = tl; this.cp = cp; this.canvas = cp.canvas; URL url = ToolBar.class.getResource("images/paused.gif"); pauseIcon = new ImageIcon(url); url = ToolBar.class.getResource("images/running.gif"); runIcon = new ImageIcon(url); toolBar = new JToolBar(); if (canvas.runnable || canvas.isRunning) { pauseResumeB = addTool("running.gif","Thread Running"); } String s = canvas.AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON ? "On" : "Off"; aliasB = addTool("A", "Antialiasing " + s); s = canvas.Rendering == RenderingHints.VALUE_RENDER_SPEED ? "Speed" : "Quality"; renderB = addTool("R", "Rendering " + s); s = Java2Demo.controls.textureCB.isSelected() ? "On" : "Off"; textureB = addTool("T", "Texture " + s); s = Java2Demo.controls.compositeCB.isSelected() ? "On" : "Off"; compositeB = addTool("C", "Composite " + s); JPopupMenu.setDefaultLightWeightPopupEnabled(false); screenCombo = new JComboBox(); screenCombo.setLightWeightPopupEnabled(false); screenCombo.setBackground(Color.lightGray); for (int i=0; i < Java2Demo.controls.screenCombo.getItemCount(); i++) { screenCombo.addItem((Object) Java2Demo.controls.screenCombo.getItemAt(i)); } screenCombo.setPreferredSize(new Dimension(100, 20)); screenCombo.setSelectedIndex(canvas.Screen); screenCombo.addActionListener(this); screenCombo.setToolTipText("Graphics Context"); setLayout(new FlowLayout()); setBackground(Color.gray); add(toolBar); add(screenCombo); } public JButton addTool(String str, String toolTip) { JButton b = null; if (str.indexOf(".") == -1) { b = (JButton) toolBar.add(new JButton(str)); b.setFont(font); if (toolTip.equals("Rendering Quality") || toolTip.equals("Antialiasing On") || toolTip.equals("Texture On") || toolTip.equals("Composite On")) { b.setBackground(Color.green); b.setSelected(true); } else { b.setBackground(Color.lightGray); b.setSelected(false); } //System.out.println(runIcon.getIconHeight()); // Hard coding single character buttons for better look. //b.setPreferredSize(new Dimension(20, 20)); //b.setMinimumSize(new Dimension(20, 20)); } else { URL url = ToolBar.class.getResource("images/" + str); b = (JButton) toolBar.add( new JButton(new ImageIcon(url))); b.setSelected(true); } b.setToolTipText(toolTip); b.addActionListener(this); return b; } public void addMoreTools() { cloneB = addTool("clone.gif","Clone the Canvas"); printB = addTool("print.gif","Print the Canvas"); } // Check toolbar against global controls public void setup(boolean doRepaint) { this.doRepaint = doRepaint; Controls c = Java2Demo.controls; if ((aliasB.isSelected() && !c.aliasCB.isSelected()) || (!aliasB.isSelected() && c.aliasCB.isSelected())) aliasB.doClick(); if ((renderB.isSelected() && !c.renderCB.isSelected()) || (!renderB.isSelected() && c.renderCB.isSelected())) renderB.doClick(); if ((textureB.isSelected() && !c.textureCB.isSelected()) || (!textureB.isSelected() && c.textureCB.isSelected())) textureB.doClick(); if ((compositeB.isSelected() && !c.compositeCB.isSelected()) || (!compositeB.isSelected() && c.compositeCB.isSelected())) compositeB.doClick(); if ((isVisible() && !c.toolBarCB.isSelected()) || (!isVisible() && c.toolBarCB.isSelected())) { setVisible(c.toolBarCB.isSelected()); validate(); } if (pauseResumeB != null && pauseResumeB.isSelected()) { // invoke repaint() for ImageObserver demos canvas.isRunning = true; // don't invoke start on ImageObserver demos if (canvas.runnable) { canvas.start(); } } screenCombo.setSelectedIndex(canvas.Screen); this.doRepaint = true; } public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj.equals(pauseResumeB)) { if (pauseResumeB.getToolTipText().equals("Thread Running")) { pauseResumeB.setIcon(pauseIcon); pauseResumeB.setToolTipText("Thread Stopped"); pauseResumeB.setSelected(false); pauseResumeB.repaint(); canvas.isRunning = false; canvas.stop(); } else { pauseResumeB.setIcon(runIcon); pauseResumeB.setToolTipText("Thread Running"); pauseResumeB.setSelected(true); pauseResumeB.repaint(); // invoke repaint() for ImageObserver demos canvas.isRunning = true; // don't invoke start on ImageObserver demos if (canvas.runnable) canvas.start(); } } else if (obj.equals(aliasB)) { if (aliasB.getToolTipText().equals("Antialiasing On")) { aliasB.setToolTipText("Antialiasing Off"); aliasB.setBackground(Color.lightGray); aliasB.setSelected(false); canvas.AntiAlias = RenderingHints.VALUE_ANTIALIAS_OFF; } else { aliasB.setToolTipText("Antialiasing On"); aliasB.setBackground(Color.green); aliasB.setSelected(true); canvas.AntiAlias = RenderingHints.VALUE_ANTIALIAS_ON; } } else if (obj.equals(renderB)) { if (renderB.getToolTipText().equals("Rendering Quality")) { renderB.setToolTipText("Rendering Speed"); renderB.setBackground(Color.lightGray); renderB.setSelected(false); canvas.Rendering = RenderingHints.VALUE_RENDER_SPEED; } else { renderB.setToolTipText("Rendering Quality"); renderB.setBackground(Color.green); renderB.setSelected(true); canvas.Rendering = RenderingHints.VALUE_RENDER_QUALITY; } } else if (obj.equals(textureB)) { if (textureB.getToolTipText().equals("Texture On")) { textureB.setToolTipText("Texture Off"); textureB.setBackground(Color.lightGray); textureB.setSelected(false); canvas.texture = null; } else { textureB.setToolTipText("Texture On"); textureB.setBackground(Color.green); textureB.setSelected(true); canvas.texture = Java2Demo.controls.texturechooser.texture; if (canvas.texture instanceof GradientPaint) { canvas.texture = new GradientPaint(0,0,Color.white,canvas.getSize().width*2,0,Color.green); } } } else if (obj.equals(compositeB)) { if (compositeB.getToolTipText().equals("Composite On")) { compositeB.setToolTipText("Composite Off"); compositeB.setBackground(Color.lightGray); compositeB.setSelected(false); canvas.composite = null; } else { compositeB.setToolTipText("Composite On"); compositeB.setBackground(Color.green); compositeB.setSelected(true); canvas.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); } } else if (obj.equals(cloneB)) { tl.CloneCanvas(); } else if (obj.equals(removeCloneB)) { tl.RemoveClone(cp); } else if (obj.equals(printB)) { start(); } else if (obj.equals(screenCombo)) { if (screenCombo.getSelectedIndex() == 0) { // getInterfaces() for ImageObserver demos if (canvas.runnable || canvas.getClass().getInterfaces().length != 0) { canvas.Screen = 2; } else { canvas.Screen = 1; } } else { canvas.Screen = screenCombo.getSelectedIndex(); } canvas.offImg = null; } if (doRepaint && (obj.equals(aliasB) || obj.equals(renderB) || obj.equals(textureB) || obj.equals(compositeB) || obj.equals(screenCombo))) { canvas.repaint(); if (Java2Demo.controls.verboseCB.isSelected()) { canvas.verbose(); } } } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Printing " + cp.className); thread.start(); } public synchronized void stop() { thread = null; notifyAll(); } public void run() { boolean stopped = false; if (canvas.thread != null || canvas.isRunning) { stopped = true; pauseResumeB.doClick(); } PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(canvas); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } if (stopped) { pauseResumeB.doClick(); } thread = null; } }