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  //CHECKSTYLE: stop HideUtilityClassConstructor
46  public class ExampleUtils {
47  
48      /** Empty constructor.
49       * <p>
50       * This constructor is not strictly necessary, but it prevents spurious
51       * javadoc warnings with JDK 18 and later.
52       * </p>
53       * @since 3.0
54       */
55      public ExampleUtils() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
56          // nothing to do
57      }
58  
59      /** Display frame. */
60      @SuppressWarnings("serial")
61      public static class ExampleFrame extends JFrame {
62  
63          /** Empty constructor.
64           * <p>
65           * This constructor is not strictly necessary, but it prevents spurious
66           * javadoc warnings with JDK 18 and later.
67           * </p>
68           * @since 3.0
69           */
70          public ExampleFrame() {
71              // nothing to do
72          }
73  
74          /**
75           * Returns the main panel which should be printed by the screenshot action.
76           * <p>
77           * By default, it returns the content pane of this frame, but can be overriden
78           * in case the frame has a global scroll pane which would cut off any offscreen content.
79           *
80           * @return the main panel to print
81           */
82          public Component getMainPanel() {
83              return getContentPane();
84          }
85      }
86  
87      /** Display example.
88       * @param frame frame to display
89       */
90      public static void showExampleFrame(final ExampleFrame frame) {
91          Runnable r = new Runnable() {
92              public void run() {
93                  JMenuItem screenshot = new JMenuItem("Screenshot (png)");
94                  screenshot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK));
95                  screenshot.addActionListener(new ActionListener() {
96                      public void actionPerformed(ActionEvent ae) {
97                          JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
98                          if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
99                            File file = fileChooser.getSelectedFile();
100                           BufferedImage img = getScreenShot(frame.getMainPanel());
101                           try {
102                               // write the image as a PNG
103                               ImageIO.write(img, "png", file);
104                           } catch (IOException e) {
105                               e.printStackTrace();
106                           }
107                         }
108                     }
109                 });
110 
111                 JMenuItem exit = new JMenuItem("Exit");
112                 exit.addActionListener(new ActionListener() {
113                     public void actionPerformed(ActionEvent e) {
114                         System.exit(0);
115                     }
116                 });
117 
118                 JMenu menu = new JMenu("File");
119                 menu.add(screenshot);
120                 menu.add(exit);
121                 JMenuBar mb = new JMenuBar();
122                 mb.add(menu);
123                 frame.setJMenuBar(mb);
124 
125                 frame.setLocationRelativeTo(null);
126                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
127                 frame.setVisible(true);
128             }
129         };
130         SwingUtilities.invokeLater(r);
131     }
132 
133     private static BufferedImage getScreenShot(Component component) {
134         BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
135         // call the Component's paint method, using the Graphics object of the image.
136         component.paint(image.getGraphics());
137         return image;
138     }
139 
140     /** Resize an image.
141      * @param originalImage original image
142      * @param width desired width
143      * @param height desired height
144      * @param type type of the create image
145      * @return resized image
146      */
147     public static BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) {
148         BufferedImage resizedImage = new BufferedImage(width, height, type);
149         Graphics2D g = resizedImage.createGraphics();
150         g.drawImage(originalImage, 0, 0, width, height, null);
151         g.dispose();
152         return resizedImage;
153     }
154 
155 }