root/graph/JavaPopWeb/src/jp/ac/nime/computer/grpsimulator/DlgAnalyzeReflectance.java

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. DlgAnalyzeReflectance
  2. resetState
  3. getParameterType
  4. getParameters
  5. stateChanged
  6. addActionListener
  7. removeActionListener
  8. requireSrc
  9. writeImage
  10. drawArrow

package jp.ac.nime.computer.grpsimulator;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.TitledBorder;
import java.io.*;
import java.util.*;
import jp.ac.nime.computer.grpsimulator.VecPr.*;

/** 反射用の設定ダイアログ
 * @version 1.0.0
 * @author  kikuchi
 */
public class DlgAnalyzeReflectance extends JPanel
        implements ChangeListener, ParameterSetting, OperationDialog
{
        private JLabel[] jL_;
        private JSlider[] jS_;
        private ImageViewPanel  canvas_;
        private String  srcPath_;
        private BufferedImage   img_;   // パネルに設定する画像
        private ActionListener actionListener_;
        
        /** Creates new form DlgContrast */
        public DlgAnalyzeReflectance() {
                setLayout(new BorderLayout());
                setBorder(new TitledBorder(GrpSim.res_.getString("DlgDescriptReflectanceLabel")));
                setMaximumSize(new Dimension(320, 300));
                setMinimumSize(new Dimension(320, 300));
                setPreferredSize(new Dimension(320, 300));
                
                JPanel jPanel0 = new JPanel();
                //jPanel0.setLayout(new BoxLayout(jPanel0, BoxLayout.Y_AXIS));
                GridBagLayout gbl = new GridBagLayout();
                jPanel0.setLayout(gbl);
                GridBagConstraints gbc = new GridBagConstraints();

                // 反射の状態を示す図
                canvas_ = new ImageViewPanel(300, 120);

                gbc.fill = GridBagConstraints.BOTH;
                gbc.weightx = 1.0;
                gbc.weighty = 1.0;
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = 4;
                gbc.gridheight = 8;
                gbl.setConstraints(canvas_, gbc);

                // 配列
                jL_ = new JLabel[8];
                jS_ = new JSlider[8];
                // 環境光
                jS_[0] = new JSlider(0, 80);
                jS_[0].setMajorTickSpacing(10);
                jS_[0].setMinorTickSpacing(1);
                jS_[0].setPaintLabels(false);
                jS_[0].setPaintTicks(false);
                jS_[0].setValue(20);
                jS_[0].addChangeListener(this);
                
                jL_[0] = new JLabel();
                jL_[0].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider1Label") + "(" + jS_[0].getValue() + ")");

                gbc.gridx = 0;
                gbc.gridy = 8;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[0], gbc);
                gbc.gridx = 2;
                gbc.gridy = 8;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[0], gbc);

                
                // 拡散反射率R 
                jS_[1] = new JSlider(0, 100);
                jS_[1].setMajorTickSpacing(25);
                jS_[1].setMinorTickSpacing(10);
                jS_[1].setPaintLabels(false);
                jS_[1].setPaintTicks(false);
                jS_[1].setValue(100);
                jS_[1].addChangeListener(this);
                jL_[1] = new JLabel();
                jL_[1].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider2Label")  + "(" + jS_[1].getValue() + ")");

                gbc.gridx = 0;
                gbc.gridy = 9;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[1], gbc);
                gbc.gridx = 2;
                gbc.gridy = 9;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[1], gbc);
                // 拡散反射率G
                jS_[2] = new JSlider(0, 100);
                jS_[2].setMajorTickSpacing(25);
                jS_[2].setMinorTickSpacing(10);
                jS_[2].setPaintLabels(false);
                jS_[2].setPaintTicks(false);
                jS_[2].setValue(1);
                jS_[2].addChangeListener(this);
                jL_[2] = new JLabel();
                jL_[2].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider3Label") + "(" + jS_[2].getValue() + ")");

                gbc.gridx = 0;
                gbc.gridy = 10;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[2], gbc);
                gbc.gridx = 2;
                gbc.gridy = 10;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[2], gbc);
                // 拡散反射率B
                jS_[3] = new JSlider(0, 100);
                jS_[3].setMajorTickSpacing(25);
                jS_[3].setMinorTickSpacing(10);
                jS_[3].setPaintLabels(false);
                jS_[3].setPaintTicks(false);
                jS_[3].setValue(1);
                jS_[3].addChangeListener(this);
                jL_[3] = new JLabel();
                jL_[3].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider4Label") + "(" + jS_[3].getValue() + ")");

                gbc.gridx = 0;
                gbc.gridy = 11;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[3], gbc);
                gbc.gridx = 2;
                gbc.gridy = 11;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[3], gbc);

                // 鏡面反射係数
                jS_[4] = new JSlider(0, 100);
                jS_[4].setMajorTickSpacing(25);
                jS_[4].setMinorTickSpacing(10);
                jS_[4].setPaintLabels(false);
                jS_[4].setPaintTicks(false);
                jS_[4].setValue(100);
                jS_[4].addChangeListener(this);
                jL_[4] = new JLabel();
                jL_[4].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider5Label") + "(" + jS_[4].getValue() + ")");

                gbc.gridx = 0;
                gbc.gridy = 12;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[4], gbc);
                gbc.gridx = 2;
                gbc.gridy = 12;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[4], gbc);

                // 鏡面反射 収束性
                jS_[5] = new JSlider(0, 80);
                jS_[5].setMajorTickSpacing(25);
                jS_[5].setMinorTickSpacing(10);
                jS_[5].setPaintLabels(false);
                jS_[5].setPaintTicks(false);
                jS_[5].setValue(80);
                jS_[5].addChangeListener(this);
                jL_[5] = new JLabel();
                jL_[5].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider6Label") + "(" + jS_[5].getValue() + ")");

                gbc.gridx = 0;
                gbc.gridy = 13;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[5], gbc);
                gbc.gridx = 2;
                gbc.gridy = 13;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[5], gbc);

                // 光源角度
                jS_[6] = new JSlider(0, 180);
                jS_[6].setMajorTickSpacing(45);
                jS_[6].setMinorTickSpacing(10);
                jS_[6].setPaintLabels(false);
                jS_[6].setPaintTicks(false);
                jS_[6].setValue(128);
                jS_[6].addChangeListener(this);
                jL_[6] = new JLabel();
                jL_[6].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider7Label") + "(" + jS_[6].getValue() + ")");

                gbc.gridx = 0;
                gbc.gridy = 14;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[6], gbc);
                gbc.gridx = 2;
                gbc.gridy = 14;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[6], gbc);

                // 視線角度
                jS_[7] = new JSlider(0, 180);
                jS_[7].setMajorTickSpacing(45);
                jS_[7].setMinorTickSpacing(10);
                jS_[7].setPaintLabels(false);
                jS_[7].setPaintTicks(false);
                jS_[7].setValue(16);
                jS_[7].addChangeListener(this);
                jL_[7] = new JLabel();
                jL_[7].setText(GrpSim.res_.getString("DlgDescriptReflectanceSlider8Label") + "(" + jS_[7].getValue() + ")");
                
                gbc.gridx = 0;
                gbc.gridy = 15;
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbl.setConstraints(jL_[7], gbc);
                gbc.gridx = 2;
                gbc.gridy = 15;
                gbc.gridwidth = 2;
                gbc.gridheight = 1;
                gbl.setConstraints(jS_[7], gbc);

                jPanel0.add(canvas_);
                for (int i = 0; i < 8; i ++) {
                        jPanel0.add(jL_[i]);
                        jPanel0.add(jS_[i]);
                }
                add(jPanel0, BorderLayout.NORTH);
                // 画像バッファ
                img_ = new BufferedImage(300, 120, BufferedImage.TYPE_INT_ARGB);
                canvas_.setImage(img_);
                writeImage(img_, jS_[6].getValue(), jS_[7].getValue());
                
        }

        /** ダイアログの状態をリセットする */
        public void resetState() {
                actionListener_.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"ParameterChanged"));
        }       

        public int getParameterType() {
                return ParameterSetting.PARAM_TYPE_DESCRIPT_REFLECTANCE;
        }    
        
        public int[] getParameters() {
                int ret[] = new int[8];
                for (int i = 0; i < 8; i++) {
                        ret[i] = jS_[i].getValue();
                }
                return ret;
        }    
                
        public void stateChanged(ChangeEvent evt) {
                // 再描画
                writeImage(img_, jS_[6].getValue(), jS_[7].getValue());
                repaint();
                // ソース
                JSlider source = (JSlider) evt.getSource();
                
                // ラベルを変更する
                for (int i = 0; i < 8; i ++) {
                        // ソースを探す
                        if (source == jS_[i]) {
                                // ラベル
                                int l = i + 1;
                                String str = "DlgDescriptReflectanceSlider"+ l + "Label";
                                jL_[i].setText(GrpSim.res_.getString(str) + "(" + jS_[i].getValue() + ")");
                        }
                }
                
                if ( !source.getValueIsAdjusting() ) 
                        actionListener_.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"ParameterChanged"));
        }
        
        public void addActionListener(ActionListener listener) {
                actionListener_ = AWTEventMulticaster.add(actionListener_, listener);
        }
        public void removeActionListener(ActionListener listener) {
                actionListener_ = AWTEventMulticaster.remove(actionListener_, listener);
        }
        
        
        /** 変換元画像を必要とするかどうかを返す
         * @return 変換元画像を必要とするならtrueを返す
         */
        public boolean requireSrc() {
                return false;
        }
        
        /** 光源と視線の関係の図を動かす
         */
        private static void writeImage(BufferedImage img, int lightDeg, int cameraDeg) {
                Graphics2D g = img.createGraphics();
                // 白でクリア
                g.setBackground(Color.white);
                g.clearRect(0, 0, img.getWidth(), img.getHeight());
                g.setStroke(new BasicStroke(0.0f)); // 一番細く 引数はfloat
                
                g.setPaint(Color.black);
                int y0 = 100;
                int x0 = 90;
                int arrowLen = 70;
                // 面
                g.setStroke(new BasicStroke(2.0f));
                g.draw(new Line2D.Double(x0 - 80, y0, x0 + 80, y0));
                // 矢印
                g.setStroke(new BasicStroke(1.5f));
                g.setPaint(Color.black);
                drawArrow(g, x0, y0, 90, arrowLen, "N"); // 法線
                g.setPaint(Color.red);
                drawArrow(g, x0, y0, lightDeg, arrowLen, "L");
                g.setPaint(Color.orange);
                drawArrow(g, x0, y0, 180 - lightDeg, arrowLen, "R");
                g.setPaint(Color.blue);
                drawArrow(g, x0, y0, cameraDeg, arrowLen, "V");
                // 矢印の角度を示す円弧を描く
                int kakudoLen = 40;
                int sita = lightDeg - 90;
                g.setPaint(Color.black);
                g.draw(new Arc2D.Double( x0 - kakudoLen, y0 - kakudoLen, kakudoLen * 2, kakudoLen * 2, 90, sita, Arc2D.OPEN));
                // 文字を書く
                int x, y;
                double rad;
                rad = (90 + (sita / 2)) * Math.PI / 180.0;
                x = x0 + (int)(Math.cos(rad) * (double)kakudoLen);
                y = y0 - (int)(Math.sin(rad) * (double)kakudoLen) - 2;
                g.drawString("θ", x, y); 
                // 矢印の角度を示す円弧を描く
                kakudoLen -= 5;
                g.draw(new Arc2D.Double( x0 - kakudoLen, y0 - kakudoLen, kakudoLen * 2, kakudoLen * 2, 90, -sita, Arc2D.OPEN));
                // 文字を書く
                rad = (90 + (-sita / 2)) * Math.PI / 180.0;
                x = x0 + (int)(Math.cos(rad) * (double)kakudoLen);
                y = y0 - (int)(Math.sin(rad) * (double)kakudoLen) - 2;
                g.drawString("θ", x, y); 
                // 矢印の角度を示す円弧を描く
                g.setPaint(Color.black);
                int revld = 180 - lightDeg;     // 反射
                int ganma = revld - cameraDeg; // γ
                kakudoLen -= 5;
                g.draw(new Arc2D.Double( x0 - kakudoLen, y0 - kakudoLen, kakudoLen * 2, kakudoLen * 2, cameraDeg, ganma, Arc2D.OPEN));
                // 文字を書く
                rad = (cameraDeg + (ganma / 2)) * Math.PI / 180.0;
                x = x0 + (int)(Math.cos(rad) * (double)kakudoLen);
                y = y0 - (int)(Math.sin(rad) * (double)kakudoLen) - 2;
                g.drawString("γ", x, y); 
                
                g.setStroke(new BasicStroke(0.0f)); // 一番細く 引数はfloat
                // 説明文
                g.setFont( new Font( "SansSerif", Font.PLAIN, 10 ) ); 
                x = x0 + 70;
                y = 20;
                g.drawString(GrpSim.res_.getString("DlgAnalyzeReflectanceN"), x, y);
                y += 15;
                g.drawString(GrpSim.res_.getString("DlgAnalyzeReflectanceL"), x, y);
                y += 15;
                g.drawString(GrpSim.res_.getString("DlgAnalyzeReflectanceR"), x, y);
                y += 15;
                g.drawString(GrpSim.res_.getString("DlgAnalyzeReflectanceV"), x, y);
                y += 15;
                g.setFont( new Font( "SansSerif", Font.PLAIN, 9 ) ); 
                g.drawString(GrpSim.res_.getString("DlgAnalyzeReflectanceNN"), x, y);
                
                g.dispose();
        }
        
        /** 矢印を書く
         */
        private static void drawArrow(Graphics2D g, int cx, int cy, int deg, int len, String str) {
                double rad = deg * Math.PI/180d;
                // 現在の変換テーブル
                AffineTransform aT = g.getTransform();
                AffineTransform af = new AffineTransform();
                // 平行移動
                af.setToTranslation(cx, cy);
                // 回転
                af.rotate((- rad));
                g.setTransform(af);
                // 矢印を 横に書く
                g.draw(new Line2D.Double(0, 0, len, 0));
                g.draw(new Line2D.Double(len, 0, len - 5, - 4));
                g.draw(new Line2D.Double(len, 0, len - 5, + 4));
                g.setTransform(aT);
                // 文字位置を計算
                int x, y;
                x = cx + (int)(Math.cos(rad) * (double)len) + 2;
                y = cy - (int)(Math.sin(rad) * (double)len) - 2;
                g.drawString(str, x, y  ); 
        }
        
}

/* [<][>][^][v][top][bottom][index][help] */