Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/DemoPanel.java
41155 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 static java2d.CustomControlsContext.State.START;35import static java2d.CustomControlsContext.State.STOP;36import java.awt.BorderLayout;37import java.awt.Component;38import java.util.logging.Level;39import java.util.logging.Logger;40import javax.swing.JPanel;41import javax.swing.border.BevelBorder;42import javax.swing.border.CompoundBorder;43import javax.swing.border.EmptyBorder;44import javax.swing.border.SoftBevelBorder;454647/**48* The panel for the Surface, Custom Controls & Tools.49* Other component types welcome.50*/51@SuppressWarnings("serial")52public class DemoPanel extends JPanel {53private final DemoInstVarsAccessor demoInstVars;54public Surface surface;55public CustomControlsContext ccc;56public Tools tools;57public String className;5859public DemoPanel(Object obj, DemoInstVarsAccessor demoInstVars) {60this.demoInstVars = demoInstVars;6162setLayout(new BorderLayout());63try {64if (obj instanceof String) {65className = (String) obj;66obj = Class.forName(className).getDeclaredConstructor().newInstance();67}68if (obj instanceof Component) {69add((Component) obj);70}71if (obj instanceof Surface) {72add("South", tools = new Tools(surface = (Surface) obj, demoInstVars));73}74if (obj instanceof CustomControlsContext) {75ccc = (CustomControlsContext) obj;76Component[] cmps = ccc.getControls();77String[] cons = ccc.getConstraints();78for (int i = 0; i < cmps.length; i++) {79add(cmps[i], cons[i]);80}81}82} catch (Exception e) {83Logger.getLogger(DemoPanel.class.getName()).log(Level.SEVERE, null,84e);85}86}8788public void start() {89if (surface != null) {90surface.startClock();91}92if (tools != null && surface != null) {93if (tools.startStopB != null && tools.startStopB.isSelected()) {94surface.animating.start();95}96}97if (ccc != null98&& demoInstVars.getCcthreadCB() != null99&& demoInstVars.getCcthreadCB().isSelected()) {100ccc.handleThread(START);101}102}103104public void stop() {105if (surface != null) {106if (surface.animating != null) {107surface.animating.stop();108}109surface.bimg = null;110}111if (ccc != null) {112ccc.handleThread(STOP);113}114}115116public void setDemoBorder(JPanel p) {117int top = (p.getComponentCount() + 1 >= 3) ? 0 : 5;118int left = ((p.getComponentCount() + 1) % 2) == 0 ? 0 : 5;119EmptyBorder eb = new EmptyBorder(top, left, 5, 5);120SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);121setBorder(new CompoundBorder(eb, sbb));122}123}124125126