mcapplet.java
来自「由java实现的marching cube」· Java 代码 · 共 921 行 · 第 1/2 页
JAVA
921 行
{ mButtonAction(ba); } }); pButton.setBackground(bgColor); pButton.add(this.mButton[2*index]); pButton.add(this.mButton[2*index+1]); this.pControlWE.add(pButton); } JPanel pDummy = new JPanel(); pDummy.setLayout(new GridLayout(2, 1)); this.pControlW.add(pDummy, "South"); this.bComplementary = new JButton("Switch to complementary case"); this.bComplementary.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { bComplementaryAction(ae); } }); this.bComplementary.setBackground(bgColor); pDummy.add(this.bComplementary); this.cbAmbigous = new JCheckBox("Use ambigous cases resolution"); this.cbAmbigous.setEnabled(true); this.cbAmbigous.setBackground(bgColor); this.cbAmbigousListener = new ActionListener() { public void actionPerformed(ActionEvent ae) { cbAmbigousAction(ae); } }; this.cbAmbigous.addActionListener(this.cbAmbigousListener); pDummy.add(this.cbAmbigous); int w = aWidth - this.pControlW.getSize().width; int h = aHeight; this.canvas = new MCCanvas(w, h); this.canvas.addKeyListener(this); this.canvas.img = this.createImage(w, h); this.canvas.dbg = this.canvas.img.getGraphics(); this.getContentPane().add(this.canvas, "Center"); this.matchCbCube(); } /** * automatically called when applet starts */ public void start() { // updates the size of the canvas depending on the control panels this.canvas.changeSize(this.getSize().width - this.pControlW.getSize().width - this.pControlE.getSize().width, this.pControlW.getSize().height); this.canvas.img = this.createImage(this.canvas.getSize().width, this.canvas.getSize().height); this.canvas.dbg = this.canvas.img.getGraphics(); MCRenderer.setWindow(this.canvas.img); MCRenderer.buildPrMatrix(MCRenderer.DEFAULT_FOCAL); this.started = true; this.resetAction(null); this.canvas.repaint(); } /** * automatically called when applet stops */ public void stop() { } /** * automatically called when applet get destroyed */ public void destroy() { } /** * updates the combo box that allow the user to choose the selected cube */ private void matchCbCube() { this.cbCube.removeAllItems(); Vector cubes = this.canvas.mesh.getCubes(); for (int index = 0; index < cubes.size(); index++) { this.cbCube.addItem(cubes.elementAt(index)); } this.cbCube.setSelectedIndex(0); } /** * manages action on the slider bars * @param ce event from the modified slider bar */ private void sVertexAction(ChangeEvent ce) { MCCube cCube = (MCCube) this.cbCube.getSelectedItem(); for (int index = 0; index < this.sVertex.length; index++) { if (ce.getSource() == this.sVertex[index]) { int value = this.sVertex[index].getValue(); cCube.v[index].weight.value = value; cCube.computeEdges(); for (int n = 0; n < cCube.neighbours.size(); n++) { MCCube nc = ((MCNeighbour) cCube.neighbours.elementAt(n)).cube; nc.computeEdges(); } } } //this.cbAmbigous.setEnabled(MCCube.isAmbigous(cCube.caseNumber())); if (MCApplet.this.started) this.canvas.repaint(); } /** * updates the textfield that displays the case number * @param ce event from the modified slider bar */ private void sVertexActionTfCase(ChangeEvent ce) { MCCube cCube = (MCCube) this.cbCube.getSelectedItem(); this.tfCase.removeCaretListener(this.tfCaseListener); this.tfCase.setText((new Integer(cCube.caseNumber())).toString()); this.tfCase.addCaretListener(this.tfCaseListener); } /** * manages add/delete cube actions * @param e event from the add or delete button */ private void addSupAction(ActionEvent e) { if(e.getSource()==this.addB) { int position = 0; if (this.face1.isSelected()) position = MCNeighbour.SOUTH; if (this.face2.isSelected()) position = MCNeighbour.NORTH; if (this.face3.isSelected()) position = MCNeighbour.EAST; if (this.face4.isSelected()) position = MCNeighbour.WEST; if (this.face5.isSelected()) position = MCNeighbour.DOWN; if (this.face6.isSelected()) position = MCNeighbour.UP; if (position != 0) { String input = JOptionPane.showInputDialog(this, "Please enter a name for the new cube:", "Cube identifier", JOptionPane.INFORMATION_MESSAGE); if (input != null) this.canvas.mesh.addCube((MCCube) (cbCube.getSelectedItem()), position, input); } } if(e.getSource()==this.supB) { if (this.canvas.mesh.getCubes().size() > 1) this.canvas.mesh.deleteCube((MCCube) (cbCube.getSelectedItem())); } this.matchCbCube(); this.matchDisabledFaces(); if (MCApplet.this.started) this.canvas.repaint(); } /** * updates radio button allowing the user to choose a face when adding a new cube, depending on the neighbourhood of the current cube */ private void matchDisabledFaces() { MCCube cube = (MCCube)(cbCube.getSelectedItem()); boolean n=true; boolean s=true; boolean e=true; boolean w=true; boolean u=true; boolean d=true; for(int i=0; i<cube.neighbours.size(); i++) { MCNeighbour voisin =(MCNeighbour) cube.neighbours.elementAt(i); switch(voisin.position) { case MCNeighbour.NORTH: n &=false; break; case MCNeighbour.SOUTH: s &=false; break; case MCNeighbour.EAST: e &=false; break; case MCNeighbour.WEST: w &=false; break; case MCNeighbour.UP: u &=false; break; case MCNeighbour.DOWN: d &=false; break; } } face2.setEnabled(n); face1.setEnabled(s); face3.setEnabled(e); face4.setEnabled(w); face5.setEnabled(d); face6.setEnabled(u); face1.setSelected(false); face2.setSelected(false); face3.setSelected(false); face4.setSelected(false); face5.setSelected(false); face6.setSelected(false); face7.setSelected(true); } /** * manages cube selection from the dedicated combo box * @param ae event from the cube selection combo box */ private void cbCubeChange(ActionEvent ae) { MCCube cube = (MCCube)(cbCube.getSelectedItem()); if (cube != null) { for (int index = 0; index < this.sVertex.length; index++) { int p = cube.v[index].weight.value; this.sVertex[index].setValue(p); } this.matchDisabledFaces(); this.cbVisible.removeActionListener(this.cbVisibleListener); this.cbVisible.setSelected(cube.hidden); this.cbVisible.addActionListener(this.cbVisibleListener); } if (MCApplet.this.started) this.canvas.repaint(); } /** * manages reference value modification * @param ce event from the reference value textfield */ private void valueAction(CaretEvent ce) { try { double v = Double.parseDouble(this.value.getText()); MCCube.seekValue = v; Vector theCubes = this.canvas.mesh.getCubes(); for(int i=0; i<theCubes.size(); i++) { ((MCCube)theCubes.elementAt(i)).computeEdges(); } if (MCApplet.this.started) this.canvas.repaint(); } catch(NumberFormatException ne) { } } /** * manages material selection * @param ae event from the material selection combo box */ private void cbMatAction(ActionEvent ae) { MCRenderer.material = (MCMaterial) cbMat.getSelectedItem(); if (MCApplet.this.started) this.canvas.repaint(); } /** * resets both view and mesh * @param ae event from the reset button */ private void resetAction(ActionEvent ae) { this.canvas.mesh = new MCMesh(); this.matchCbCube(); if (MCApplet.this.started) { this.repaint(); this.canvas.repaint(); } } /** * switch to translation mode * @param ae event from the translaion radio button */ private void transAction(ActionEvent ae) { this.canvas.tfMode = MCCanvas.TF_MODE_TRANSLATION; } /** * switch to rotation mode * @param ae event from the rotation radio button */ private void rotAction(ActionEvent ae) { this.canvas.tfMode = MCCanvas.TF_MODE_ROTATION; } /** * manages complementary case action * @param ae event from the dedicated checkbox */ private void bComplementaryAction(ActionEvent ae) { MCCube cCube = (MCCube) this.cbCube.getSelectedItem(); this.tfCase.setText((new Integer(255 - cCube.caseNumber())).toString()); if (MCApplet.this.started) this.canvas.repaint(); } /** * manages min/max actions * @param be event from a min/max button */ private void mButtonAction(ActionEvent ba) { JButton b = (JButton)ba.getSource(); for (int i=0; i<this.mButton.length; i++) { if(mButton[i]==b) { if(i%2 == 0) { this.sVertex[i/2].setValue((-1)*DEFAULT_RANGE); }else { this.sVertex[i/2].setValue(DEFAULT_RANGE); } } } } /** * manages explicit case number specification * @param ce event from the case number textfield */ private void tfCaseAction(CaretEvent ce) { try { int caseNumber = Integer.parseInt(this.tfCase.getText()); if (caseNumber < 0 || caseNumber > 255) throw new NumberFormatException(); for (int p = 7; p >= 0; p--) { this.sVertex[p].removeChangeListener(this.sVertexListenerTfCase); int n = (int) Math.pow(2, p); if (caseNumber >= n) { this.sVertex[p].setValue(DEFAULT_RANGE); caseNumber -= n; } else { this.sVertex[p].setValue(-DEFAULT_RANGE); } this.sVertex[p].addChangeListener(this.sVertexListenerTfCase); } } catch (NumberFormatException nfe) { } } // keyboard events public void keyPressed(KeyEvent ke) { switch (ke.getKeyCode()) { // enables transformations along z axis case KeyEvent.VK_SHIFT: this.shift = true; break; } } public void keyReleased(KeyEvent ke) { switch (ke.getKeyCode()) { // enables transformations along xy axis case KeyEvent.VK_SHIFT: this.shift = false; break; } } public void keyTyped(KeyEvent ke) { switch (ke.getKeyChar()) { // cycle through all the cubes case ' ': int index = this.cbCube.getSelectedIndex() + 1; if (index >= this.cbCube.getItemCount()) { index = 0; } this.cbCube.setSelectedIndex(index); break; } } /** * toggles selected cube visibility * @param ae event from the dedicated checkbox */ private void cbVisibleAction(ActionEvent ae) { MCCube cCube = (MCCube) this.cbCube.getSelectedItem(); cCube.hidden = !cCube.hidden; if (this.started) this.canvas.repaint(); } /** * toggles ambigous case resolution * @ ae event from the dedicated checkbox */ private void cbAmbigousAction(ActionEvent ae) { MCCube cCube = (MCCube) this.cbCube.getSelectedItem(); cCube.amb = !cCube.amb; if (this.started) this.canvas.repaint(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?