  
import java.applet.Applet;
import java.awt.FileDialog.*; 
import java.io.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLException;

public class blender extends JFrame implements ActionListener, ItemListener{
StringBuffer sb;
JTextArea JTA;
JFrame F;
JPanel P; 
JPanel P2;
InputStream is;
URL url = null;//;//= new URL("c:/windows/desktop/myfile.txt");
 public File f = null;
            
 public  File  file = null;
 public File newfile = null ;
   JButton  JB = null;
   JButton JB2 = null;
   JButton JB3 = null;
   JTextField TF ;
   JLabel label;
   int num = 0;
  
  
   public blender() { 
 
   Choice choice = new Choice();
      choice.add("choose model");
      choice.add("outerbars");
      choice.add("light");
      choice.add("dome");
       choice.add("car");
       choice.add("spitfire");
       choice.add("boat");
       choice.addItemListener(this);
   
      P = new JPanel();
      P2 = new JPanel();
      label = new JLabel("number of components");
      JScrollPane sp = new JScrollPane();
      JViewport port = sp.getViewport();
               P2.setLayout(new BorderLayout());
               TF = new JTextField(2);
               TF.setText("5");
               P2.add("Center", TF);
               P2.add("West", label);
                P2.add("East", choice);
              F = new JFrame("blender convertor");
                JB  = new JButton("  Open ");
                JB2  = new JButton("  Save ");
                JB3  = new JButton("  SHOW MODEL ");
                
            JB.addActionListener(this);
            JB2.addActionListener(this);
             JB3.addActionListener(this);
           F.getContentPane().setLayout(new BorderLayout());
            JTA = new JTextArea(24,24);
            port.add(JTA);
            P.add("West", JB);
            P.add("East", JB2); 
               P.add("Center", JB3); 
              F.getContentPane().add("North" , P);
               F.getContentPane().add("South" , P2);
              // F.getContentPane().add("South" , JB2);
              
             F.getContentPane().add("Center", sp);
            F.setSize(320, 320); 
           F.show();
 }
    String mynewString;
    String mynewString2;
          int noc = 128000;         
          char buff[] = new char[noc];
          char buff2[] = new char[noc];
        
   
         //newfile() = null;

        Object matO  = "Material" ;
        int len = 0;
         String NEW = null;
        char mydata[] = new char[5];
          int z = 0;
          int a = 0;
          int b = 0;
          
          int Z = 0;
          int A = 0;
          int B = 0;
          
          int ZZ = 0;
          int AA = 0;
          int BB = 0;
          
        String begin = (" Transform { \n " +
                       "   children Shape { \n " +
                        "    appearance Appearance{ \n " +
                        "      material Material \n") ;
       String next =( " } \n " +
                     "}  \n " +
                     " geometry IndexedFaceSet { solid FALSE \n " +
                     " coord Coordinate { \n" +
                     "  point  \n ") ;
        
        String next1 = (" ] \n" +
                      " } \n " +
                      "coordIndex  \n") ;
        String end =( " ] \n  " +
                     " } \n" +
                     " } \n " +
                     " } \n ") ;                                           
          int z1 = 0;
          int a1 = 0;
          int p1 = 0;
          StringBuffer mnsb = null;
          
