Skip to content

Commit 84740af

Browse files
author
realPaulsen
committed
V1.1.1b
New: - Added PUIMatrix, which automatically scales and positions Elements in a Grid-Pattern Fixes/Updates: - Every Element/Canvas is now rendered by using Graphics2D instead of Graphics because of more useful renderingmethods - PUIRotaryControl: + fixed displaying Value as lines (now Arc/Oval) - PUIScrollPanel: + fixed missed spacing between Elements when in Horizontal-Orientation - MemoryOptimizations: + PUIElements can now be released, where they are no longer referenced by in the PUICore & PUIElement-RegisteredElements-List + Java-GBC is called every 10s when Elements have been released and are not further used by PUIElement, Frame or PUICore
1 parent a566ee2 commit 84740af

12 files changed

+387
-178
lines changed
Lines changed: 192 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,192 @@
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+
}

src/com/paulsen/ui/PUICanvas.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
public class PUICanvas {
66

7+
private final long creationID = System.currentTimeMillis(); // creation time
8+
79
protected PUIFrame frame;
810
protected PUIPaintable paint;
911

@@ -30,7 +32,7 @@ private void init() {
3032
frame.add(this);
3133
}
3234

33-
public synchronized void draw(Graphics g) {
35+
public synchronized void draw(Graphics2D g) {
3436
if (g != null && frame != null)
3537
paint.paint(g, 0, 0, frame.w(), frame.h());
3638
}
@@ -62,4 +64,8 @@ public int getDrawLayer() {
6264
public void setDrawLayer(int drawLayer) {
6365
this.drawLayer = drawLayer;
6466
}
67+
68+
public long getCreationID() {
69+
return creationID;
70+
}
6571
}

src/com/paulsen/ui/PUICheckBox.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void run(PUIElement that) {
2828
}
2929

3030
@Override
31-
public void draw(Graphics g) {
31+
public void draw(Graphics2D g) {
3232
super.draw(g);
3333

3434
if (!activated)

src/com/paulsen/ui/PUICore.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import javax.management.InvalidAttributeValueException;
44
import java.awt.event.*;
5+
import java.util.ArrayList;
56
import java.util.Comparator;
7+
import java.util.Timer;
8+
import java.util.TimerTask;
69
import java.util.concurrent.CopyOnWriteArrayList;
710

811
public final class PUICore {
@@ -103,6 +106,15 @@ public void mouseWheelMoved(MouseWheelEvent e) {
103106
}
104107
}
105108
});
109+
110+
// init java-GBC
111+
new Timer().scheduleAtFixedRate(new TimerTask() {
112+
@Override
113+
public void run() {
114+
if (PUIElement.useGBC)
115+
System.gc();
116+
}
117+
}, 0, 10000);
106118
}
107119

108120
public void addElement(PUIElement e) {
@@ -130,7 +142,11 @@ public int compare(PUIElement o1, PUIElement o2) {
130142
return -1;
131143
if (o1.getInteractionLayer() < o2.getInteractionLayer())
132144
return 1;
133-
return 0;
145+
146+
// same layer but different time
147+
if (o1.getCreationID() < o2.getCreationID())
148+
return -1;
149+
return 1;
134150
}
135151
};
136152
elements.sort(comp);
@@ -141,4 +157,8 @@ public int compare(PUIElement o1, PUIElement o2) {
141157
// }
142158
}
143159

160+
public ArrayList<PUIElement> getElements() {
161+
return new ArrayList<>(elements);
162+
}
163+
144164
}

0 commit comments

Comments
 (0)