Add support for creating screenshots.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1485435 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ab6b3c02c3
commit
37376db2eb
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 RandomVectorGeneratorExamples {
|
|||
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 RandomVectorGeneratorExamples {
|
|||
}
|
||||
}
|
||||
|
||||
@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 RandomVectorGeneratorExamples {
|
|||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
Display d = new Display();
|
||||
d.setVisible(true);
|
||||
}
|
||||
});
|
||||
ExampleUtils.showExampleFrame(new Display());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue