You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/05/22 23:32:02 UTC

svn commit: r1485435 - in /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide: ClusteringExamples.java ExampleUtils.java RandomVectorGeneratorExamples.java

Author: tn
Date: Wed May 22 21:32:02 2013
New Revision: 1485435

URL: http://svn.apache.org/r1485435
Log:
Add support for creating screenshots.

Added:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java   (with props)
Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ClusteringExamples.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/RandomVectorGeneratorExamples.java

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ClusteringExamples.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ClusteringExamples.java?rev=1485435&r1=1485434&r2=1485435&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ClusteringExamples.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ClusteringExamples.java Wed May 22 21:32:02 2013
@@ -33,8 +33,7 @@ import java.util.List;
 
 import javax.swing.JComponent;
 import javax.swing.JFrame;
-import javax.swing.JTextArea;
-import javax.swing.SwingUtilities;
+import javax.swing.JLabel;
 
 import org.apache.commons.math3.distribution.NormalDistribution;
 import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
@@ -180,15 +179,12 @@ public class ClusteringExamples {
         return points;
     }
     
+    @SuppressWarnings("serial")
     public static class Display extends JFrame {
         
-        private static final long serialVersionUID = -8846964550416589808L;
-
         public Display() {
             setTitle("Clustering examples");
             setSize(800, 800);
-            setLocationRelativeTo(null);
-            setDefaultCloseOperation(EXIT_ON_CLOSE);
             
             setLayout(new GridBagLayout());
             
@@ -217,9 +213,7 @@ public class ClusteringExamples {
             c.insets = new Insets(2, 2, 2, 2);
 
             for (Pair<String, Clusterer<DoublePoint>> pair : algorithms) {
-                JTextArea text = new JTextArea(pair.getFirst());
-                text.setEditable(false);
-                text.setOpaque(false);
+                JLabel text = new JLabel("<html><body>" + pair.getFirst().replace("\n", "<br>"));
                 add(text, c);
                 c.gridx++;
             }
@@ -239,10 +233,9 @@ public class ClusteringExamples {
         }
     }
 
+    @SuppressWarnings("serial")
     public static class ClusterPlot extends JComponent {
 
-        private static final long serialVersionUID = 4546352048750419587L;
-
         private static double PAD = 10;
 
         private List<? extends Cluster<DoublePoint>> clusters;
@@ -274,7 +267,7 @@ public class ClusteringExamples {
                 for (DoublePoint point : cluster.getPoints()) {
                     Clusterable p = transform(point, w, h);
                     double[] arr = p.getPoint();
-                    g2.fill(new Ellipse2D.Double(arr[0] - 2, arr[1] - 2, 4, 4));
+                    g2.fill(new Ellipse2D.Double(arr[0] - 1, arr[1] - 1, 3, 3));
                 }
                 
                 if (cluster instanceof CentroidCluster) {
@@ -304,11 +297,6 @@ public class ClusteringExamples {
     }
 
     public static void main(String[] args) {
-        SwingUtilities.invokeLater(new Runnable() {
-            public void run() {
-                Display d = new Display();
-                d.setVisible(true);
-            }
-        });
+        ExampleUtils.showExampleFrame(new Display());
     }
 }

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java?rev=1485435&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java Wed May 22 21:32:02 2013
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.userguide;
+
+import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+import java.awt.image.BufferedImage;
+import java.io.File;
+
+import javax.imageio.ImageIO;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.KeyStroke;
+import javax.swing.SwingUtilities;
+
+public class ExampleUtils {
+
+    public static void showExampleFrame(final JFrame frame) {
+        Runnable r = new Runnable() {
+            public void run() {
+                JMenuItem screenshot = new JMenuItem("Screenshot (png)");
+                screenshot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK));
+                screenshot.addActionListener(new ActionListener() {
+                    public void actionPerformed(ActionEvent ae) {
+                        JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
+                        if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
+                          File file = fileChooser.getSelectedFile();
+                          BufferedImage img = getScreenShot(frame.getContentPane());
+                          try {
+                              // write the image as a PNG
+                              ImageIO.write(img, "png", file);
+                          } catch (Exception e) {
+                              e.printStackTrace();
+                          }
+                        }
+                    }
+                });
+                JMenu menu = new JMenu("File");
+                menu.add(screenshot);
+                JMenuBar mb = new JMenuBar();
+                mb.add(menu);
+                frame.setJMenuBar(mb);
+
+                frame.setLocationRelativeTo(null);
+                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+                frame.setVisible(true);
+            }
+        };
+        SwingUtilities.invokeLater(r);
+    }
+
+    private static BufferedImage getScreenShot(Component component) {
+        BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
+        // call the Component's paint method, using the Graphics object of the image.
+        component.paint(image.getGraphics());
+        return image;
+    }
+
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision HeadURL

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/ExampleUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/RandomVectorGeneratorExamples.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/RandomVectorGeneratorExamples.java?rev=1485435&r1=1485434&r2=1485435&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/RandomVectorGeneratorExamples.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/userguide/RandomVectorGeneratorExamples.java Wed May 22 21:32:02 2013
@@ -32,7 +32,6 @@ import javax.swing.JComponent;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JTextArea;
-import javax.swing.SwingUtilities;
 
 import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
 import org.apache.commons.math3.random.HaltonSequenceGenerator;
@@ -118,15 +117,12 @@ public class RandomVectorGeneratorExampl
         return points;
     }
     
+    @SuppressWarnings("serial")
     public static class Display extends JFrame {
         
-        private static final long serialVersionUID = -8846964550416589808L;
-
         public Display() {
             setTitle("Pseudo/Quasi-random examples");
             setSize(800, 800);
-            setLocationRelativeTo(null);
-            setDefaultCloseOperation(EXIT_ON_CLOSE);
             
             setLayout(new GridBagLayout());
             
@@ -206,10 +202,9 @@ public class RandomVectorGeneratorExampl
         }
     }
 
+    @SuppressWarnings("serial")
     public static class Plot extends JComponent {
 
-        private static final long serialVersionUID = 4546352048750419587L;
-
         private static double PAD = 10;
 
         private List<Vector2D> points;
@@ -252,11 +247,6 @@ public class RandomVectorGeneratorExampl
     }
 
     public static void main(String[] args) {
-        SwingUtilities.invokeLater(new Runnable() {
-            public void run() {
-                Display d = new Display();
-                d.setVisible(true);
-            }
-        });
+        ExampleUtils.showExampleFrame(new Display());
     }
 }