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 = () -> {
92 JMenuItem screenshot = new JMenuItem("Screenshot (png)");
93 screenshot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK));
94 screenshot.addActionListener(ae -> {
95 JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
96 if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
97 File file = fileChooser.getSelectedFile();
98 BufferedImage img = getScreenShot(frame.getMainPanel());
99 try {
100 // write the image as a PNG
101 ImageIO.write(img, "png", file);
102 } catch (IOException e) {
103 e.printStackTrace();
104 }
105 }
106 });
107
108 JMenuItem exit = new JMenuItem("Exit");
109 exit.addActionListener(e -> System.exit(0));
110
111 JMenu menu = new JMenu("File");
112 menu.add(screenshot);
113 menu.add(exit);
114 JMenuBar mb = new JMenuBar();
115 mb.add(menu);
116 frame.setJMenuBar(mb);
117
118 frame.setLocationRelativeTo(null);
119 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
120 frame.setVisible(true);
121 };
122 SwingUtilities.invokeLater(r);
123 }
124
125 private static BufferedImage getScreenShot(Component component) {
126 BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
127 // call the Component's paint method, using the Graphics object of the image.
128 component.paint(image.getGraphics());
129 return image;
130 }
131
132 /** Resize an image.
133 * @param originalImage original image
134 * @param width desired width
135 * @param height desired height
136 * @param type type of the create image
137 * @return resized image
138 */
139 public static BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) {
140 BufferedImage resizedImage = new BufferedImage(width, height, type);
141 Graphics2D g = resizedImage.createGraphics();
142 g.drawImage(originalImage, 0, 0, width, height, null);
143 g.dispose();
144 return resizedImage;
145 }
146
147 }