X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbackend%2FSwingTerminal.java;h=bab8f820df9ed168a8ff348235706ad6d317eb9b;hb=a69ed767c9c07cf35cf1c5f7821fc009cfe79cd2;hp=598f0ab1bce9f7d834cb770a283500c6aa95b2f2;hpb=d36057dfab8def933a64be042b039d76708ac5ba;p=fanfix.git diff --git a/src/jexer/backend/SwingTerminal.java b/src/jexer/backend/SwingTerminal.java index 598f0ab..bab8f82 100644 --- a/src/jexer/backend/SwingTerminal.java +++ b/src/jexer/backend/SwingTerminal.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -80,11 +80,11 @@ import static jexer.TKeypress.*; * and uses a SwingComponent wrapper class to call the JFrame or JComponent * methods. */ -public final class SwingTerminal extends LogicalScreen - implements TerminalReader, - ComponentListener, KeyListener, - MouseListener, MouseMotionListener, - MouseWheelListener, WindowListener { +public class SwingTerminal extends LogicalScreen + implements TerminalReader, + ComponentListener, KeyListener, + MouseListener, MouseMotionListener, + MouseWheelListener, WindowListener { // ------------------------------------------------------------------------ // Constants -------------------------------------------------------------- @@ -398,14 +398,16 @@ public final class SwingTerminal extends LogicalScreen SwingTerminal.this.textHeight, windowWidth, windowHeight); - SwingTerminal.this.setDimensions(sessionInfo.getWindowWidth(), - sessionInfo.getWindowHeight()); + SwingTerminal.this.setDimensions(sessionInfo. + getWindowWidth(), sessionInfo.getWindowHeight()); SwingTerminal.this.resizeToScreen(); SwingTerminal.this.swing.setVisible(true); } }); - } catch (Exception e) { + } catch (java.lang.reflect.InvocationTargetException e) { + e.printStackTrace(); + } catch (InterruptedException e) { e.printStackTrace(); } @@ -527,7 +529,9 @@ public final class SwingTerminal extends LogicalScreen SwingTerminal.this.textHeight); } }); - } catch (Exception e) { + } catch (java.lang.reflect.InvocationTargetException e) { + e.printStackTrace(); + } catch (InterruptedException e) { e.printStackTrace(); } @@ -643,6 +647,24 @@ public final class SwingTerminal extends LogicalScreen // SwingTerminal ---------------------------------------------------------- // ------------------------------------------------------------------------ + /** + * Get the width of a character cell in pixels. + * + * @return the width in pixels of a character cell + */ + public int getTextWidth() { + return textWidth; + } + + /** + * Get the height of a character cell in pixels. + * + * @return the height in pixels of a character cell + */ + public int getTextHeight() { + return textHeight; + } + /** * Setup Swing colors to match DOS color palette. */ @@ -727,7 +749,10 @@ public final class SwingTerminal extends LogicalScreen Font terminus = terminusRoot.deriveFont(Font.PLAIN, fontSize); gotTerminus = true; font = terminus; - } catch (Exception e) { + } catch (java.awt.FontFormatException e) { + e.printStackTrace(); + font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize); + } catch (java.io.IOException e) { e.printStackTrace(); font = new Font(Font.MONOSPACED, Font.PLAIN, fontSize); } @@ -742,6 +767,15 @@ public final class SwingTerminal extends LogicalScreen * @return the Swing Color */ private Color attrToForegroundColor(final CellAttributes attr) { + int rgb = attr.getForeColorRGB(); + if (rgb >= 0) { + int red = (rgb >> 16) & 0xFF; + int green = (rgb >> 8) & 0xFF; + int blue = rgb & 0xFF; + + return new Color(red, green, blue); + } + if (attr.isBold()) { if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) { return MYBOLD_BLACK; @@ -790,6 +824,15 @@ public final class SwingTerminal extends LogicalScreen * @return the Swing Color */ private Color attrToBackgroundColor(final CellAttributes attr) { + int rgb = attr.getBackColorRGB(); + if (rgb >= 0) { + int red = (rgb >> 16) & 0xFF; + int green = (rgb >> 8) & 0xFF; + int blue = rgb & 0xFF; + + return new Color(red, green, blue); + } + if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) { return MYBLACK; } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) { @@ -946,7 +989,41 @@ public final class SwingTerminal extends LogicalScreen * Resize to font dimensions. */ public void resizeToScreen() { - swing.setDimensions(textWidth * width, textHeight * height); + swing.setDimensions(textWidth * (width + 1), textHeight * (height + 1)); + } + + /** + * Draw one cell's image to the screen. + * + * @param gr the Swing Graphics context + * @param cell the Cell to draw + * @param xPixel the x-coordinate to render to. 0 means the + * left-most pixel column. + * @param yPixel the y-coordinate to render to. 0 means the top-most + * pixel row. + */ + private void drawImage(final Graphics gr, final Cell cell, + final int xPixel, final int yPixel) { + + /* + System.err.println("drawImage(): " + xPixel + " " + yPixel + + " " + cell); + */ + + // Draw the background rectangle, then the foreground character. + assert (cell.isImage()); + gr.setColor(cell.getBackground()); + gr.fillRect(xPixel, yPixel, textWidth, textHeight); + + BufferedImage image = cell.getImage(); + if (image != null) { + if (swing.getFrame() != null) { + gr.drawImage(image, xPixel, yPixel, swing.getFrame()); + } else { + gr.drawImage(image, xPixel, yPixel, swing.getComponent()); + } + return; + } } /** @@ -1106,7 +1183,8 @@ public final class SwingTerminal extends LogicalScreen */ } - if ((swing.getBufferStrategy() != null) + if ((swing.getFrame() != null) + && (swing.getBufferStrategy() != null) && (SwingUtilities.isEventDispatchThread()) ) { // System.err.println("paint(), skip first paint on swing thread"); @@ -1171,7 +1249,11 @@ public final class SwingTerminal extends LogicalScreen || reallyCleared || (swing.getFrame() == null)) { - drawGlyph(gr, lCell, xPixel, yPixel); + if (lCell.isImage()) { + drawImage(gr, lCell, xPixel, yPixel); + } else { + drawGlyph(gr, lCell, xPixel, yPixel); + } // Physical is always updated physical[x][y].setTo(lCell); @@ -1241,7 +1323,11 @@ public final class SwingTerminal extends LogicalScreen && cursorVisible) || (lCell.isBlink()) ) { - drawGlyph(gr, lCell, xPixel, yPixel); + if (lCell.isImage()) { + drawImage(gr, lCell, xPixel, yPixel); + } else { + drawGlyph(gr, lCell, xPixel, yPixel); + } physical[x][y].setTo(lCell); } } @@ -1368,6 +1454,15 @@ public final class SwingTerminal extends LogicalScreen return sessionInfo; } + /** + * Getter for the underlying Swing component. + * + * @return the SwingComponent + */ + public SwingComponent getSwingComponent() { + return swing; + } + // ------------------------------------------------------------------------ // KeyListener ------------------------------------------------------------ // ------------------------------------------------------------------------