summaryrefslogtreecommitdiffstats
path: root/src/net/java/joglutils/lighting/MaterialPanel.java
blob: bfa02ba29a41e26380c01fbfcc0e7816b22f9ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (c) 2006 Erik Tollerud (erik.tollerud@gmail.com) All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *    
 * - Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *   
 * The names of Erik Tollerud, Sun Microsystems, Inc. or the names of
 * contributors may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *    
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ERIK TOLLERUD,
 * SUN MICROSYSTEMS, INC. ("SUN"), AND SUN'S LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL BEN
 * CHAPPELL, SUN, OR SUN'S LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT 
 * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF BEN
 * CHAPPELL OR SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *   
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 */

package net.java.joglutils.lighting;

/**
 *
 * @author  Erik J. Tollerud
 */
public class MaterialPanel extends javax.swing.JPanel {
    
    /** Creates new form MaterialPanel */
    public MaterialPanel() {
        initComponents();
        matToPanel(new Material());
    }
    public MaterialPanel(Material m) {
        initComponents();
        matToPanel(m);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        shininessField = new javax.swing.JFormattedTextField(java.text.NumberFormat.getInstance());
        jLabel1 = new javax.swing.JLabel();
        specularButton = new javax.swing.JButton();
        diffuseButton = new javax.swing.JButton();
        ambientButton = new javax.swing.JButton();
        emissiveButton = new javax.swing.JButton();

        shininessField.setText("num");

        jLabel1.setText("Shininess");

        specularButton.setText("Specular");
        specularButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorAction(evt);
            }
        });

        diffuseButton.setText("Diffuse");
        diffuseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorAction(evt);
            }
        });

        ambientButton.setText("Ambient");
        ambientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorAction(evt);
            }
        });

        emissiveButton.setText("Emissive");
        emissiveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorAction(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(layout.createSequentialGroup()
                                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(diffuseButton))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(23, 23, 23)
                                .add(ambientButton)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(specularButton)
                            .add(emissiveButton)))
                    .add(layout.createSequentialGroup()
                        .add(56, 56, 56)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(shininessField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(ambientButton)
                    .add(specularButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(diffuseButton)
                    .add(emissiveButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(shininessField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void colorAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorAction
        javax.swing.JButton src = (javax.swing.JButton)evt.getSource();
        java.awt.Color c = javax.swing.JColorChooser.showDialog(this,src.getText()+" Color",src.getForeground());
        if (c != null)
            src.setForeground(c);
    }//GEN-LAST:event_colorAction
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ambientButton;
    private javax.swing.JButton diffuseButton;
    private javax.swing.JButton emissiveButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JFormattedTextField shininessField;
    private javax.swing.JButton specularButton;
    // End of variables declaration//GEN-END:variables
    public void matToPanel(Material m) {
        this.shininessField.setValue(m.getShininess());
        this.ambientButton.setForeground(m.getAmbient());
        this.diffuseButton.setForeground(m.getDiffuse());
        this.specularButton.setForeground(m.getSpecular());
        this.emissiveButton.setForeground(m.getEmissive());
        
    }
    public void panelToMat(Material m) {
        m.setShininess(((Number)this.shininessField.getValue()).floatValue());
        m.setAmbient(this.ambientButton.getForeground());
        m.setDiffuse(this.diffuseButton.getForeground());
        m.setSpecular(this.specularButton.getForeground());
        m.setEmissive(this.emissiveButton.getForeground());
    }
    public static void main(String[] args) {
        Material m = new Material();
        MaterialPanel mp = new MaterialPanel(m);
        net.java.joglutils.JPanelDialog jdp = new net.java.joglutils.JPanelDialog(mp);
        jdp.setTitle("the new title");
        System.err.println("bf modal show");
        boolean accept = jdp.showAsModal();
        System.err.println("after modal show");
        if (accept) {
            mp.panelToMat(m);
            System.err.println("shininess:"+m.getShininess()+" Emissive color:"+m.getEmissive());
        } else
            System.err.println("cancelled");
    }
            
}