48e4a44fa6b5b9f2db1765ec8f700f78137d38b3
2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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]
29 package jexer
.backend
;
31 import java
.awt
.Color
;
32 import java
.awt
.Cursor
;
34 import java
.awt
.Graphics
;
35 import java
.awt
.Insets
;
36 import java
.awt
.Point
;
37 import java
.awt
.Toolkit
;
38 import java
.awt
.event
.ComponentListener
;
39 import java
.awt
.event
.KeyListener
;
40 import java
.awt
.event
.MouseListener
;
41 import java
.awt
.event
.MouseMotionListener
;
42 import java
.awt
.event
.MouseWheelListener
;
43 import java
.awt
.event
.WindowListener
;
44 import java
.awt
.image
.BufferedImage
;
45 import java
.awt
.image
.BufferStrategy
;
46 import javax
.swing
.JComponent
;
47 import javax
.swing
.JFrame
;
50 * Wrapper for integrating with Swing, because JFrame and JComponent have
51 * separate hierarchies.
53 class SwingComponent
{
56 * If true, use triple buffering when drawing to a JFrame.
58 public static boolean tripleBuffer
= true;
61 * Get the BufferStrategy object needed for triple-buffering.
63 * @return the BufferStrategy
64 * @throws IllegalArgumentException if this function is called when
65 * not rendering to a JFrame
67 public BufferStrategy
getBufferStrategy() {
69 return frame
.getBufferStrategy();
71 throw new IllegalArgumentException("BufferStrategy not used " +
72 "for JComponent access");
77 * The frame reference, if we are drawing to a JFrame.
82 * The component reference, if we are drawing to a JComponent.
84 private JComponent component
;
87 * Get the JFrame reference.
89 * @return the frame, or null if this is drawing to a JComponent
91 public JFrame
getFrame() {
96 * Get the JComponent reference.
98 * @return the component, or null if this is drawing to a JFrame
100 public JComponent
getComponent() {
105 * Construct using a JFrame.
107 * @param frame the JFrame to draw to
109 public SwingComponent(final JFrame frame
) {
115 * Construct using a JComponent.
117 * @param component the JComponent to draw to
119 public SwingComponent(final JComponent component
) {
120 this.component
= component
;
125 * Setup to render to an existing JComponent.
127 public void setupComponent() {
128 component
.setBackground(Color
.black
);
130 // Kill the X11 cursor
131 // Transparent 16 x 16 pixel cursor image.
132 BufferedImage cursorImg
= new BufferedImage(16, 16,
133 BufferedImage
.TYPE_INT_ARGB
);
134 // Create a new blank cursor.
135 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
136 cursorImg
, new Point(0, 0), "blank cursor");
137 component
.setCursor(blankCursor
);
139 // Be capable of seeing Tab / Shift-Tab
140 component
.setFocusTraversalKeysEnabled(false);
144 * Setup to render to an existing JFrame.
146 public void setupFrame() {
147 frame
.setTitle("Jexer Application");
148 frame
.setBackground(Color
.black
);
151 // Kill the X11 cursor
152 // Transparent 16 x 16 pixel cursor image.
153 BufferedImage cursorImg
= new BufferedImage(16, 16,
154 BufferedImage
.TYPE_INT_ARGB
);
155 // Create a new blank cursor.
156 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
157 cursorImg
, new Point(0, 0), "blank cursor");
158 frame
.setCursor(blankCursor
);
160 // Be capable of seeing Tab / Shift-Tab
161 frame
.setFocusTraversalKeysEnabled(false);
163 // Setup triple-buffering
165 frame
.setIgnoreRepaint(true);
166 frame
.createBufferStrategy(3);
171 * Set the window title.
173 * @param title the new title
175 public void setTitle(final String title
) {
177 frame
.setTitle(title
);
182 * Paints this component.
184 * @param g the graphics context to use for painting
186 public void paint(Graphics g
) {
195 * Repaints this component.
197 public void repaint() {
206 * Repaints the specified rectangle of this component.
208 * @param x the x coordinate
209 * @param y the y coordinate
210 * @param width the width
211 * @param height the height
213 public void repaint(int x
, int y
, int width
, int height
) {
215 frame
.repaint(x
, y
, width
, height
);
217 component
.repaint(x
, y
, width
, height
);
222 * If a border has been set on this component, returns the border's
223 * insets; otherwise calls super.getInsets.
225 * @return the value of the insets property
227 public Insets
getInsets() {
229 return frame
.getInsets();
231 return component
.getInsets();
236 * Returns the current width of this component.
238 * @return the current width of this component
240 public int getWidth() {
242 return frame
.getWidth();
244 return component
.getWidth();
249 * Returns the current height of this component.
251 * @return the current height of this component
253 public int getHeight() {
255 return frame
.getHeight();
257 return component
.getHeight();
262 * Gets the font of this component.
264 * @return this component's font; if a font has not been set for this
265 * component, the font of its parent is returned
267 public Font
getFont() {
269 return frame
.getFont();
271 return component
.getFont();
276 * Sets the font of this component.
278 * @param f the font to become this component's font; if this parameter
279 * is null then this component will inherit the font of its parent
281 public void setFont(final Font f
) {
285 component
.setFont(f
);
290 * Shows or hides this Window depending on the value of parameter b.
292 * @param b if true, make visible, else make invisible
294 public void setVisible(final boolean b
) {
298 component
.setVisible(b
);
303 * Creates a graphics context for this component. This method will return
304 * null if this component is currently not displayable.
306 * @return a graphics context for this component, or null if it has none
308 public Graphics
getGraphics() {
310 return frame
.getGraphics();
312 return component
.getGraphics();
317 * Releases all of the native screen resources used by this Window, its
318 * subcomponents, and all of its owned children. That is, the resources
319 * for these Components will be destroyed, any memory they consume will
320 * be returned to the OS, and they will be marked as undisplayable.
322 public void dispose() {
326 component
.getParent().remove(component
);
331 * Resize the component to match the font dimensions.
333 * @param width the new width in pixels
334 * @param height the new height in pixels
336 public void setDimensions(final int width
, final int height
) {
337 // Figure out the thickness of borders and use that to set the final
339 Insets insets
= getInsets();
342 frame
.setSize(width
+ insets
.left
+ insets
.right
,
343 height
+ insets
.top
+ insets
.bottom
);
345 component
.setSize(width
+ insets
.left
+ insets
.right
,
346 height
+ insets
.top
+ insets
.bottom
);
351 * Adds the specified component listener to receive component events from
352 * this component. If listener l is null, no exception is thrown and no
353 * action is performed.
355 * @param l the component listener
357 public void addComponentListener(ComponentListener l
) {
359 frame
.addComponentListener(l
);
361 component
.addComponentListener(l
);
366 * Adds the specified key listener to receive key events from this
367 * component. If l is null, no exception is thrown and no action is
370 * @param l the key listener.
372 public void addKeyListener(KeyListener l
) {
374 frame
.addKeyListener(l
);
376 component
.addKeyListener(l
);
381 * Adds the specified mouse listener to receive mouse events from this
382 * component. If listener l is null, no exception is thrown and no action
385 * @param l the mouse listener
387 public void addMouseListener(MouseListener l
) {
389 frame
.addMouseListener(l
);
391 component
.addMouseListener(l
);
396 * Adds the specified mouse motion listener to receive mouse motion
397 * events from this component. If listener l is null, no exception is
398 * thrown and no action is performed.
400 * @param l the mouse motion listener
402 public void addMouseMotionListener(MouseMotionListener l
) {
404 frame
.addMouseMotionListener(l
);
406 component
.addMouseMotionListener(l
);
411 * Adds the specified mouse wheel listener to receive mouse wheel events
412 * from this component. Containers also receive mouse wheel events from
415 * @param l the mouse wheel listener
417 public void addMouseWheelListener(MouseWheelListener l
) {
419 frame
.addMouseWheelListener(l
);
421 component
.addMouseWheelListener(l
);
426 * Adds the specified window listener to receive window events from this
427 * window. If l is null, no exception is thrown and no action is
430 * @param l the window listener
432 public void addWindowListener(WindowListener l
) {
434 frame
.addWindowListener(l
);