Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/Tools.java
41155 views
/*1*2* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d;323334import static java.awt.Color.BLACK;35import static java.awt.Color.GREEN;36import static java.awt.Color.LIGHT_GRAY;37import static java.awt.Color.WHITE;38import java.awt.BorderLayout;39import java.awt.Color;40import java.awt.Component;41import java.awt.Dimension;42import java.awt.FlowLayout;43import java.awt.Font;44import java.awt.Graphics;45import java.awt.Image;46import java.awt.Insets;47import java.awt.RenderingHints;48import java.awt.event.ActionEvent;49import java.awt.event.ActionListener;50import java.awt.event.MouseAdapter;51import java.awt.event.MouseEvent;52import java.awt.print.PrinterJob;53import java.text.DecimalFormat;54import java.util.logging.Level;55import java.util.logging.Logger;56import javax.print.attribute.HashPrintRequestAttributeSet;57import javax.print.attribute.PrintRequestAttributeSet;58import javax.swing.Icon;59import javax.swing.ImageIcon;60import javax.swing.JButton;61import javax.swing.JComboBox;62import javax.swing.JLabel;63import javax.swing.JOptionPane;64import javax.swing.JPanel;65import javax.swing.JSlider;66import javax.swing.JToggleButton;67import javax.swing.JToolBar;68import javax.swing.SwingConstants;69import javax.swing.border.EtchedBorder;70import javax.swing.event.ChangeEvent;71import javax.swing.event.ChangeListener;727374/**75* Tools to control individual demo graphic attributes. Also, control for76* start & stop on animated demos; control for cloning the demo; control for77* printing the demo. Expand and collapse the Tools panel with ToggleIcon.78*/79@SuppressWarnings("serial")80public final class Tools extends JPanel implements ActionListener,81ChangeListener, Runnable {82private final DemoInstVarsAccessor demoInstVars;83private ImageIcon stopIcon, startIcon;84private Font font = new Font(Font.SERIF, Font.PLAIN, 10);85private Color roColor = new Color(187, 213, 238);86private Surface surface;87private Thread thread;88private JPanel toolbarPanel;89private JPanel sliderPanel;90private JLabel label;91private ToggleIcon bumpyIcon, rolloverIcon;92private DecimalFormat decimalFormat = new DecimalFormat("000");93protected boolean focus;94public JToggleButton toggleB;95public JButton printB;96public JComboBox<String> screenCombo;97public JToggleButton renderB, aliasB;98public JToggleButton textureB, compositeB;99public JButton startStopB;100public JButton cloneB;101public boolean issueRepaint = true;102public JToolBar toolbar;103public JSlider slider;104public boolean doSlider;105public boolean isExpanded;106107@SuppressWarnings("LeakingThisInConstructor")108public Tools(Surface surface, DemoInstVarsAccessor demoInstVars) {109this.surface = surface;110this.demoInstVars = demoInstVars;111112setLayout(new BorderLayout());113114stopIcon = new ImageIcon(DemoImages.getImage("stop.gif", this));115startIcon = new ImageIcon(DemoImages.getImage("start.gif", this));116bumpyIcon = new ToggleIcon(this, LIGHT_GRAY);117rolloverIcon = new ToggleIcon(this, roColor);118toggleB = new JToggleButton(bumpyIcon);119toggleB.addMouseListener(new MouseAdapter() {120121@Override122public void mouseEntered(MouseEvent e) {123focus = true;124bumpyIcon.start();125}126127@Override128public void mouseExited(MouseEvent e) {129focus = false;130bumpyIcon.stop();131}132});133isExpanded = false;134toggleB.addActionListener(this);135toggleB.setMargin(new Insets(0, 0, -4, 0));136toggleB.setBorderPainted(false);137toggleB.setFocusPainted(false);138toggleB.setContentAreaFilled(false);139toggleB.setRolloverIcon(rolloverIcon);140add("North", toggleB);141142toolbar = new JToolBar();143toolbar.setPreferredSize(new Dimension(5*25, 26));144toolbar.setFloatable(false);145146String s = surface.AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON147? "On" : "Off";148aliasB = addTool("A", "Antialiasing " + s, this);149150s = surface.Rendering == RenderingHints.VALUE_RENDER_SPEED151? "Speed" : "Quality";152renderB = addTool("R", "Rendering " + s, this);153154s = surface.texture != null ? "On" : "Off";155textureB = addTool("T", "Texture " + s, this);156157s = surface.composite != null ? "On" : "Off";158compositeB = addTool("C", "Composite " + s, this);159160Image printBImg = DemoImages.getImage("print.gif", this);161printB = addTool(printBImg, "Print the Surface", this);162163if (surface instanceof AnimatingSurface) {164Image stopImg = DemoImages.getImage("stop.gif", this);165startStopB = addTool(stopImg, "Stop Animation", this);166toolbar.setPreferredSize(new Dimension(6*25, 26));167}168169screenCombo = new JComboBox<>();170screenCombo.setPreferredSize(new Dimension(100, 18));171screenCombo.setFont(font);172for (String name : GlobalControls.screenNames) {173screenCombo.addItem(name);174}175screenCombo.addActionListener(this);176toolbarPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));177toolbarPanel.setLocation(0, 6);178toolbarPanel.setVisible(false);179toolbarPanel.add(toolbar);180toolbarPanel.add(screenCombo);181toolbarPanel.setBorder(new EtchedBorder());182add(toolbarPanel);183184setPreferredSize(new Dimension(200, 8));185186if (surface instanceof AnimatingSurface) {187sliderPanel = new JPanel(new BorderLayout());188label = new JLabel(" Sleep = 030 ms");189label.setForeground(BLACK);190sliderPanel.add(label, BorderLayout.WEST);191slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 30);192slider.addChangeListener(this);193sliderPanel.setBorder(new EtchedBorder());194sliderPanel.add(slider);195196addMouseListener(new MouseAdapter() {197198@Override199public void mouseClicked(MouseEvent e) {200if (toolbarPanel.isVisible()) {201invalidate();202if ((doSlider = !doSlider)) {203remove(toolbarPanel);204add(sliderPanel);205} else {206remove(sliderPanel);207add(toolbarPanel);208}209validate();210repaint();211}212}213});214}215}216217public JButton addTool(Image img,218String toolTip,219ActionListener al) {220JButton b = new JButton(new ImageIcon(img)) {221222Dimension prefSize = new Dimension(25, 22);223224@Override225public Dimension getPreferredSize() {226return prefSize;227}228229@Override230public Dimension getMaximumSize() {231return prefSize;232}233234@Override235public Dimension getMinimumSize() {236return prefSize;237}238};239toolbar.add(b);240b.setFocusPainted(false);241b.setSelected(true);242b.setToolTipText(toolTip);243b.addActionListener(al);244return b;245}246247public JToggleButton addTool(String name,248String toolTip,249ActionListener al) {250JToggleButton b = new JToggleButton(name) {251252Dimension prefSize = new Dimension(25, 22);253254@Override255public Dimension getPreferredSize() {256return prefSize;257}258259@Override260public Dimension getMaximumSize() {261return prefSize;262}263264@Override265public Dimension getMinimumSize() {266return prefSize;267}268};269toolbar.add(b);270b.setFocusPainted(false);271if (toolTip.equals("Rendering Quality") || toolTip.equals(272"Antialiasing On") || toolTip.equals("Texture On") || toolTip.273equals("Composite On")) {274b.setSelected(true);275} else {276b.setSelected(false);277}278b.setToolTipText(toolTip);279b.addActionListener(al);280return b;281}282283@Override284public void actionPerformed(ActionEvent e) {285Object obj = e.getSource();286if (obj instanceof JButton) {287JButton b = (JButton) obj;288b.setSelected(!b.isSelected());289if (b.getIcon() == null) {290b.setBackground(b.isSelected() ? GREEN : LIGHT_GRAY);291}292}293if (obj.equals(toggleB)) {294isExpanded = !isExpanded;295if (isExpanded) {296setPreferredSize(new Dimension(200, 38));297} else {298setPreferredSize(new Dimension(200, 6));299}300toolbarPanel.setVisible(isExpanded);301if (sliderPanel != null) {302sliderPanel.setVisible(isExpanded);303}304getParent().validate();305toggleB.getModel().setRollover(false);306return;307}308if (obj.equals(printB)) {309start();310return;311}312313if (obj.equals(startStopB)) {314if (startStopB.getToolTipText().equals("Stop Animation")) {315startStopB.setIcon(startIcon);316startStopB.setToolTipText("Start Animation");317surface.animating.stop();318} else {319startStopB.setIcon(stopIcon);320startStopB.setToolTipText("Stop Animation");321surface.animating.start();322}323} else if (obj.equals(aliasB)) {324if (aliasB.getToolTipText().equals("Antialiasing On")) {325aliasB.setToolTipText("Antialiasing Off");326} else {327aliasB.setToolTipText("Antialiasing On");328}329surface.setAntiAlias(aliasB.isSelected());330} else if (obj.equals(renderB)) {331if (renderB.getToolTipText().equals("Rendering Quality")) {332renderB.setToolTipText("Rendering Speed");333} else {334renderB.setToolTipText("Rendering Quality");335}336surface.setRendering(renderB.isSelected());337} else if (obj.equals(textureB)) {338if (textureB.getToolTipText().equals("Texture On")) {339textureB.setToolTipText("Texture Off");340surface.setTexture(null);341surface.clearSurface = true;342} else {343textureB.setToolTipText("Texture On");344surface.setTexture(demoInstVars.getControls().texturechooser.texture);345}346} else if (obj.equals(compositeB)) {347if (compositeB.getToolTipText().equals("Composite On")) {348compositeB.setToolTipText("Composite Off");349} else {350compositeB.setToolTipText("Composite On");351}352surface.setComposite(compositeB.isSelected());353} else if (obj.equals(screenCombo)) {354surface.setImageType(screenCombo.getSelectedIndex());355}356357if (issueRepaint && surface.animating != null) {358if (surface.getSleepAmount() != 0) {359if (surface.animating.running()) {360surface.animating.doRepaint();361}362}363} else if (issueRepaint) {364surface.repaint();365}366}367368@Override369public void stateChanged(ChangeEvent e) {370int value = slider.getValue();371label.setText(" Sleep = " + decimalFormat.format(value) + " ms");372label.repaint();373surface.setSleepAmount(value);374}375376public void start() {377thread = new Thread(this);378thread.setPriority(Thread.MAX_PRIORITY);379thread.setName("Printing " + surface.name);380thread.start();381}382383public synchronized void stop() {384thread = null;385notifyAll();386}387388@Override389public void run() {390boolean stopped = false;391if (surface.animating != null && surface.animating.running()) {392stopped = true;393startStopB.doClick();394}395396try {397PrinterJob printJob = PrinterJob.getPrinterJob();398printJob.setPrintable(surface);399boolean pDialogState = true;400PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();401402if (!demoInstVars.getPrintCB().isSelected()) {403pDialogState = printJob.printDialog(aset);404}405if (pDialogState) {406printJob.print(aset);407}408} catch (@SuppressWarnings("removal") java.security.AccessControlException ace) {409String errmsg = "Applet access control exception; to allow "410+ "access to printer, set\n"411+ "permission for \"queuePrintJob\" in "412+ "RuntimePermission.";413JOptionPane.showMessageDialog(this, errmsg, "Printer Access Error",414JOptionPane.ERROR_MESSAGE);415} catch (Exception ex) {416Logger.getLogger(Tools.class.getName()).log(Level.SEVERE,417null, ex);418}419420if (stopped) {421startStopB.doClick();422}423thread = null;424}425426427/**428* Expand and Collapse the Tools Panel with this bumpy button.429*/430static class ToggleIcon implements Icon, Runnable {431432private Color shadowColor = new Color(102, 102, 153);433private Color fillColor;434private Tools tools;435private Thread thread;436437public ToggleIcon(Tools tools, Color fillColor) {438this.tools = tools;439this.fillColor = fillColor;440}441442@Override443public void paintIcon(Component c, Graphics g, int x, int y) {444int w = getIconWidth();445int h = getIconHeight();446g.setColor(fillColor);447g.fillRect(0, 0, w, h);448for (; x < w - 2; x += 4) {449g.setColor(WHITE);450g.fillRect(x, 1, 1, 1);451g.fillRect(x + 2, 3, 1, 1);452g.setColor(shadowColor);453g.fillRect(x + 1, 2, 1, 1);454g.fillRect(x + 3, 4, 1, 1);455}456}457458@Override459public int getIconWidth() {460return tools.getSize().width;461}462463@Override464public int getIconHeight() {465return 6;466}467468public void start() {469thread = new Thread(this);470thread.setPriority(Thread.MIN_PRIORITY);471thread.setName("ToggleIcon");472thread.start();473}474475public synchronized void stop() {476if (thread != null) {477thread.interrupt();478}479thread = null;480}481482@Override483public void run() {484try {485Thread.sleep(400);486} catch (InterruptedException e) {487}488if (tools.focus && thread != null) {489tools.toggleB.doClick();490}491thread = null;492}493}494} // End Tools class495496497498