| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 Kevin Lamonte |
| 7 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 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. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer.demos; |
| 30 | |
| 31 | import java.util.ResourceBundle; |
| 32 | |
| 33 | import jexer.TApplication; |
| 34 | import jexer.backend.*; |
| 35 | import jexer.demos.DemoApplication; |
| 36 | |
| 37 | /** |
| 38 | * This class shows off the use of MultiBackend and MultiScreen. |
| 39 | */ |
| 40 | public class Demo6 { |
| 41 | |
| 42 | /** |
| 43 | * Translated strings. |
| 44 | */ |
| 45 | private static final ResourceBundle i18n = ResourceBundle.getBundle(Demo6.class.getName()); |
| 46 | |
| 47 | // ------------------------------------------------------------------------ |
| 48 | // Demo6 ------------------------------------------------------------------ |
| 49 | // ------------------------------------------------------------------------ |
| 50 | |
| 51 | /** |
| 52 | * Main entry point. |
| 53 | * |
| 54 | * @param args Command line arguments |
| 55 | */ |
| 56 | public static void main(final String [] args) { |
| 57 | try { |
| 58 | |
| 59 | /* |
| 60 | * In this demo we will create two applications spanning three |
| 61 | * screens. One of the applications will have both an ECMA48 |
| 62 | * screen and a Swing screen, with all I/O mirrored between them. |
| 63 | * The second application will have a Swing screen containing a |
| 64 | * window showing the first application, also mirroring I/O |
| 65 | * between the window and the other two screens. |
| 66 | */ |
| 67 | |
| 68 | /* |
| 69 | * We create the first screen and use it to establish a |
| 70 | * MultiBackend. |
| 71 | */ |
| 72 | ECMA48Backend ecmaBackend = new ECMA48Backend(); |
| 73 | MultiBackend multiBackend = new MultiBackend(ecmaBackend); |
| 74 | |
| 75 | /* |
| 76 | * Now we create the first application (a standard demo). |
| 77 | */ |
| 78 | DemoApplication demoApp = new DemoApplication(multiBackend); |
| 79 | |
| 80 | /* |
| 81 | * We will need the width and height of the ECMA48 screen, so get |
| 82 | * the Screen reference now. |
| 83 | */ |
| 84 | Screen multiScreen = multiBackend.getScreen(); |
| 85 | |
| 86 | /* |
| 87 | * Now we create the second screen (backend) for the first |
| 88 | * application. It will be the same size as the ECMA48 screen, |
| 89 | * with a font size of 16 points. |
| 90 | */ |
| 91 | SwingBackend swingBackend = new SwingBackend(multiScreen.getWidth(), |
| 92 | multiScreen.getHeight(), 16); |
| 93 | |
| 94 | /* |
| 95 | * Add this screen to the MultiBackend, and at this point we have |
| 96 | * one demo application spanning two physical screens. |
| 97 | */ |
| 98 | multiBackend.addBackend(swingBackend); |
| 99 | multiBackend.setListener(demoApp); |
| 100 | |
| 101 | /* |
| 102 | * Time for the second application. This one will have a single |
| 103 | * window mirroring the contents of the first application. Let's |
| 104 | * make it a little larger than the first application's |
| 105 | * width/height. |
| 106 | */ |
| 107 | int width = multiScreen.getWidth(); |
| 108 | int height = multiScreen.getHeight(); |
| 109 | |
| 110 | /* |
| 111 | * Make a new Swing window for the second application. |
| 112 | */ |
| 113 | SwingBackend monitorBackend = new SwingBackend(width + 5, |
| 114 | height + 5, 20); |
| 115 | |
| 116 | /* |
| 117 | * Setup the second application, give it the basic file and |
| 118 | * window menus. |
| 119 | */ |
| 120 | TApplication monitor = new TApplication(monitorBackend); |
| 121 | monitor.addToolMenu(); |
| 122 | monitor.addFileMenu(); |
| 123 | monitor.addWindowMenu(); |
| 124 | |
| 125 | /* |
| 126 | * Now add the third screen to the first application. We want to |
| 127 | * change the object it locks on in its draw() method to the |
| 128 | * MultiScreen, that will dramatically reduce (not totally |
| 129 | * eliminate) screen tearing/artifacts. |
| 130 | */ |
| 131 | TWindowBackend windowBackend = new TWindowBackend(demoApp, |
| 132 | monitor, i18n.getString("monitorWindow"), |
| 133 | width + 2, height + 2); |
| 134 | windowBackend.setDrawLock(multiScreen); |
| 135 | windowBackend.setOtherApplication(demoApp); |
| 136 | multiBackend.addBackend(windowBackend); |
| 137 | |
| 138 | /* |
| 139 | * Three screens, two applications: spin them up! |
| 140 | */ |
| 141 | (new Thread(demoApp)).start(); |
| 142 | (new Thread(monitor)).start(); |
| 143 | } catch (Exception e) { |
| 144 | e.printStackTrace(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } |