X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbackend%2FSwingComponent.java;h=23b50ab3b7b9f5e157cdb81ff867aa98ee44e070;hb=97bc3f29f12d64d27d49b0d61947d15dfa64d156;hp=84e147263476050530bfed776a948d3a9fcb8e76;hpb=42873e30bf487bc0b695d60652dba44f82185dbb;p=fanfix.git diff --git a/src/jexer/backend/SwingComponent.java b/src/jexer/backend/SwingComponent.java index 84e1472..23b50ab 100644 --- a/src/jexer/backend/SwingComponent.java +++ b/src/jexer/backend/SwingComponent.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"), @@ -45,6 +45,7 @@ import java.awt.image.BufferedImage; import java.awt.image.BufferStrategy; import javax.swing.JComponent; import javax.swing.JFrame; +import javax.swing.SwingUtilities; /** * Wrapper for integrating with Swing, because JFrame and JComponent have @@ -52,26 +53,14 @@ import javax.swing.JFrame; */ class SwingComponent { - /** - * If true, use triple buffering when drawing to a JFrame. - */ - public static boolean tripleBuffer = false; + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** - * Get the BufferStrategy object needed for triple-buffering. - * - * @return the BufferStrategy - * @throws IllegalArgumentException if this function is called when - * not rendering to a JFrame + * If true, use triple buffering when drawing to a JFrame. */ - public BufferStrategy getBufferStrategy() { - if (frame != null) { - return frame.getBufferStrategy(); - } else { - throw new IllegalArgumentException("BufferStrategy not used " + - "for JComponent access"); - } - } + public static boolean tripleBuffer = true; /** * The frame reference, if we are drawing to a JFrame. @@ -84,22 +73,19 @@ class SwingComponent { private JComponent component; /** - * Get the JFrame reference. - * - * @return the frame, or null if this is drawing to a JComponent + * An optional border in pixels to add. */ - public JFrame getFrame() { - return frame; - } + private static final int BORDER = 1; /** - * Get the JComponent reference. - * - * @return the component, or null if this is drawing to a JFrame + * Adjustable Insets for this component. This has the effect of adding a + * black border around the drawing area. */ - public JComponent getComponent() { - return component; - } + Insets adjustInsets = new Insets(BORDER + 5, BORDER, BORDER, BORDER); + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Construct using a JFrame. @@ -121,6 +107,44 @@ class SwingComponent { setupComponent(); } + // ------------------------------------------------------------------------ + // SwingComponent --------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Get the BufferStrategy object needed for triple-buffering. + * + * @return the BufferStrategy + * @throws IllegalArgumentException if this function is called when + * not rendering to a JFrame + */ + public BufferStrategy getBufferStrategy() { + if (frame != null) { + return frame.getBufferStrategy(); + } else { + throw new IllegalArgumentException("BufferStrategy not used " + + "for JComponent access"); + } + } + + /** + * Get the JFrame reference. + * + * @return the frame, or null if this is drawing to a JComponent + */ + public JFrame getFrame() { + return frame; + } + + /** + * Get the JComponent reference. + * + * @return the component, or null if this is drawing to a JFrame + */ + public JComponent getComponent() { + return component; + } + /** * Setup to render to an existing JComponent. */ @@ -225,11 +249,17 @@ class SwingComponent { * @return the value of the insets property */ public Insets getInsets() { + Insets swingInsets = null; if (frame != null) { - return frame.getInsets(); + swingInsets = frame.getInsets(); } else { - return component.getInsets(); + swingInsets = component.getInsets(); } + Insets result = new Insets(swingInsets.top + adjustInsets.top, + swingInsets.left + adjustInsets.left, + swingInsets.bottom + adjustInsets.bottom, + swingInsets.right + adjustInsets.right); + return result; } /** @@ -334,17 +364,38 @@ class SwingComponent { * @param height the new height in pixels */ public void setDimensions(final int width, final int height) { - // Figure out the thickness of borders and use that to set the final - // size. - Insets insets = getInsets(); - - if (frame != null) { - frame.setSize(width + insets.left + insets.right, - height + insets.top + insets.bottom); - } else { - component.setSize(width + insets.left + insets.right, - height + insets.top + insets.bottom); + if (SwingUtilities.isEventDispatchThread()) { + // We are in the Swing thread and can safely set the size. + + // Figure out the thickness of borders and use that to set the + // final size. + if (frame != null) { + Insets insets = getInsets(); + frame.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } else { + Insets insets = getInsets(); + component.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } + return; } + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + // Figure out the thickness of borders and use that to set + // the final size. + if (frame != null) { + Insets insets = getInsets(); + frame.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } else { + Insets insets = getInsets(); + component.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } + } + }); } /**