Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/CloningFeature.java
41154 views
/*1*2* Copyright (c) 2007, 2018, 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 java.awt.BorderLayout;35import java.awt.Color;36import java.awt.Component;37import java.awt.Dimension;38import java.awt.Font;39import javax.swing.JPanel;40import javax.swing.JScrollPane;41import javax.swing.JTextArea;42import javax.swing.border.BevelBorder;43import javax.swing.border.CompoundBorder;44import javax.swing.border.EmptyBorder;45import javax.swing.border.SoftBevelBorder;464748/**49* Illustration of how to use the clone feature of the demo.50*/51@SuppressWarnings("serial")52public final class CloningFeature extends JPanel implements Runnable {5354private final DemoInstVarsAccessor demoInstVars;55private Thread thread;56private JTextArea ta;5758public CloningFeature(DemoInstVarsAccessor demoInstVars) {59this.demoInstVars = demoInstVars;6061setLayout(new BorderLayout());62EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);63SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);64setBorder(new CompoundBorder(eb, sbb));6566ta = new JTextArea("Cloning Demonstrated\n\nClicking once on a demo\n");67ta.setMinimumSize(new Dimension(300, 500));68JScrollPane scroller = new JScrollPane();69scroller.getViewport().add(ta);70ta.setFont(new Font("Dialog", Font.PLAIN, 14));71ta.setForeground(Color.black);72ta.setBackground(Color.lightGray);73ta.setEditable(false);7475add("Center", scroller);7677start();78}7980public void start() {81thread = new Thread(this);82thread.setPriority(Thread.MAX_PRIORITY);83thread.setName("CloningFeature");84thread.start();85}8687public void stop() {88if (thread != null) {89thread.interrupt();90}91thread = null;92}9394@Override95@SuppressWarnings("SleepWhileHoldingLock")96public void run() {979899int index = demoInstVars.getTabbedPane().getSelectedIndex();100if (index == 0) {101demoInstVars.getTabbedPane().setSelectedIndex(1);102try {103Thread.sleep(3333);104} catch (Exception e) {105return;106}107}108109if (!demoInstVars.getControls().toolBarCB.isSelected()) {110demoInstVars.getControls().toolBarCB.setSelected(true);111try {112Thread.sleep(2222);113} catch (Exception e) {114return;115}116}117118index = demoInstVars.getTabbedPane().getSelectedIndex() - 1;119DemoGroup dg = demoInstVars.getGroup()[index];120DemoPanel dp = (DemoPanel) dg.getPanel().getComponent(0);121if (dp.surface == null) {122ta.append("Sorry your zeroth component is not a Surface.");123return;124}125126dg.mouseClicked(dp.surface);127128try {129Thread.sleep(3333);130} catch (Exception e) {131return;132}133134ta.append("Clicking the ToolBar double document button\n");135try {136Thread.sleep(3333);137} catch (Exception e) {138return;139}140141dp = (DemoPanel) dg.clonePanels[0].getComponent(0);142143if (dp.tools != null) {144for (int i = 0; i < 3 && thread != null; i++) {145ta.append(" Cloning\n");146dp.tools.cloneB.doClick();147try {148Thread.sleep(3333);149} catch (Exception e) {150return;151}152}153}154155ta.append("Changing attributes \n");156157try {158Thread.sleep(3333);159} catch (Exception e) {160return;161}162163Component[] cmps = dg.clonePanels[0].getComponents();164for (int i = 0; i < cmps.length && thread != null; i++) {165if ((dp = (DemoPanel) cmps[i]).tools == null) {166continue;167}168switch (i) {169case 0:170ta.append(" Changing AntiAliasing\n");171dp.tools.aliasB.doClick();172break;173case 1:174ta.append(" Changing Composite & Texture\n");175dp.tools.compositeB.doClick();176dp.tools.textureB.doClick();177break;178case 2:179ta.append(" Changing Screen\n");180dp.tools.screenCombo.setSelectedIndex(4);181break;182case 3:183ta.append(" Removing a clone\n");184dp.tools.cloneB.doClick();185}186try {187Thread.sleep(3333);188} catch (Exception e) {189return;190}191}192193ta.append("\nAll Done!");194}195}196197198