📄 mcapplet.java
字号:
/** Marching Cubes Tutorial Applet * Copyright (C) 2002 - GERVAISE Raphael & RICHARD Karen** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*//** * applet designed to view and experiment with marching cubes algorithm * @author GERVAISE Raphael & RICHARD Karen */package marchingcubes;// not much comments: interface work... better have a glance at MCCube and the others...import java.awt.Color;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.KeyListener;import java.awt.event.KeyEvent;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.JApplet;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JComboBox;import javax.swing.JButton;import javax.swing.ButtonGroup;import javax.swing.JCheckBox;import javax.swing.JLabel;import javax.swing.JTextField;import javax.swing.JSlider;import javax.swing.BoxLayout;import javax.swing.BorderFactory;import javax.swing.SwingConstants;import javax.swing.event.CaretListener;import javax.swing.event.CaretEvent;import javax.swing.event.ChangeListener;import javax.swing.event.ChangeEvent;import javax.swing.JOptionPane;import javax.swing.border.TitledBorder;import java.util.Vector;public class MCApplet extends JApplet implements KeyListener{ // range value of the slider bars public static final int DEFAULT_RANGE = 100; // background color of the interface private Color bgColor = new Color(230, 210, 185); // true when the applet starts protected static boolean started = false; // visual components private MCCanvas canvas; private JPanel pControlW; private JPanel pControlE; private JPanel pControlWW; private JPanel pControlWE; private JPanel pOperation; private ButtonGroup transfo; private JRadioButton trans; private JRadioButton rot; private JPanel pRender; private JComboBox cbMat; private ButtonGroup rendu; private JRadioButton lambert; private JRadioButton phong; private ButtonGroup visu; protected static JRadioButton wCube; private JRadioButton woCube; private JPanel pModel; private JLabel reference; private JTextField value; private JLabel lCase; private JTextField tfCase; private JCheckBox cbVisible; private ActionListener cbVisibleListener; private CaretListener tfCaseListener; private ButtonGroup faceBox; private JRadioButton face1; private JRadioButton face2; private JRadioButton face3; private JRadioButton face4; private JRadioButton face5; private JRadioButton face6; private JRadioButton face7; private JButton addB; private JButton supB; protected static JComboBox cbCube; private JButton reset; private JSlider[] sVertex; private ChangeListener sVertexListener; private ChangeListener sVertexListenerTfCase; private JButton[] mButton; private static JButton bComplementary; private static ActionListener bComplementaryListener; protected static JCheckBox cbAmbigous; private ActionListener cbAmbigousListener; protected static boolean shift; /** * automatically called when applet initializes */ public void init() { this.addKeyListener(this); MCMaterial.fillMaterials(); int aWidth; int aHeight; try { // gets size specified in HTML document aWidth = Integer.parseInt(this.getParameter("width")); aHeight = Integer.parseInt(this.getParameter("height")); }catch (NumberFormatException nfe) { this.showStatus("Applet could not be initialized"); return; } this.getContentPane().setLayout(new BorderLayout()); this.pControlW=new JPanel(); this.pControlW.setBackground(bgColor); this.pControlW.setLayout(new BorderLayout()); this.getContentPane().add(this.pControlW, "East"); this.pControlWE=new JPanel(); this.pControlWE.setBackground(bgColor); this.pControlWE.setLayout(new GridLayout(8, 1)); this.pControlW.add(this.pControlWE, "East"); this.pControlWW=new JPanel(); this.pControlWW.setBackground(bgColor); this.pControlWW.setLayout(new GridLayout(8, 1)); this.pControlW.add(this.pControlWW, "West"); this.pControlE=new JPanel(); this.pControlE.setBackground(bgColor); this.pControlE.setLayout(new BoxLayout(this.pControlE, BoxLayout.Y_AXIS)); this.getContentPane().add(this.pControlE, "West"); this.pOperation=new JPanel(); this.pOperation.setBackground(bgColor); this.pOperation.setLayout(new GridLayout(2,1)); this.pOperation.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transformation", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION)); this.transfo=new ButtonGroup(); this.trans=new JRadioButton("Translate", false); this.trans.setBackground(bgColor); this.trans.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { transAction(ae); } } ); this.rot=new JRadioButton("Rotate", true); this.rot.setBackground(bgColor); this.rot.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { rotAction(ae); } } ); this.transfo.add(this.trans); this.transfo.add(this.trans); this.transfo.add(this.rot); this.pOperation.add(this.trans); this.pOperation.add(this.rot); this.pControlE.add(this.pOperation); pRender = new JPanel(); this.pRender.setBackground(bgColor); this.pRender.setLayout(new GridLayout(7,1)); pRender.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Rendering", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION)); this.cbMat = new JComboBox(); this.cbMat.setBackground(bgColor); for (int index = 0; index < MCMaterial.materials.size(); index++) { this.cbMat.addItem(MCMaterial.materials.elementAt(index)); } this.cbMat.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { cbMatAction(ae); } }); this.pRender.add(this.cbMat); JPanel saut = new JPanel(); saut.setBackground(bgColor); this.pRender.add(saut); this.rendu=new ButtonGroup(); this.lambert=new JRadioButton("Lambert", true); this.lambert.setBackground(bgColor); this.lambert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { MCRenderer.liModl = MCRenderer.LI_MODL_LAMBERT; if (MCApplet.this.started) canvas.repaint(); } } ); this.phong=new JRadioButton("Phong", false); this.phong.setBackground(bgColor); this.phong.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { MCRenderer.liModl = MCRenderer.LI_MODL_PHONG; if (MCApplet.this.started) canvas.repaint(); } } ); this.rendu.add(this.lambert); this.rendu.add(this.phong); this.pRender.add(this.lambert); this.pRender.add(this.phong); saut = new JPanel(); saut.setBackground(bgColor); this.pRender.add(saut); this.visu = new ButtonGroup(); this.wCube = new JRadioButton("With cube", true); this.wCube.setBackground(bgColor); this.wCube.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if (MCApplet.this.started) canvas.repaint(); } } ); this.woCube = new JRadioButton("Without cube", false); this.woCube.setBackground(bgColor); this.woCube.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if (MCApplet.this.started) canvas.repaint(); } }); this.visu.add(this.wCube); this.pRender.add(this.wCube); this.visu.add(this.woCube); this.pRender.add(this.woCube); this.pControlE.add(pRender); pModel = new JPanel(); this.pModel.setBackground(bgColor); this.pModel.setLayout(new GridLayout(15,1)); pModel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Modeling", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION)); this.reference = new JLabel("Isovalue", SwingConstants.CENTER); this.reference.setBackground(bgColor); this.pModel.add(this.reference); this.value = new JTextField("0.00"); this.value.setBackground(bgColor); this.value.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent ce) { valueAction(ce); } } ); this.pModel.add(this.value); this.lCase = new JLabel("Case number", SwingConstants.CENTER); this.lCase.setBackground(bgColor); this.pModel.add(this.lCase); this.tfCase = new JTextField("0"); this.tfCase.setBackground(bgColor); this.tfCaseListener = new CaretListener() { public void caretUpdate(CaretEvent ce) { tfCaseAction(ce); } }; this.tfCase.addCaretListener(this.tfCaseListener); this.pModel.add(this.tfCase); this.cbVisible = new JCheckBox("Hidden"); this.cbVisibleListener = new ActionListener() { public void actionPerformed(ActionEvent ae) { MCApplet.this.cbVisibleAction(ae); } }; this.cbVisible.addActionListener(this.cbVisibleListener); this.cbVisible.setBackground(bgColor); this.pModel.add(this.cbVisible); saut = new JPanel(); saut.setBackground(bgColor); this.pModel.add(saut); this.faceBox = new ButtonGroup(); this.face1 = new JRadioButton("face 0,1,2,3", false); this.face1.setBackground(bgColor); this.face2 = new JRadioButton("face 4,5,6,7", false); this.face2.setBackground(bgColor); this.face3 = new JRadioButton("face 1,2,5,6", false); this.face3.setBackground(bgColor); this.face4 = new JRadioButton("face 0,3,4,7", false); this.face4.setBackground(bgColor); this.face5 = new JRadioButton("face 0,1,4,5", false); this.face5.setBackground(bgColor); this.face6 = new JRadioButton("face 2,3,6,7", false); this.face6.setBackground(bgColor); this.face7 = new JRadioButton("", true); this.faceBox.add(this.face1); this.faceBox.add(this.face2); this.faceBox.add(this.face3); this.faceBox.add(this.face4); this.faceBox.add(this.face5); this.faceBox.add(this.face6); this.faceBox.add(this.face7); this.pModel.add(this.face1); this.pModel.add(this.face2); this.pModel.add(this.face3); this.pModel.add(this.face4); this.pModel.add(this.face5); this.pModel.add(this.face6); this.addB = new JButton("Add a cube"); this.addB.setBackground(bgColor); this.addB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addSupAction(e); } } ); this.pModel.add(this.addB); this.supB = new JButton("Del the cube"); this.supB.setBackground(bgColor); this.supB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addSupAction(e); } } ); this.pModel.add(this.supB); this.pControlE.add(this.pModel); this.reset = new JButton("Reset"); this.reset.setBackground(bgColor); this.reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { resetAction(ae); } }); this.pModel.add(this.reset); this.cbCube = new JComboBox(); this.cbCube.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Current cube", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION)); this.cbCube.setBackground(bgColor); this.pControlW.add(this.cbCube, "North"); this.cbCube.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { cbCubeChange(ae); } }); this.sVertexListener = new ChangeListener() { public void stateChanged(ChangeEvent ce) { MCApplet.this.sVertexAction(ce); } }; this.sVertexListenerTfCase = new ChangeListener() { public void stateChanged(ChangeEvent ce) { MCApplet.this.sVertexActionTfCase(ce); } }; this.sVertex = new JSlider[8]; this.mButton = new JButton[16]; for (int index = 0; index < this.sVertex.length; index++) { this.sVertex[index] = new JSlider(-DEFAULT_RANGE, DEFAULT_RANGE, 0); this.sVertex[index].setBackground(bgColor); this.sVertex[index].setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Vertex " + index + " (" + (int) Math.pow(2, index) + ")", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION)); this.sVertex[index].setMajorTickSpacing(DEFAULT_RANGE/2); this.sVertex[index].setMinorTickSpacing(1); this.sVertex[index].setSnapToTicks(true); this.sVertex[index].setPaintTicks(false); this.sVertex[index].setPaintLabels(true); this.sVertex[index].setPaintTrack(true); this.sVertex[index].addChangeListener(this.sVertexListener); this.sVertex[index].addChangeListener(this.sVertexListenerTfCase); this.pControlWW.add(this.sVertex[index]); JPanel pButton=new JPanel(); pButton.setBackground(bgColor); pButton.setLayout(new GridLayout(1, 2)); this.mButton[2*index] = new JButton("Min"); this.mButton[2*index].setBackground(bgColor); this.mButton[2*index].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ba) { mButtonAction(ba); } }); this.mButton[2*index+1] = new JButton("Max"); this.mButton[2*index+1].setBackground(bgColor); this.mButton[2*index+1].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ba)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -