FileDocCategorySizeDatePackage
Tools.javaAPI DocExample15136Wed Aug 08 15:13:48 BST 2001None

Tools

public class Tools extends JPanel implements MouseListener, ActionListener, Runnable, ChangeListener
Tools to control individual demo graphic attributes. Also, control for start & stop on animated demos; control for cloning the demo; control for printing the demo. Expand and collapse the Tools panel with ToggleIcon.

Fields Summary
private ImageIcon
stopIcon
private ImageIcon
startIcon
private Font
font
private Color
roColor
private Surface
surface
private Thread
thread
private JPanel
toolbarPanel
private JPanel
sliderPanel
private JLabel
label
private ToggleIcon
bumpyIcon
private ToggleIcon
rolloverIcon
protected boolean
focus
public JButton
toggleB
public JButton
printB
public JComboBox
screenCombo
public JButton
renderB
public JButton
aliasB
public JButton
textureB
public JButton
compositeB
public JButton
startStopB
public JButton
cloneB
public boolean
issueRepaint
public JToolBar
toolbar
public JSlider
slider
public boolean
doSlider
public boolean
isExpanded
Constructors Summary
public Tools(Surface surface)


       
        this.surface = surface;
        setBackground(Color.gray);
        setLayout(new BorderLayout());

        stopIcon = new ImageIcon(DemoImages.getImage("stop.gif", this));
        startIcon = new ImageIcon(DemoImages.getImage("start.gif",this));
        bumpyIcon = new ToggleIcon(this, Color.lightGray);
        rolloverIcon = new ToggleIcon(this, roColor);
        toggleB = new JButton(bumpyIcon);
        toggleB.addMouseListener(this);
        isExpanded = false;
        toggleB.addActionListener(this);
        toggleB.setMargin(new Insets(0,0,-4,0));
        toggleB.setBorderPainted(false);
        toggleB.setFocusPainted(false);
        toggleB.setContentAreaFilled(false);
        toggleB.setRolloverIcon(rolloverIcon);
        add("North", toggleB);


        toolbar = new JToolBar();
        toolbar.setPreferredSize(new Dimension(100, 26));
        toolbar.setFloatable(false);

        String s = surface.AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON 
            ? "On" : "Off";
        aliasB = addTool( "A", "Antialiasing " + s, this);

        s = surface.Rendering == RenderingHints.VALUE_RENDER_SPEED
            ? "Speed" : "Quality";
        renderB = addTool("R", "Rendering " + s, this);

        s = surface.texture != null ? "On" : "Off";
        textureB = addTool("T", "Texture " + s, this);

        s = surface.composite != null ? "On" : "Off";
        compositeB = addTool("C", "Composite " + s, this);

        printB = addTool("print.gif", "Print the Surface", this);

        if (surface instanceof AnimatingSurface) {
            startStopB = addTool("stop.gif", "Stop Animation", this);
            toolbar.setPreferredSize(new Dimension(122, 26));
        }

        screenCombo = new JComboBox();
        screenCombo.setPreferredSize(new Dimension(100, 18));
        screenCombo.setFont(font);
        for (int i = 0; i < GlobalControls.screenNames.length; i++) {
            screenCombo.addItem(GlobalControls.screenNames[i]);
        } 
        screenCombo.addActionListener(this);
        toolbarPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
        toolbarPanel.setBackground(Color.gray);
        toolbarPanel.setLocation(0,6);
        toolbarPanel.setVisible(false);
        toolbarPanel.add(toolbar);
        toolbarPanel.add(screenCombo);
        add(toolbarPanel);

        setPreferredSize(new Dimension(200,6));

        if (surface instanceof AnimatingSurface) {
            sliderPanel = new JPanel(new GridLayout(0,2,5,5));
            sliderPanel.setBackground(Color.gray);
            label = new JLabel("sleep = 30 ms");
            label.setForeground(Color.black);
            sliderPanel.add(label);
            slider = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
            slider.setBackground(Color.gray);
            slider.addChangeListener(this);
            TitledBorder tb = new TitledBorder(new EtchedBorder());
            tb.setTitleFont(new Font("serif", Font.PLAIN, 8));
            tb.setTitle("sleep = 30 ms");
            EmptyBorder eb = new EmptyBorder(4,5,4,5);
            slider.setBorder(new CompoundBorder(eb, new EtchedBorder()));
            sliderPanel.add(slider);

            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                   if (toolbarPanel.isVisible()) {
                       invalidate();
                       if ((doSlider = !doSlider)) {
                           remove(toolbarPanel);
                           add(sliderPanel);
                       } else {
                           remove(sliderPanel);
                           add(toolbarPanel);
                       }
                       validate();
                       repaint();
                   }
                }
            });
        }
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        Object obj = e.getSource();
        if (obj.equals(toggleB)) {
            isExpanded = !isExpanded;
            if (isExpanded) {
                setPreferredSize(new Dimension(200,38));
            } else {
                setPreferredSize(new Dimension(200,6));
            }
            toolbarPanel.setVisible(isExpanded);
            if (sliderPanel != null) {
                sliderPanel.setVisible(isExpanded);
            }
            getParent().validate();
            toggleB.getModel().setRollover(false);
            return;
        }
        if (obj.equals(printB)) {
            start();
            return;
        }
        if (obj instanceof JButton) {
            JButton b = (JButton) obj;
            b.setSelected(!b.isSelected());
            if (b.getIcon() == null) {
                b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
            }
        }
        if (obj.equals(startStopB)) {
            if (startStopB.getToolTipText().equals("Stop Animation")) {
                startStopB.setIcon(startIcon);
                startStopB.setToolTipText("Start Animation");
                surface.animating.stop();
            } else {
                startStopB.setIcon(stopIcon);
                startStopB.setToolTipText("Stop Animation");
                surface.animating.start();
            }
        } else if (obj.equals(aliasB)) {
            if (aliasB.getToolTipText().equals("Antialiasing On")) {
                aliasB.setToolTipText("Antialiasing Off");
            } else {
                aliasB.setToolTipText("Antialiasing On");
            }
            surface.setAntiAlias(aliasB.isSelected());
        } else if (obj.equals(renderB)) {
            if (renderB.getToolTipText().equals("Rendering Quality")) {
                renderB.setToolTipText("Rendering Speed");
            } else {
                renderB.setToolTipText("Rendering Quality");
            }
            surface.setRendering(renderB.isSelected());
        } else if (obj.equals(textureB)) {
            Object texture = null;
            if (textureB.getToolTipText().equals("Texture On")) {
                textureB.setToolTipText("Texture Off");
                surface.setTexture(null);
                surface.clearSurface = true;
            } else {
                textureB.setToolTipText("Texture On");
                surface.setTexture(TextureChooser.texture);
            }
        } else if (obj.equals(compositeB)) {
            if (compositeB.getToolTipText().equals("Composite On")) {
                compositeB.setToolTipText("Composite Off");
            } else {
                compositeB.setToolTipText("Composite On");
            }
            surface.setComposite(compositeB.isSelected());
        } else if (obj.equals(screenCombo)) {
            surface.setImageType(screenCombo.getSelectedIndex());
        }

        if (issueRepaint && surface.animating != null) {
            if (surface.getSleepAmount() != 0) {
                if (surface.animating.thread != null) {
                    surface.animating.thread.interrupt();
                }
            }
        } else if (issueRepaint) {
            surface.repaint();
        }
    
