From cb37a129d1adb403019c96e798e86e2da9667f15 Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Sun, 17 Nov 2019 17:56:04 -0500
Subject: [PATCH] Maze

---
 Grafreed.java |  461 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 441 insertions(+), 20 deletions(-)

diff --git a/Grafreed.java b/Grafreed.java
index 95f44cd..320521a 100644
--- a/Grafreed.java
+++ b/Grafreed.java
@@ -5,6 +5,7 @@
 
 import java.io.*;
 
+import java.util.ArrayList;
 import javax.swing.UIManager;
 import javax.swing.plaf.metal.MetalLookAndFeel;
 import javax.swing.plaf.ColorUIResource;
@@ -12,17 +13,41 @@
 
 import javax.sound.sampled.*;
 
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+
+import java.security.cert.X509Certificate;
+import java.net.Authenticator;
+import java.net.PasswordAuthentication;
+
+import java.util.Iterator;
+import java.util.Locale;
+
+import javax.imageio.IIOImage;
+import javax.imageio.ImageIO;
+import javax.imageio.ImageWriteParam;
+import javax.imageio.ImageWriter;
+import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
+import javax.imageio.stream.ImageOutputStream;
+import javax.imageio.ImageWriteParam;
+
+import java.awt.image.*;
+
 //import com.jamonapi.*;
 public class Grafreed extends Applet implements ActionListener
