]> icculus.org git repositories - divverent/nexuiz.git/blob - tools/ImgToMap/src/imgtomap/JFrameMain.java
err... better give that JSpinner a meaningful name
[divverent/nexuiz.git] / tools / ImgToMap / src / imgtomap / JFrameMain.java
1 /*
2  * JFrameMain.java
3  *
4  * Created on 17. Mai 2008, 15:02
5  */
6
7 package imgtomap;
8
9 import java.io.File;
10 import javax.swing.JFileChooser;
11 import javax.swing.JOptionPane;
12
13 /**
14  *
15  * @author  maik
16  */
17 public class JFrameMain extends javax.swing.JFrame {
18
19     /** Creates new form JFrameMain */
20     public JFrameMain() {
21         initComponents();
22         String testinput = "/mnt/data/nexuizsvn/nexuiz/trunk/tools/ImgToMap/test1.png";
23         String testoutput = "/tmp/test.map";
24         
25         if(new File(testinput).exists()) {
26             jTextFieldInput.setText(testinput);
27             jTextFieldOutput.setText(testoutput);
28         }
29         
30     }
31
32     /** This method is called from within the constructor to
33      * initialize the form.
34      * WARNING: Do NOT modify this code. The content of this method is
35      * always regenerated by the Form Editor.
36      */
37     @SuppressWarnings("unchecked")
38     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
39     private void initComponents() {
40
41         jPanel1 = new javax.swing.JPanel();
42         jTextFieldInput = new javax.swing.JTextField();
43         jButtonInput = new javax.swing.JButton();
44         jTextFieldOutput = new javax.swing.JTextField();
45         jButtonOutput = new javax.swing.JButton();
46         jSpinnerUnits = new javax.swing.JSpinner();
47         jLabel1 = new javax.swing.JLabel();
48         jSpinnerHeight = new javax.swing.JSpinner();
49         jLabel2 = new javax.swing.JLabel();
50         jButtonOK = new javax.swing.JButton();
51         jLabel3 = new javax.swing.JLabel();
52         jTextFieldTexture = new javax.swing.JTextField();
53         jCheckBoxDetail = new javax.swing.JCheckBox();
54         jLabel4 = new javax.swing.JLabel();
55         jSpinnerTextureScale = new javax.swing.JSpinner();
56
57         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
58         setTitle("ImgToMap");
59
60         jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Convert image to map"));
61
62         jButtonInput.setText("Select input file");
63         jButtonInput.addActionListener(new java.awt.event.ActionListener() {
64             public void actionPerformed(java.awt.event.ActionEvent evt) {
65                 jButtonInputActionPerformed(evt);
66             }
67         });
68
69         jButtonOutput.setText("Select output file");
70         jButtonOutput.addActionListener(new java.awt.event.ActionListener() {
71             public void actionPerformed(java.awt.event.ActionEvent evt) {
72                 jButtonOutputActionPerformed(evt);
73             }
74         });
75
76         jSpinnerUnits.setModel(new javax.swing.SpinnerNumberModel(256, 1, 2048, 1));
77
78         jLabel1.setText("Units per pixel:");
79
80         jSpinnerHeight.setModel(new javax.swing.SpinnerNumberModel(1024, 256, 16384, 1));
81
82         jLabel2.setText("Height for white:");
83
84         jButtonOK.setText("Start conversion!");
85         jButtonOK.addActionListener(new java.awt.event.ActionListener() {
86             public void actionPerformed(java.awt.event.ActionEvent evt) {
87                 jButtonOKActionPerformed(evt);
88             }
89         });
90
91         jLabel3.setText("Cover terrain surface with texture:");
92
93         jTextFieldTexture.setText("terrain/terrain1");
94
95         jCheckBoxDetail.setSelected(true);
96         jCheckBoxDetail.setText("make detail");
97
98         jLabel4.setText("Texture scale:");
99
100         jSpinnerTextureScale.setModel(new javax.swing.SpinnerNumberModel(0.5d, 0.25d, 16.0d, 0.25d));
101
102         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
103         jPanel1.setLayout(jPanel1Layout);
104         jPanel1Layout.setHorizontalGroup(
105             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
106             .addGroup(jPanel1Layout.createSequentialGroup()
107                 .addContainerGap()
108                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
109                     .addComponent(jTextFieldTexture, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
110                     .addComponent(jButtonOK)
111                     .addGroup(jPanel1Layout.createSequentialGroup()
112                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
113                             .addComponent(jLabel1)
114                             .addComponent(jSpinnerUnits, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
115                         .addGap(27, 27, 27)
116                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
117                             .addComponent(jLabel2)
118                             .addComponent(jSpinnerHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
119                         .addGap(24, 24, 24)
120                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
121                             .addComponent(jSpinnerTextureScale)
122                             .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
123                     .addComponent(jLabel3)
124                     .addComponent(jCheckBoxDetail)
125                     .addGroup(jPanel1Layout.createSequentialGroup()
126                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
127                             .addComponent(jTextFieldOutput, javax.swing.GroupLayout.Alignment.LEADING)
128                             .addComponent(jTextFieldInput, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE))
129                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
130                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
131                             .addComponent(jButtonInput, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
132                             .addComponent(jButtonOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))))
133                 .addContainerGap())
134         );
135         jPanel1Layout.setVerticalGroup(
136             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
137             .addGroup(jPanel1Layout.createSequentialGroup()
138                 .addContainerGap()
139                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
140                     .addComponent(jButtonInput)
141                     .addComponent(jTextFieldInput, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
142                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
143                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
144                     .addComponent(jButtonOutput)
145                     .addComponent(jTextFieldOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
146                 .addGap(16, 16, 16)
147                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
148                     .addComponent(jLabel1)
149                     .addComponent(jLabel2)
150                     .addComponent(jLabel4))
151                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
152                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
153                     .addComponent(jSpinnerUnits, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
154                     .addComponent(jSpinnerHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
155                     .addComponent(jSpinnerTextureScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
156                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
157                 .addComponent(jLabel3)
158                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
159                 .addComponent(jTextFieldTexture, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
160                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
161                 .addComponent(jCheckBoxDetail)
162                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
163                 .addComponent(jButtonOK)
164                 .addContainerGap())
165         );
166
167         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
168         getContentPane().setLayout(layout);
169         layout.setHorizontalGroup(
170             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
171             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
172                 .addContainerGap()
173                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
174                 .addContainerGap())
175         );
176         layout.setVerticalGroup(
177             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
178             .addGroup(layout.createSequentialGroup()
179                 .addContainerGap()
180                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
181                 .addContainerGap())
182         );
183
184         pack();
185     }// </editor-fold>//GEN-END:initComponents
186
187 private void jButtonInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInputActionPerformed
188     JFileChooser fc = new JFileChooser();
189     int ret = fc.showOpenDialog(this);
190     
191     if(ret == fc.APPROVE_OPTION) {
192         File f = fc.getSelectedFile();
193         if(f.exists()) {
194             jTextFieldInput.setText(f.getAbsolutePath());
195         }
196     }
197 }//GEN-LAST:event_jButtonInputActionPerformed
198
199 private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
200     this.setVisible(false);
201     Parameters p = new Parameters();
202     p.infile = jTextFieldInput.getText();
203     p.outfile = jTextFieldOutput.getText();
204     p.texture = jTextFieldTexture.getText();
205     p.pixelsize = (Integer)jSpinnerUnits.getValue();
206     p.height = (Integer)jSpinnerHeight.getValue();
207     p.texturescale = (Double)jSpinnerTextureScale.getValue();
208     p.detail = jCheckBoxDetail.isSelected();
209     int ret = new MapWriter().writeMap(p);
210     if(ret == 0) {
211         JOptionPane.showMessageDialog(this, "Map successfully generated!", "Success!", JOptionPane.INFORMATION_MESSAGE);
212     } else {
213         JOptionPane.showMessageDialog(this, "Error occured! Oh no!", "Error!", JOptionPane.ERROR_MESSAGE);
214     }
215     
216     
217     this.setVisible(true);
218 }//GEN-LAST:event_jButtonOKActionPerformed
219
220 private void jButtonOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOutputActionPerformed
221     JFileChooser fc = new JFileChooser();
222     int ret = fc.showOpenDialog(this);
223     
224     if(ret == fc.APPROVE_OPTION) {
225         File f = fc.getSelectedFile();
226         jTextFieldOutput.setText(f.getAbsolutePath());
227     }
228 }//GEN-LAST:event_jButtonOutputActionPerformed
229
230     /**
231     * @param args the command line arguments
232     */
233     public static void main(String args[]) {
234         java.awt.EventQueue.invokeLater(new Runnable() {
235             public void run() {
236                 new JFrameMain().setVisible(true);
237             }
238         });
239     }
240
241     // Variables declaration - do not modify//GEN-BEGIN:variables
242     private javax.swing.JButton jButtonInput;
243     private javax.swing.JButton jButtonOK;
244     private javax.swing.JButton jButtonOutput;
245     private javax.swing.JCheckBox jCheckBoxDetail;
246     private javax.swing.JLabel jLabel1;
247     private javax.swing.JLabel jLabel2;
248     private javax.swing.JLabel jLabel3;
249     private javax.swing.JLabel jLabel4;
250     private javax.swing.JPanel jPanel1;
251     private javax.swing.JSpinner jSpinnerHeight;
252     private javax.swing.JSpinner jSpinnerTextureScale;
253     private javax.swing.JSpinner jSpinnerUnits;
254     private javax.swing.JTextField jTextFieldInput;
255     private javax.swing.JTextField jTextFieldOutput;
256     private javax.swing.JTextField jTextFieldTexture;
257     // End of variables declaration//GEN-END:variables
258
259 }