            public void itemStateChanged(ItemEvent e) {
            File NF = null;
            File OF = null;
             if (e.getSource() instanceof Choice) {
           String choice3 = (String) e.getItem();
           
            if (choice3.equals("car")) {
          
            NF = new File("c:/windows/desktop/anims/mech/cars/car2V2.wrl"); 
            OF = new File("c:/windows/desktop/anims/mech/cars/car2.wrl");   
            }
          else   if (choice3.equals("spitfire")) {
            
            NF = new File("c:/windows/desktop/anims/mech/planes/spitfire/spitfireV2.wrl"); 
            OF = new File("c:/windows/desktop/anims/mech/planes/spitfire/spitfire.wrl"); 
             }
        
          else  if (choice3.equals("boat")) {
           
            NF = new File("c:/windows/desktop/anims/mech/boats/boatV2.wrl"); 
            OF = new File("c:/windows/desktop/anims/mech/boats/boat.wrl"); 
           }  
           
           else  if (choice3.equals("dome")) {
           
            NF = new File("c:/windows/desktop/anims/worlds/structures/domeV2.wrl"); 
            OF = new File("c:/windows/desktop/anims/worlds/structures/dome.wrl"); 
           }  
           
            else  if (choice3.equals("light")) {
           
            NF = new File("c:/windows/desktop/anims/worlds/lightV2.wrl"); 
            OF = new File("c:/windows/desktop/anims/worlds/light.wrl"); 
           }  
           else  if (choice3.equals("outerbars")) {
           
            NF = new File("c:/windows/desktop/mirror/outerbarsV2.wrl"); 
            OF = new File("c:/windows/desktop/mirror/outerbars.wrl"); 
           }  
           
              
      }
     newfile = NF;
     f = OF;   
  }  
        
        
          public void actionPerformed(ActionEvent evt)
         {   
           sb = new StringBuffer();
            Object src = evt.getSource();
             StringBuffer myStringBuff  =  null;
           //f = f;
               if(src == JB) {
                
                
                //File f =  new File();//"c:/windows/desktop/anims/mech/planes/spitfire/spitfireV1.wrl");
                //this.f = f;
                try {
                num = Integer.parseInt(TF.getText());
                }
                catch (NumberFormatException nfe )
               {}
                String cush = null;
                 String cushString = null;
                int data;
                int p = 0;
                int P = 0;
                int PP = 0;
                String mynewstring[] = null;
               // File f = new File("c:/windows/desktop/anims/mech/planes/spitfire/spitfireV1.wrl");
           
               if(f.exists())
   try {    
                FileInputStream inFile = new FileInputStream(f);
                myStringBuff = new StringBuffer();
                BufferedReader  br = new BufferedReader(new InputStreamReader(inFile)); 
               
                while((data = br.read()) != -1)
                myStringBuff.append((char) data);// this has data in it but not so well organised.
                 cushString = new String(myStringBuff);//this is just same.
                   len = cushString.length();

                    for (int x=0 ; x<=3;x++) {
                       mydata[x] = cushString.charAt(x);
                       }           
                  mnsb = new StringBuffer();
                 
                  int q = cushString.indexOf("Material" );
                   int num2 = num - 1;
                   for (int g =0; g<=num2; g++) { // 2 for car and boat
                     z = cushString.indexOf("Material",a +1);
                    a = cushString.indexOf("}", z);
                    p = cushString.indexOf("{" , z);
                    
                    char mat[] = new char[a - p];
                    cushString.getChars(p, a  , mat, 0);// this puts it in mat[]
                    
                   
                     Z = cushString.indexOf("point", A);
                    A = cushString.indexOf("]", Z);
                    P = cushString.indexOf("[" , Z);
                   
                    char point[] = new char[A - P];
                    cushString.getChars(P, A  , point, 0);
                  
                    
                     ZZ = cushString.indexOf("coordIndex", AA);
                    AA = cushString.indexOf("]", ZZ);
                    PP = cushString.indexOf("[" , ZZ);
                   
                    char ci[] = new char[AA - PP];
                    cushString.getChars(PP, AA  , ci, 0);
                   
                    mnsb.append(
                    begin + mat + next + point + next1 + ci + end);
                     a = cushString.indexOf("}", a);
                      A = cushString.indexOf("]", A);
                      AA = cushString.indexOf("]",AA);
                   }
    }  
             catch(IOException ioe) {
            System.out.println("Error: " + ioe.toString());
           }
          
          JTA.setText("#VRML V2.0 utf8 \n" + mnsb.toString() );
        } 
        
         if(src == JB2) {
          
                            // FileDialog fd = new FileDialog(this);
           
                          //  fd.setMode(FileDialog.SAVE);
                          // fd.show( );  
                          // newfile = new File(fd.getDirectory() + fd.getFile());  
            // newfile = new File("c:/windows/desktop/anims/mech/planes/spitfire/spitfireV2.wrl");
        try {
             Writer out = new FileWriter(newfile);
             out.write(JTA.getText());
             out.flush();
             
        }
        catch (IOException ieo){}
        
         }
        
        }
           
            
         public static void main(String args[]) {
            
             blender bl = new blender();
              
        
    } 
}                                            
  
  
  
