View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.samples;
23  
24  import java.awt.Component;
25  import java.awt.Graphics2D;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.awt.event.InputEvent;
29  import java.awt.event.KeyEvent;
30  import java.awt.image.BufferedImage;
31  import java.io.File;
32  import java.io.IOException;
33  
34  import javax.imageio.ImageIO;
35  import javax.swing.JFileChooser;
36  import javax.swing.JFrame;
37  import javax.swing.JMenu;
38  import javax.swing.JMenuBar;
39  import javax.swing.JMenuItem;
40  import javax.swing.KeyStroke;
41  import javax.swing.SwingUtilities;
42  
43  /** Graphics utilities for examples.
44   */
45  public class ExampleUtils {
46  
47      /** Empty constructor.
48       * <p>
49       * This constructor is not strictly necessary, but it prevents spurious
50       * javadoc warnings with JDK 18 and later.
51       * </p>
52       * @since 3.0
53       */
54      public ExampleUtils() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
55          // nothing to do
56      }
57  
58      /** Display frame. */
59      @SuppressWarnings("serial")
60      public static class ExampleFrame extends JFrame {
61  
62          /** Empty constructor.
63           * <p>
64           * This constructor is not strictly necessary, but it prevents spurious
65           * javadoc warnings with JDK 18 and later.
66           * </p>
67           * @since 3.0
68           */
69          public ExampleFrame() {
70              // nothing to do
71          }
72  
73          /**
74           * Returns the main panel which should be printed by the screenshot action.
75           * <p>
76           * By default, it returns the content pane of this frame, but can be overriden
77           * in case the frame has a global scroll pane which would cut off any offscreen content.
78           *
79           * @return the main panel to print
80           */
81          public Component getMainPanel() {
82              return getContentPane();
83          }
84      }
85  
86      /** Display example.
87       * @param frame frame to display
88       */
89      public static void showExampleFrame(final ExampleFrame frame) {
90          Runnable r = new Runnable() {
91              public void run() {
92                  JMenuItem screenshot = new JMenuItem("Screenshot (png)");
93                  screenshot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK));
94                  screenshot.addActionListener(new ActionListener() {
95                      public void actionPerformed(ActionEvent ae) {
96                          JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
97                          if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
98                            File file = fileChooser.getSelectedFile();
99                            BufferedImage img = getScreenShot(frame.getMainPanel());
100                           try {
101                               // write the image as a PNG
102                               ImageIO.write(img, "png", file);
103                           } catch (IOException e) {
104                               e.printStackTrace();
105                           }
106                         }
107                     }
108                 });
109 
110                 JMenuItem exit = new JMenuItem("Exit");
111                 exit.addActionListener(new ActionListener() {
112                     public void actionPerformed(ActionEvent e) {
113                         System.exit(0);
114                     }
115                 });
116 
117                 JMenu menu = new JMenu("File");
118                 menu.add(screenshot);
119                 menu.add(exit);
120                 JMenuBar mb = new JMenuBar();
121                 mb.add(menu);
122                 frame.setJMenuBar(mb);
123 
124                 frame.setLocationRelativeTo(null);
125                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
126                 frame.setVisible(true);
127             }
128         };
129         SwingUtilities.invokeLater(r);
130     }
131 
132     private static BufferedImage getScreenShot(Component component) {
133         BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
134         // call the Component's paint method, using the Graphics object of the image.
135         component.paint(image.getGraphics());
136         return image;
137     }
138 
139     /** Resize an image.
140      * @param originalImage original image
141      * @param width desired width
142      * @param height desired height
143      * @param type type of the create image
144      * @return resized image
145      */
146     public static BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) {
147         BufferedImage resizedImage = new BufferedImage(width, height, type);
148         Graphics2D g = resizedImage.createGraphics();
149         g.drawImage(originalImage, 0, 0, width, height, null);
150         g.dispose();
151         return resizedImage;
152     }
153 
154 }