-{
-        static boolean NIMBUSLAF = true;
-        
+{        
     static int RENDERME = 0;
 
     static boolean epsequal = false;
     static float epsvertex2 = 0.001f;
     static boolean linkUV = false; // not uniform at load time
 
+    static boolean smoothmode = false;
+    
     static Wav wav = new Wav("/Users/nbriere/0ut/wavs/monoformat"); // output");
     static boolean hassound = false;
     static boolean savesound = false;
@@ -41,7 +66,176 @@
     static void Assert(boolean b)
     {
         if (!b)
+        {
+            b = !!b;
             new Exception().printStackTrace(); // assert(b);
+            
+        }
+    }
+
+    static void Beep()
+    {
+        java.awt.Toolkit.getDefaultToolkit().beep();
+    }
+    
+    static String SaveImage(String filename, BufferedImage bufImage, float compressionQuality)
+    {
+        String ext = "jpg"; // filename.substring(filename.length()-3, filename.length())
+        
+        if (compressionQuality == 1)
+        {
+            ext = "png";
+        }
+        
+        BufferedImage rendImage; // = new BufferedImage(bufImage.getWidth(), bufImage.getHeight(), BufferedImage.TYPE_INT_RGB);
+
+        try
+        {
+            int[] storage = ((sun.awt.image.IntegerInterleavedRaster)bufImage.getRaster()).getDataStorage();
+            
+            rendImage = new BufferedImage(bufImage.getWidth(), bufImage.getHeight(), BufferedImage.TYPE_INT_RGB);
+            int[] storage2 = ((sun.awt.image.IntegerInterleavedRaster)rendImage.getRaster()).getDataStorage();
+
+            for (int i=storage.length; --i>=0;)
+            {
+                storage2[i] = storage[i];
+            }
+        }
+        catch (Exception e)
+        {
+            byte[] storage = ((sun.awt.image.ByteInterleavedRaster)bufImage.getRaster()).getDataStorage();
+            
+            rendImage = new BufferedImage(bufImage.getWidth(), bufImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
+            byte[] storage2 = ((sun.awt.image.ByteInterleavedRaster)rendImage.getRaster()).getDataStorage();
+
+            for (int i=storage.length/2; --i>=0;)
+            {
+                storage2[i] = storage[2*i];
+            }
+        }
+        
+//        rendImage.setRGB(0,0,bufImage.getWidth(),bufImage.getHeight(),
+//                bufImage.getRGB(0, 0, bufImage.getWidth(), bufImage.getHeight(), null, 0, bufImage.getWidth()),
+//                0,bufImage.getWidth());
+//                //bufImage.getWidth()*(bufImage.getHeight()-1),-bufImage.getWidth());
+            
+        ImageWriter writer = null;
+        Iterator iter = ImageIO.getImageWritersByFormatName(ext);
+        
+        if (iter.hasNext()) {
+            writer = (ImageWriter)iter.next();
+        }
+
+//        int i = frames.size();
+//        
+//        filename = filename + (i%100000)/10000 + "" + (i%10000)/1000 + "" + (i%1000)/100 + "" + (i%100)/10 + "" + (i%10) + ".jpg";
+        
+        {
+            String fn = filename;
+            if (!fn.endsWith(".jpg"))
+            {
+                fn += ".jpg";
+            }
+            
+            java.io.File outfile = new java.io.File(fn);
+            
+            if (outfile.getParentFile() != null) // nov 2013
+                outfile.getParentFile().mkdirs();
+            
+            filename = outfile.getAbsolutePath();
+
+            //float compressionQuality = 0.85f;
+
+            try
+            {
+                ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
+                writer.setOutput(ios);
+                JPEGImageWriteParam iwparam=new JPEGImageWriteParam(Locale.getDefault());
+                iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
+                iwparam.setCompressionQuality(compressionQuality);
+                writer.write(null, new IIOImage(rendImage, null, null), iwparam);
+                ios.flush();
+                writer.dispose();
+                ios.close();
+            }
+            catch(Exception e){};
+        }
+        
+        return ".jpg";
+//        return;// filename;
+    }
+    
+    public static String RemovePrefix(String s, String prefix)
+    {
+        if (s != null && prefix != null && s.startsWith(prefix))
+        {
+            return s.substring(prefix.length());
+        }
+
+        return s;
+    }
+    
+    static void ParseFileSystem(java.io.File dir, iResourceCallBack callback, String prefix)
+    {
+        callback.ResourceCallBack(RemovePrefix(dir.toString(),prefix).split("/"));
+        File[] listFiles = dir.listFiles();
+        if (listFiles != null)
+        {
+            for (java.io.File file : listFiles)
+            {
+                ParseFileSystem(file, callback, prefix);
+            }
+        }
+    }
+        
+    static interface iResourceCallBack
+    {
+        void ResourceCallBack(String[] path);
+    }
+    
+    static void ParseResources(String path, iResourceCallBack callback)
+    {
+        java.io.File jarFile = new java.io.File(Grafreed.class.getProtectionDomain().getCodeSource().getLocation().getPath());
+
+        if (jarFile.isFile())
+        {
+            // Run with JAR file
+            try
+            {
+                java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
+                java.util.Enumeration<java.util.jar.JarEntry> entries = jar.entries(); //gives ALL entries in jar
+                while (entries.hasMoreElements())
+                {
+                    String name = entries.nextElement().getName();
+                    
+                    if (name.startsWith(path + "/"))
+                        callback.ResourceCallBack(RemovePrefix(name, path + "/").split("/"));
+                }
+                jar.close();
+            } catch (Exception ex)
+            {
+                ex.printStackTrace();
+            }
+        } else
+        {
+            // Run with IDE
+            final java.net.URL url = Object3D.class.getResource("/" + path);
+            if (url != null)
+            {
+                try
+                {
+                    java.io.File apps = new java.io.File(url.toURI());
+                    ParseFileSystem(apps, callback, RemovePrefix(url.toString() + "/", "file:"));
+//                    for (java.io.File app : apps.listFiles())
+//                    {
+//                        //System.out.println(RemovePrefix(app.toString(), RemovePrefix(url.toString(), "file:") + "/"));
+//                    }
+                } catch (Exception ex)
+                {
+                    ex.printStackTrace();
+                }
+            }
+        }
     }
     
     public void init()
@@ -52,7 +246,7 @@
         universe.material = new cMaterial();
         //god.addChild(universe);
         universe.name = "Applet";
-        grafreeD = this;
+        grafreed = this;
     }
 
     /**/
@@ -595,11 +789,18 @@
         PlayWord(word, 1);
     }
     
+    static boolean isWindows;
+    
     public static void main(String argv[])
     {
-                String osArch = System.getProperty("os.arch");
+        String osArch = System.getProperty("os.arch");
+        if (Globals.DEBUG)
                 System.out.println("os.arch = " + osArch);
                 
+        String osName = System.getProperty("os.name");
+
+        isWindows = !osName.equals("Mac OS X");
+            
         if (argv.length == 0)
         {
             String javaPath = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
@@ -802,22 +1003,31 @@
 //        try{Thread.sleep(5000);}catch(Exception e){}
 //        PlayAudio("/Users/nbriere/Downloads/Footsteps-6.wav", 4, 1);
         
-        
 /**/
-        if (NIMBUSLAF)
+        if (Globals.NIMBUSLAF)
         {
                 try
                 {
-                    Object o = UIManager.getInstalledLookAndFeels();
+                    //Object o = UIManager.getInstalledLookAndFeels();
                         
                     javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.DefaultMetalTheme());
                     //MetalLookAndFeel.setCurrentTheme(new Theme(Constants.beigeTheme));
-                    UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
+                    //UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
                     
                         UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
             //UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
             //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                         //UIManager.put("nimbusBase", new Color(0,0,0));
+                        
+                    javax.swing.UIDefaults ui = UIManager.getDefaults();
+                    
+                    ui.put("TabbedPane.tabInsets", new javax.swing.plaf.InsetsUIResource(0,8,0,0));
+                    
+                    for (java.util.Enumeration e = ui.keys(); e.hasMoreElements();)
+                    {
+                        Object key = e.nextElement();
+                        System.out.println(key + " --> " + ui.get(key));
+                    }
                 }
                 catch (Exception e)
                 {
@@ -827,35 +1037,241 @@
         {
                 try
                 {
-                    MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
-                    MetalLookAndFeel.setCurrentTheme(new Theme(Constants.yellowTheme));
+                    //Object o = UIManager.getInstalledLookAndFeels();
+                    
+                    MetalLookAndFeel.setCurrentTheme(new javax.swing.plaf.metal.DefaultMetalTheme());
+                    //MetalLookAndFeel.setCurrentTheme(new Theme(Constants.yellowTheme));
+                    //UIManager.put("ScrollBar.background", new javax.swing.plaf.ColorUIResource(100,0,0));
+                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
+                    
+                    javax.swing.UIDefaults ui = UIManager.getDefaults();
+                    
+                    Object x = ui.get("TabbedPane.background");
+                    
                     UIManager.setLookAndFeel(new MetalLookAndFeel());
-                    //UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                     //UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
+                    //UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                     //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
+                    
+                    ui = UIManager.getDefaults();
+                    
+                    x = ui.get("RadioButton.icon");
+                    
+                    ArrayList gradient = new java.util.ArrayList(5);
+                    gradient.add(1.0);
+                    gradient.add(0.0);
+                    gradient.add(new javax.swing.plaf.ColorUIResource(255,255,255));
+                    gradient.add(new javax.swing.plaf.ColorUIResource(192,192,192));
+                    gradient.add(new javax.swing.plaf.ColorUIResource(0,0,0));
+                    
+                    ui.put("Button.gradient", gradient);
+                    
+                    //ui.put("RadioButton.icon", new CheckBox()); //ObjEditor.GetIcon("icons/white-sphere-icon.png")));
+                    ui.put("CheckBox.icon", new CheckBox()); //ObjEditor.GetIcon("icons/white-sphere-icon.png")));
+                    //ui.put("CheckBoxMenuItem.checkIcon", ObjEditor.GetIcon("icons/white-sphere-icon.png"));
+                    
+                    ui.put("Slider.foreground", new javax.swing.plaf.ColorUIResource(0,0,0));
+                    ui.put("Slider.horizontalThumbIcon", ObjEditor.GetIcon("icons/white-sphere-icon.png"));
+                    
+                    /*
+TabbedPane.unselectedBackground	Color
+TabbedPane.unselectedTabBackground	Color
+TabbedPane.unselectedTabForeground	Color
+TabbedPane.unselectedTabHighlight	Color
+TabbedPane.unselectedTabShadow	
+                     */
+//                    ui.put("TabbedPane.contentAreaColor", new javax.swing.plaf.ColorUIResource(0,100,0));
+                    //ui.put("TabbedPane.selected", new javax.swing.plaf.ColorUIResource(200,0,200));
+                    
+                    ui.put("TabbedPane.background", new javax.swing.plaf.ColorUIResource(150,150,150));
+                    ui.put("TabbedPane.foreground", new javax.swing.plaf.ColorUIResource(50,50,50));
+                    ui.put("TabbedPane.light", new javax.swing.plaf.ColorUIResource(255,255,255));
+                    ui.put("TabbedPane.selectedForeground", new javax.swing.plaf.ColorUIResource(0,0,0));
+                    ui.put("TabbedPane.selectHighlight", new javax.swing.plaf.ColorUIResource(255,255,255));
+                    ui.put("TabbedPane.darkShadow", new javax.swing.plaf.ColorUIResource(0,0,0));
+                    
+//                    ui.put("TabbedPane.shadow", new javax.swing.plaf.ColorUIResource(200,0,0));
+//                    ui.put("TabbedPane.tabAreaBackground", new javax.swing.plaf.ColorUIResource(0,200,0));
+//                    ui.put("TabbedPane.unselectedBackground", new javax.swing.plaf.ColorUIResource(200,200,0));
+//                    ui.put("TabbedPane.unselectedTabBackground", new javax.swing.plaf.ColorUIResource(0,0,200));
+//                    ui.put("TabbedPane.unselectedTabForeground", new javax.swing.plaf.ColorUIResource(200,0,200));
+//                    ui.put("TabbedPane.unselectedTabHighlight", new javax.swing.plaf.ColorUIResource(0,200,200));
+//                    ui.put("TabbedPane.unselectedTabShadow", new javax.swing.plaf.ColorUIResource(200,200,200));
+                    
+                    ui.put("TabbedPane.textIconGap", 0);
+                    ui.put("TabbedPane.contentBorderInsets", new javax.swing.plaf.InsetsUIResource(0,0,0,0));
+                    ui.put("TabbedPane.tabAreaInsets", new javax.swing.plaf.InsetsUIResource(1,1,0,0));
+                    ui.put("TabbedPane.tabInsets", new javax.swing.plaf.InsetsUIResource(0,8,0,0));
+                    
+                    Object openIcon2 = ui.get("Tree.openIcon");
+                    
+                    ui.put("Tree.openIcon", ObjEditor.GetIcon("icons/folderopen.png"));
+                    ui.put("Tree.closedIcon", ObjEditor.GetIcon("icons/folderclose.png"));
+                    ui.put("Tree.leafIcon", ObjEditor.GetIcon("icons/file.png"));
+
+                    //javax.swing.plaf.metal.MetalIconFactory.getHorizontalSliderThumbIcon().
+                    Object o = ui.get("Slider.horizontalThumbIcon");
+                    
+                    gradient = new java.util.ArrayList(5);
+                    gradient.add(1.0);
+                    gradient.add(0.0);
+                    gradient.add(new javax.swing.plaf.ColorUIResource(192,192,192));
+                    gradient.add(new javax.swing.plaf.ColorUIResource(255,255,255));
+                    gradient.add(new javax.swing.plaf.ColorUIResource(0,0,0));
+                    
+                    ui.put("ToggleButton.gradient", gradient);
+                    //ui.put("Button[MouseOver].backgroundPainter", new com.sun.java.swing.plaf.nimbus.ButtonPainter());
+                    //ui.put("Button.highlight", new javax.swing.plaf.ColorUIResource(155,155,155));
+                    
+                    for (java.util.Enumeration e = ui.keys(); e.hasMoreElements();)
+                    {
+                        Object key = e.nextElement();
+                        //System.out.println(key + " --> " + ui.get(key));
+                    }
                 } catch (Exception e)
                 {
                 }
         }
 /**/
+        
+        // Create a trust manager that does not validate certificate chains
+        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
+            @Override
+            public void checkClientTrusted(final X509Certificate[] chain, final String authType) {
+            }
+
+            @Override
+            public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
+            }
+
+            @Override
+            public X509Certificate[] getAcceptedIssuers() {
+                return null;
+            }
+        } };
+
+        try
+        {
+            // Install the all-trusting trust manager
+            final SSLContext sslContext = SSLContext.getInstance("SSL");
+            sslContext.init(null, trustAllCerts, null);
+            // Create an ssl socket factory with our all-trusting manager
+            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
+            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
+                public boolean verify(String urlHostName, SSLSession session) {
+                    return true;
+                }
+            });
+            // be authentic
+            Authenticator.setDefault(new Authenticator() {
+                @Override
+                protected PasswordAuthentication getPasswordAuthentication() {
+                    return new PasswordAuthentication("args[0]", "args[1]".toCharArray());
+                }
+            });
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        
+        
+        /////////////
 
 //        javax.swing.ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
         
         //Monitor mon=MonitorFactory.start("myFirstMonitor");
         standAlone = true;
-        grafreeD = new Grafreed();
-        grafreeD.universe = new Composite();
-        grafreeD.universe.name = "Grafreed";
-        grafreeD.universe.material = new cMaterial();
+        
+        grafreed = new Grafreed();   
+        
+        grafreed.materials = ReadGFD(grafreed.getClass().getClassLoader().getResourceAsStream("gfd/materials.gfd"));
+        
+        grafreed.universe = new cGroup();
+        grafreed.universe.name = "Grafreed";
+        grafreed.universe.material = new cMaterial();
+        
         //   theApplet3D.universe.textures = CameraPane.DEFAULT_TEXTURE;
 
-        grafreeD.universe.root = true;
-        grafreeD.universe.openEditWindow(null, true); //, true);
+        grafreed.universe.root = true;
+        grafreed.universe.openEditWindow(null, true); //, true);
+        grafreed.universe.editWindow.New();
+        
         //mon.stop();
         //System.out.println(mon);
         //timeflow.app.TimeflowAppLauncher.GetTimeFlow();
+        
+        javax.swing.ToolTipManager.sharedInstance().setEnabled(Globals.TOOLTIPS);
     }
 
+    static class CheckBox extends javax.swing.plaf.metal.MetalCheckBoxIcon
+    {
+        java.awt.image.BufferedImage image;
+        
+        CheckBox()
+        {
+            try
+            {
+                image = javax.imageio.ImageIO.read(ObjEditor.class.getClassLoader().getResourceAsStream("icons/checkbox.png"));
+            }
+            catch (Exception e)
+            {
+            }
+        }
+        
+        protected void drawCheck(Component c, Graphics g, int x, int y)
+        {
+            super.drawCheck(c, g, x, y);
+        }
+        
+        public void paintIcon(Component c, Graphics g, int x, int y)
+        {
+            g.drawImage(image, x-1, y-1, 19, 19, null);
+            super.paintIcon(c, g, x+2, y+2);
+        }
+    }
+    
+    static Object3D materials;
+    
+    static Object3D ReadGFD(java.io.InputStream istream)
+    {
+        Object3D readobj = null;
+        
+        try
+        {
+            // Try compressed version first.
+            java.util.zip.GZIPInputStream zstream = new java.util.zip.GZIPInputStream(istream);
+            java.io.ObjectInputStream p = new java.io.ObjectInputStream(zstream);
+
+            readobj = (Object3D) p.readObject();
+            p.close();
+            zstream.close();
+            istream.close();
+
+            readobj.ResetDisplayList();
+        } catch (Exception e)
+        {
+            if (!e.toString().contains("GZIP"))
+                e.printStackTrace();
+            
+            try
+            {
+                java.io.ObjectInputStream p = new java.io.ObjectInputStream(istream);
+
+                readobj = (Object3D) p.readObject();
+                p.close();
+                istream.close();
+
+                readobj.ResetDisplayList();
+            } catch (Exception e2)
+            {
+                e2.printStackTrace();
+            }
+        }
+        
+        return readobj;
+    }
+    
     // Timer callback
     public void actionPerformed(ActionEvent e)
     {
@@ -870,7 +1286,7 @@
     static int depth = 0;
     static java.util.Stack stack = new java.util.Stack();
     static boolean traceoff = false; // true;
-    static float[] colorV = new float[5];
+    static float[] colorV = new float[4]; // 5];
 
     static void traceon()
     {
@@ -977,6 +1393,11 @@
 
     static public Object clone(Object o)
     {
+        if (o instanceof Object3D)
+        {
+            assert(((Object3D)o).parent == null);
+        }
+        
         if (o == null)
             return null;
         
@@ -1079,7 +1500,7 @@
         } while (avail > 0 && numRead >= 0);
         return new String(data, 0, pos, "US-ASCII");
     }
-    public static Grafreed grafreeD;
+    public static Grafreed grafreed;
     public static boolean standAlone = true;
     public Composite universe;
     public static Object3D clipboard = new Object3D();

--
Gitblit v1.6.2