public javax.swing.JButtonaddTool(java.lang.String name, java.lang.String toolTip, java.awt.event.ActionListener al)

        JButton b = null;
        if (name.indexOf(".") == -1) {
            b = (JButton) toolbar.add(new JButton(name));
            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);
            }
            b.setPreferredSize(new Dimension(18, 22));
            b.setMaximumSize(new Dimension(18, 22));
            b.setMinimumSize(new Dimension(18, 22));
        } else {
            Image img = DemoImages.getImage(name, this);
            b = (JButton) toolbar.add(new JButton(new ImageIcon(img)));
            b.setSelected(true);
        }
        b.setToolTipText(toolTip);
        b.addActionListener(al);
        return b;
    
public voidmouseClicked(java.awt.event.MouseEvent e)

    
public voidmouseEntered(java.awt.event.MouseEvent e)

        focus = true;
        bumpyIcon.start();
    
public voidmouseExited(java.awt.event.MouseEvent e)

        focus = false;
        bumpyIcon.stop();
    
public voidmousePressed(java.awt.event.MouseEvent e)

    
public voidmouseReleased(java.awt.event.MouseEvent e)

    
public voidrun()

        boolean stopped = false;
        if (surface.animating != null && surface.animating.thread != null) {
            stopped = true;
            startStopB.doClick();
        }

        try {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(surface);
            boolean pDialogState = true;
            
            if (!Java2Demo.printCB.isSelected()) {
                pDialogState = printJob.printDialog();
            }
            if (pDialogState) {
                printJob.print();
            }
        } catch (java.security.AccessControlException ace) {
            String errmsg = "Applet access control exception; to allow " +
                            "access to printer, run policytool and set\n" +
                            "permission for \"queuePrintJob\" in " +
                            "RuntimePermission.";
            JOptionPane.showMessageDialog(this, errmsg, "Printer Access Error",
                                          JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (stopped) {
            startStopB.doClick();
        }
        thread = null;
    
public voidstart()

        thread = new Thread(this);
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.setName("Printing " + surface.name);
        thread.start();
    
public voidstateChanged(javax.swing.event.ChangeEvent e)

        int value = slider.getValue();
        label.setText("sleep = " + String.valueOf(value) + " ms");
        label.repaint();
        surface.setSleepAmount(value);
    
public synchronized voidstop()

        thread = null;
        notifyAll();