*
* 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"),
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
*/
private JComponent component;
+ /**
+ * An optional border in pixels to add.
+ */
+ private static final int BORDER = 1;
+
+ /**
+ * Adjustable Insets for this component. This has the effect of adding a
+ * black border around the drawing area.
+ */
+ Insets adjustInsets = new Insets(BORDER + 5, BORDER, BORDER, BORDER);
+
// ------------------------------------------------------------------------
// Constructors -----------------------------------------------------------
// ------------------------------------------------------------------------
* @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;
}
/**
* @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);
+ }
+ }
+ });
}
/**