|
1 |
| -package com.paulsen.demo; |
2 |
| - |
3 |
| -import com.paulsen.ui.*; |
4 |
| -import com.paulsen.ui.PUIElement.ElementAlignment; |
5 |
| - |
6 |
| -import java.awt.*; |
7 |
| - |
8 |
| -public class Main { |
9 |
| - |
10 |
| - public Main() { |
11 |
| - |
12 |
| - // initialize variables before using them in update/paint |
13 |
| - PUIFrame f = new PUIFrame("Project-Library Demo", 600, 600); |
14 |
| - |
15 |
| - |
16 |
| - // Drawing a rectangle in the background |
17 |
| - PUICanvas canvas = new PUICanvas(f, new PUIPaintable() { |
18 |
| - @Override |
19 |
| - public void paint(Graphics g, int x, int y, int w, int h) { |
20 |
| - g.setColor(new Color(100, 100, 100)); |
21 |
| - g.fillRoundRect(40, 40, w - 80, h - 80, 20, 20); |
22 |
| - } |
23 |
| - }, -1); |
24 |
| - |
25 |
| - |
26 |
| - PUIText darkmodeButton = new PUIText(f, "LIGHT", 2); |
27 |
| - darkmodeButton.addActionListener(new PUIAction() { |
28 |
| - @Override |
29 |
| - public void run(PUIElement that) { |
30 |
| - PUIElement.darkUIMode = !PUIElement.darkUIMode; |
31 |
| - if (PUIElement.darkUIMode) { |
32 |
| - darkmodeButton.setText("DARK"); |
33 |
| - } else { |
34 |
| - darkmodeButton.setText("LIGHT"); |
35 |
| - } |
36 |
| - } |
37 |
| - }); |
38 |
| - // if set to false: when pressed the eventchain doesnt stop => elements on layers behind this Button can be triggered as well |
39 |
| - darkmodeButton.setRaycastable(false); |
40 |
| - |
41 |
| - PUIScrollPanel sp = new PUIScrollPanel(f); |
42 |
| - |
43 |
| - PUICheckBox cb = new PUICheckBox(f); |
44 |
| - cb.addActionListener(new PUIAction() { |
45 |
| - @Override |
46 |
| - public void run(PUIElement that) { |
47 |
| - if (sp.getAlignment() == ElementAlignment.VERTICAL) |
48 |
| - sp.setAlignment(ElementAlignment.HORIZONTAL); |
49 |
| - else |
50 |
| - sp.setAlignment(ElementAlignment.VERTICAL); |
51 |
| - } |
52 |
| - }); |
53 |
| - |
54 |
| - PUIRotaryControl rc = new PUIRotaryControl(f, 1); |
55 |
| - |
56 |
| - PUISlider slider = new PUISlider(f); |
57 |
| - slider.setValue(0.5f); |
58 |
| - slider.setAlignment(ElementAlignment.HORIZONTAL); |
59 |
| - slider.addValueUpdateAction(new Runnable() { |
60 |
| - @Override |
61 |
| - public void run() { |
62 |
| - rc.setValueLength(slider.getValue()); |
63 |
| - } |
64 |
| - }); |
65 |
| - |
66 |
| - PUISlider slider2 = new PUISlider(f); |
67 |
| - slider2.setAlignment(ElementAlignment.HORIZONTAL); |
68 |
| - slider2.addValueUpdateAction(new Runnable() { |
69 |
| - @Override |
70 |
| - public void run() { |
71 |
| - rc.setValueThickness((int) (67 * slider2.getValue())); |
72 |
| - } |
73 |
| - }); |
74 |
| - |
75 |
| - // add test-Buttons for scrollpanel |
76 |
| - for (int i = 1; i <= 10; i++) { |
77 |
| - PUIText t = new PUIText(f, "" + i); |
78 |
| - t.addActionListener(new PUIAction() { |
79 |
| - @Override |
80 |
| - public void run(PUIElement that) { |
81 |
| - f.setTitle(((PUIText) that).getText()); |
82 |
| - |
83 |
| - // automatically centers clicked Element in the UI |
84 |
| - sp.center(that); |
85 |
| - } |
86 |
| - }); |
87 |
| - sp.addElement(t); |
88 |
| - } |
89 |
| - // comment out if the size of the element inside of the panel should be further limited |
90 |
| -// sp.setElementSpacing(6,0,3,3); |
91 |
| - |
92 |
| - // prevent different colors when hovering/pressing |
93 |
| - for (PUIElement e : PUIElement.registeredElements) { |
94 |
| - e.doPaintOverOnHover(false); |
95 |
| - e.doPaintOverOnPress(false); |
96 |
| - } |
97 |
| - |
98 |
| - f.setUpdateElements(new PUIUpdatable() { // initialize updateMethod |
99 |
| - @Override |
100 |
| - public void update(int w, int h) { |
101 |
| - |
102 |
| - // Element-Positions can also be defined relative by using width & height variables |
103 |
| - |
104 |
| - cb.setBounds(w - 150, 50, 100, 100);// relative |
105 |
| - rc.setBounds(w - 150, 200, 100, 100);// relative |
106 |
| - sp.setBounds(50, h - 200, 300, 150); // relative |
107 |
| - } |
108 |
| - }); |
109 |
| - |
110 |
| - // Set Position of other non-relative Elements |
111 |
| - darkmodeButton.setBounds(50, 50, 300, 100); |
112 |
| - slider.setBounds(50, 200, 300, 50); |
113 |
| - slider2.setBounds(50, 250, 300, 50); |
114 |
| - |
115 |
| - f.updateElements(); |
116 |
| - } |
117 |
| - |
118 |
| - public static void main(String[] args) { |
119 |
| - new Main(); |
120 |
| - } |
121 |
| - |
122 |
| -} |
| 1 | +package com.paulsen.demo; |
| 2 | + |
| 3 | +import com.paulsen.ui.*; |
| 4 | +import com.paulsen.ui.PUIElement.ElementAlignment; |
| 5 | + |
| 6 | +import java.awt.*; |
| 7 | + |
| 8 | +public class Demo { |
| 9 | + |
| 10 | + // PUI-Objects |
| 11 | + PUIFrame f; |
| 12 | + PUICanvas canvas; |
| 13 | + PUIMatrix matrix; |
| 14 | + PUIText darkmodeButton; |
| 15 | + PUIScrollPanel sp; |
| 16 | + PUICheckBox cb; |
| 17 | + PUIRotaryControl rc; |
| 18 | + PUISlider slider; |
| 19 | + PUISlider slider2; |
| 20 | + |
| 21 | + public Demo() { |
| 22 | + |
| 23 | + // initialize frame before using creating Elements |
| 24 | + f = new PUIFrame("Project-Library Demo", 600, 600); |
| 25 | + |
| 26 | + |
| 27 | + // Drawing a rectangle in the background |
| 28 | + canvas = new PUICanvas(f, new PUIPaintable() { |
| 29 | + @Override |
| 30 | + public void paint(Graphics2D g, int x, int y, int w, int h) { |
| 31 | + g.setColor(new Color(100, 100, 100)); |
| 32 | + g.fillRoundRect(40, 40, w - 80, h - 80, 20, 20); |
| 33 | + } |
| 34 | + }, -1); |
| 35 | + |
| 36 | + // Scaling Matrix |
| 37 | + matrix = new PUIMatrix(f, 4, 8); |
| 38 | + // generate Elements fot the Matrix to scale them |
| 39 | + setMatrixElements(false); |
| 40 | + |
| 41 | + |
| 42 | + darkmodeButton = new PUIText(f, "LIGHT", 2); |
| 43 | + darkmodeButton.addActionListener(new PUIAction() { |
| 44 | + @Override |
| 45 | + public void run(PUIElement that) { |
| 46 | + PUIElement.darkUIMode = !PUIElement.darkUIMode; |
| 47 | + if (PUIElement.darkUIMode) { |
| 48 | + darkmodeButton.setText("DARK"); |
| 49 | + } else { |
| 50 | + darkmodeButton.setText("LIGHT"); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + // if set to false: when pressed the eventchain doesnt stop => elements on layers behind this Button can be triggered as well |
| 55 | + darkmodeButton.doBlockRaycast(false); |
| 56 | + |
| 57 | + sp = new PUIScrollPanel(f); |
| 58 | + |
| 59 | + cb = new PUICheckBox(f); |
| 60 | + cb.addActionListener(new PUIAction() { |
| 61 | + @Override |
| 62 | + public void run(PUIElement that) { |
| 63 | + if (sp.getAlignment() == ElementAlignment.VERTICAL) { |
| 64 | + sp.setAlignment(ElementAlignment.HORIZONTAL); |
| 65 | + |
| 66 | + setMatrixElements(true); |
| 67 | + } else { |
| 68 | + sp.setAlignment(ElementAlignment.VERTICAL); |
| 69 | + setMatrixElements(false); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + rc = new PUIRotaryControl(f, 1); |
| 75 | + rc.addValueUpdateAction(new Runnable() { |
| 76 | + @Override |
| 77 | + public void run() { |
| 78 | + // RotaryControl changes spacing between elements in PUIScrollPanel & PUIMatrix |
| 79 | + int space = (int) (rc.getValue() * 7); |
| 80 | + sp.setElementSpacing(space, space, space * 2, space * 2); |
| 81 | + matrix.setElementSpacing(space, space, space, space); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + slider = new PUISlider(f); |
| 86 | + slider.setValue(0.5f); |
| 87 | + slider.setAlignment(ElementAlignment.HORIZONTAL); |
| 88 | + slider.addValueUpdateAction(new Runnable() { |
| 89 | + @Override |
| 90 | + public void run() { |
| 91 | + rc.setValueLength(slider.getValue()); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + slider2 = new PUISlider(f); |
| 96 | + slider2.setAlignment(ElementAlignment.HORIZONTAL); |
| 97 | + slider2.addValueUpdateAction(new Runnable() { |
| 98 | + @Override |
| 99 | + public void run() { |
| 100 | + rc.setValueThickness((int) (360 * slider2.getValue())); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + // add test-Buttons for scrollpanel |
| 105 | + for (int i = 1; i <= 10; i++) { |
| 106 | + PUIText t = new PUIText(f, "" + i); |
| 107 | + t.addActionListener(new PUIAction() { |
| 108 | + @Override |
| 109 | + public void run(PUIElement that) { |
| 110 | + f.setTitle(((PUIText) that).getText()); |
| 111 | + |
| 112 | + // automatically centers clicked Element in the UI |
| 113 | + sp.center(that); |
| 114 | + } |
| 115 | + }); |
| 116 | + sp.addElement(t); |
| 117 | + } |
| 118 | + // comment out if the size of the element inside of the panel should be further limited |
| 119 | +// sp.setElementSpacing(6,0,3,3); |
| 120 | + |
| 121 | + // prevent different colors when hovering/pressing |
| 122 | + for (PUIElement e : PUIElement.registeredElements) { |
| 123 | + e.doPaintOverOnHover(false); |
| 124 | + e.doPaintOverOnPress(false); |
| 125 | + } |
| 126 | + |
| 127 | + f.setUpdateElements(new PUIUpdatable() { // initialize updateMethod |
| 128 | + @Override |
| 129 | + public void update(int w, int h) { |
| 130 | + |
| 131 | + // Element-Positions can also be defined relative by using width & height variables |
| 132 | + |
| 133 | + cb.setBounds(w - 150, 50, 100, 100);// relative |
| 134 | + rc.setBounds(w - 150, 200, 100, 100);// relative |
| 135 | + sp.setBounds(50, h - 200, 300, 150); // relative |
| 136 | + |
| 137 | + matrix.setBounds(390, 340, w - 440, h - 390);// relative |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + // Set Position of other non-relative Elements |
| 142 | + darkmodeButton.setBounds(50, 50, 300, 100); |
| 143 | + slider.setBounds(50, 200, 300, 50); |
| 144 | + slider2.setBounds(50, 250, 300, 50); |
| 145 | + |
| 146 | + f.updateElements(); |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + public static void main(String[] args) { |
| 151 | + new Demo(); |
| 152 | + } |
| 153 | + |
| 154 | + public void setMatrixElements(boolean genHalf) { |
| 155 | + for (int i = 0; i < matrix.getColumns(); i++) { |
| 156 | + for (int j = 0; j < matrix.getRows(); j++) { |
| 157 | + f.remove(matrix.get(i, j)); |
| 158 | + } |
| 159 | + } |
| 160 | + matrix.reset(); |
| 161 | + if (genHalf) |
| 162 | + for (int i = 0; i < 4; i++) |
| 163 | + for (int j = (i % 2 == 0 ? 0 : 1); j < 8; j += 2) { |
| 164 | + PUIElement e = new PUIText(f, i + "," + j); |
| 165 | + e.addActionListener(new PUIAction() { |
| 166 | + @Override |
| 167 | + public void run(PUIElement that) { |
| 168 | + that.getFrame().setTitle(((PUIText) that).getText()); |
| 169 | + } |
| 170 | + }); |
| 171 | + matrix.setElement(e, i, j); |
| 172 | + } |
| 173 | + else |
| 174 | + for (int i = 0; i < 4; i++) |
| 175 | + for (int j = 0; j < 8; j++) { |
| 176 | + PUIElement e = new PUIText(f, i + "," + j); |
| 177 | + e.addActionListener(new PUIAction() { |
| 178 | + @Override |
| 179 | + public void run(PUIElement that) { |
| 180 | + that.getFrame().setTitle(((PUIText) that).getText()); |
| 181 | + } |
| 182 | + }); |
| 183 | + matrix.setElement(e, i, j); |
| 184 | + } |
| 185 | + |
| 186 | + // Checking MemoryOptimizations |
| 187 | +// System.out.println("Core-Elements: " + PUICore.getCore(f).getElements().size()); |
| 188 | +// System.out.println("Registered-Elements: " + PUIElement.registeredElements.size()); |
| 189 | + f.updateElements(); |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments