2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.awt
.image
.BufferedImage
;
33 import jexer
.backend
.ECMA48Terminal
;
34 import jexer
.backend
.MultiScreen
;
35 import jexer
.backend
.SwingTerminal
;
36 import jexer
.bits
.Cell
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import static jexer
.TKeypress
.*;
42 * TImage renders a piece of a bitmap image on screen.
44 public class TImage
extends TWidget
{
46 // ------------------------------------------------------------------------
47 // Variables --------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * The action to perform when the user clicks on the image.
53 private TAction clickAction
;
56 * The image to display.
58 private BufferedImage image
;
61 * The original image from construction time.
63 private BufferedImage originalImage
;
66 * The current scaling factor for the image.
68 private double scaleFactor
= 1.0;
71 * The current clockwise rotation for the image.
73 private int clockwise
= 0;
76 * Left column of the image. 0 is the left-most column.
81 * Top row of the image. 0 is the top-most row.
86 * The cells containing the broken up image pieces.
88 private Cell cells
[][];
91 * The number of rows in cells[].
96 * The number of columns in cells[].
98 private int cellColumns
;
101 * Last text width value.
103 private int lastTextWidth
= -1;
106 * Last text height value.
108 private int lastTextHeight
= -1;
110 // ------------------------------------------------------------------------
111 // Constructors -----------------------------------------------------------
112 // ------------------------------------------------------------------------
115 * Public constructor.
117 * @param parent parent widget
118 * @param x column relative to parent
119 * @param y row relative to parent
120 * @param width number of text cells for width of the image
121 * @param height number of text cells for height of the image
122 * @param image the image to display
123 * @param left left column of the image. 0 is the left-most column.
124 * @param top top row of the image. 0 is the top-most row.
125 * @param clickAction function to call when mouse is pressed
127 public TImage(final TWidget parent
, final int x
, final int y
,
128 final int width
, final int height
,
129 final BufferedImage image
, final int left
, final int top
,
130 final TAction clickAction
) {
132 // Set parent and window
133 super(parent
, x
, y
, width
, height
);
135 setCursorVisible(false);
136 this.originalImage
= image
;
139 this.clickAction
= clickAction
;
143 getApplication().addImage(this);
146 // ------------------------------------------------------------------------
147 // Event handlers ---------------------------------------------------------
148 // ------------------------------------------------------------------------
151 * Subclasses should override this method to cleanup resources. This is
152 * called by TWindow.onClose().
155 protected void close() {
156 getApplication().removeImage(this);
161 * Handle mouse press events.
163 * @param mouse mouse button press event
166 public void onMouseDown(final TMouseEvent mouse
) {
167 if (clickAction
!= null) {
176 * @param keypress keystroke event
179 public void onKeypress(final TKeypressEvent keypress
) {
180 if (!keypress
.getKey().isFnKey()) {
181 if (keypress
.getKey().getChar() == '+') {
182 // Make the image bigger.
188 if (keypress
.getKey().getChar() == '-') {
189 // Make the image smaller.
196 if (keypress
.equals(kbAltUp
)) {
197 // Make the image bigger.
203 if (keypress
.equals(kbAltDown
)) {
204 // Make the image smaller.
210 if (keypress
.equals(kbAltRight
)) {
218 if (keypress
.equals(kbAltLeft
)) {
219 // Rotate counter-clockwise.
229 // Pass to parent for the things we don't care about.
230 super.onKeypress(keypress
);
233 // ------------------------------------------------------------------------
234 // TWidget ----------------------------------------------------------------
235 // ------------------------------------------------------------------------
244 // We have already broken the image up, just draw the last set of
246 for (int x
= 0; (x
< getWidth()) && (x
+ left
< cellColumns
); x
++) {
247 if ((left
+ x
) * lastTextWidth
> image
.getWidth()) {
251 for (int y
= 0; (y
< getHeight()) && (y
+ top
< cellRows
); y
++) {
252 if ((top
+ y
) * lastTextHeight
> image
.getHeight()) {
255 assert (x
+ left
< cellColumns
);
256 assert (y
+ top
< cellRows
);
258 getWindow().putCharXY(x
, y
, cells
[x
+ left
][y
+ top
]);
264 // ------------------------------------------------------------------------
265 // TImage -----------------------------------------------------------------
266 // ------------------------------------------------------------------------
269 * Size cells[][] according to the screen font size.
271 * @param always if true, always resize the cells
273 private void sizeToImage(final boolean always
) {
277 if (getScreen() instanceof SwingTerminal
) {
278 SwingTerminal terminal
= (SwingTerminal
) getScreen();
280 textWidth
= terminal
.getTextWidth();
281 textHeight
= terminal
.getTextHeight();
282 } if (getScreen() instanceof MultiScreen
) {
283 MultiScreen terminal
= (MultiScreen
) getScreen();
285 textWidth
= terminal
.getTextWidth();
286 textHeight
= terminal
.getTextHeight();
287 } else if (getScreen() instanceof ECMA48Terminal
) {
288 ECMA48Terminal terminal
= (ECMA48Terminal
) getScreen();
290 textWidth
= terminal
.getTextWidth();
291 textHeight
= terminal
.getTextHeight();
295 image
= scaleImage(originalImage
, scaleFactor
);
296 image
= rotateImage(image
, clockwise
);
299 if ((always
== true) ||
301 && (textWidth
!= lastTextWidth
)
303 && (textHeight
!= lastTextHeight
))
305 cellColumns
= image
.getWidth() / textWidth
;
306 if (cellColumns
* textWidth
< image
.getWidth()) {
309 cellRows
= image
.getHeight() / textHeight
;
310 if (cellRows
* textHeight
< image
.getHeight()) {
314 // Break the image up into an array of cells.
315 cells
= new Cell
[cellColumns
][cellRows
];
317 for (int x
= 0; x
< cellColumns
; x
++) {
318 for (int y
= 0; y
< cellRows
; y
++) {
320 int width
= textWidth
;
321 if ((x
+ 1) * textWidth
> image
.getWidth()) {
322 width
= image
.getWidth() - (x
* textWidth
);
324 int height
= textHeight
;
325 if ((y
+ 1) * textHeight
> image
.getHeight()) {
326 height
= image
.getHeight() - (y
* textHeight
);
329 Cell cell
= new Cell();
330 cell
.setImage(image
.getSubimage(x
* textWidth
,
331 y
* textHeight
, width
, height
));
337 lastTextWidth
= textWidth
;
338 lastTextHeight
= textHeight
;
341 if ((left
+ getWidth()) > cellColumns
) {
342 left
= cellColumns
- getWidth();
347 if ((top
+ getHeight()) > cellRows
) {
348 top
= cellRows
- getHeight();
356 * Get the top corner to render.
358 * @return the top row
360 public int getTop() {
365 * Set the top corner to render.
367 * @param top the new top row
369 public void setTop(final int top
) {
371 if (this.top
> cellRows
- getHeight()) {
372 this.top
= cellRows
- getHeight();
380 * Get the left corner to render.
382 * @return the left column
384 public int getLeft() {
389 * Set the left corner to render.
391 * @param left the new left column
393 public void setLeft(final int left
) {
395 if (this.left
> cellColumns
- getWidth()) {
396 this.left
= cellColumns
- getWidth();
404 * Get the number of text cell rows for this image.
406 * @return the number of rows
408 public int getRows() {
413 * Get the number of text cell columns for this image.
415 * @return the number of columns
417 public int getColumns() {
422 * Scale an image by to be scaleFactor size.
424 * @param image the image to scale
425 * @param factor the scale to make the new image
427 private BufferedImage
scaleImage(final BufferedImage image
,
428 final double factor
) {
430 if (Math
.abs(factor
- 1.0) < 0.03) {
431 // If we are within 3% of 1.0, just return the original image.
435 int width
= (int) (image
.getWidth() * factor
);
436 int height
= (int) (image
.getHeight() * factor
);
438 BufferedImage newImage
= new BufferedImage(width
, height
,
439 BufferedImage
.TYPE_INT_ARGB
);
441 java
.awt
.Graphics gr
= newImage
.createGraphics();
442 gr
.drawImage(image
, 0, 0, width
, height
, null);
449 * Rotate an image either clockwise or counterclockwise.
451 * @param image the image to scale
452 * @param clockwise number of turns clockwise
454 private BufferedImage
rotateImage(final BufferedImage image
,
455 final int clockwise
) {
457 if (clockwise
% 4 == 0) {
461 BufferedImage newImage
= null;
463 if (clockwise
% 4 == 1) {
464 // 90 degrees clockwise
465 newImage
= new BufferedImage(image
.getHeight(), image
.getWidth(),
466 BufferedImage
.TYPE_INT_ARGB
);
467 for (int x
= 0; x
< image
.getWidth(); x
++) {
468 for (int y
= 0; y
< image
.getHeight(); y
++) {
469 newImage
.setRGB(y
, x
,
470 image
.getRGB(x
, image
.getHeight() - 1 - y
));
473 } else if (clockwise
% 4 == 2) {
474 // 180 degrees clockwise
475 newImage
= new BufferedImage(image
.getWidth(), image
.getHeight(),
476 BufferedImage
.TYPE_INT_ARGB
);
477 for (int x
= 0; x
< image
.getWidth(); x
++) {
478 for (int y
= 0; y
< image
.getHeight(); y
++) {
479 newImage
.setRGB(x
, y
,
480 image
.getRGB(image
.getWidth() - 1 - x
,
481 image
.getHeight() - 1 - y
));
484 } else if (clockwise
% 4 == 3) {
485 // 270 degrees clockwise
486 newImage
= new BufferedImage(image
.getHeight(), image
.getWidth(),
487 BufferedImage
.TYPE_INT_ARGB
);
488 for (int x
= 0; x
< image
.getWidth(); x
++) {
489 for (int y
= 0; y
< image
.getHeight(); y
++) {
490 newImage
.setRGB(y
, x
,
491 image
.getRGB(image
.getWidth() - 1 - x
, y
));