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