changed lock

changed placer to abstract class
This commit is contained in:
Macocian Adrian Radu
2020-06-10 21:41:05 +03:00
parent 58614024cc
commit 01bd4ce2e6
15 changed files with 106 additions and 525 deletions

View File

@@ -3,8 +3,6 @@
Name="Window" Name="Window"
Visible="True" Visible="True"
Title="Sudoku 1.0" Title="Sudoku 1.0"
EnableGPU="True"
HardwareAccelerated="True"
Size="1024, 576"> Size="1024, 576">
<GridPanel <GridPanel
BackgroundColor="#75ff75" BackgroundColor="#75ff75"
@@ -19,7 +17,7 @@
<Panel <Panel
BackgroundColor="#555555" BackgroundColor="#555555"
Name="Panel" Name="Panel"
Overlapping="false" Overlapping="true"
Row="0" Row="0"
Column="1"> Column="1">
<Picture <Picture
@@ -28,6 +26,7 @@
Name="Image" Name="Image"
Image="heart"/> Image="heart"/>
<RadioButtons <RadioButtons
Name="Radio Buttons"
Size="0.4f, 0.3f"> Size="0.4f, 0.3f">
<RadioButton <RadioButton
Text="Iubesc" Text="Iubesc"
@@ -53,6 +52,7 @@
BackgroundColor="#999999" BackgroundColor="#999999"
AccentColor="#aaaaaa" AccentColor="#aaaaaa"
ForegroundColor="#ffffff" ForegroundColor="#ffffff"
Name="Input Text Box"
Location="0.0f, 0.8f" Location="0.0f, 0.8f"
Size="300, 60"> Size="300, 60">
</InputTextBox>> </InputTextBox>>

View File

@@ -1,4 +1,5 @@
import guiTree.Components.Button; import guiTree.Components.Button;
import guiTree.Components.Panel;
import guiTree.Components.Picture; import guiTree.Components.Picture;
import guiTree.Window; import guiTree.Window;
import guiTree.events.MouseAdapter; import guiTree.events.MouseAdapter;
@@ -25,6 +26,7 @@ public class Main {
Button bottomCenter = (Button) window.findByName("bottomCenter"); Button bottomCenter = (Button) window.findByName("bottomCenter");
Button bottomRight = (Button) window.findByName("bottomRight"); Button bottomRight = (Button) window.findByName("bottomRight");
Picture picture = (Picture) window.findByName("Image"); Picture picture = (Picture) window.findByName("Image");
Panel panel = (Panel) window.findByName("Panel");
topLeft.addMouseListener(new MouseAdapter() { topLeft.addMouseListener(new MouseAdapter() {
@Override @Override

View File

@@ -1,55 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class BottomCenterPlacer implements Placer { public class BottomCenterPlacer extends Placer {
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public BottomCenterPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -1,55 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class BottomLeftPlacer implements Placer { public class BottomLeftPlacer extends Placer {
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public BottomLeftPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -1,55 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class BottomRightPlacer implements Placer { public class BottomRightPlacer extends Placer {
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public BottomRightPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -3,57 +3,7 @@ package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4; import guiTree.Helper.Point4;
public class GeneralPlacer implements Placer { public class GeneralPlacer extends Placer {
private Point2<Integer> location;
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point2<Float> relativeLocation;
private Point4<Integer> margin;
public GeneralPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
relativeLocation = new Point2<>(x, y);
}
@Override
public void setLocation(int x, int y) {
location = new Point2<>(x, y);
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -1,55 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class MiddleCenterPlacer implements Placer { public class MiddleCenterPlacer extends Placer {
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public MiddleCenterPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -1,56 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class MiddleLeftPlacer implements Placer { public class MiddleLeftPlacer extends Placer {
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public MiddleLeftPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -1,56 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class MiddleRightPlacer implements Placer { public class MiddleRightPlacer extends Placer {
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public MiddleRightPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -3,13 +3,58 @@ package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4; import guiTree.Helper.Point4;
public interface Placer { public abstract class Placer {
void setRelativeLocation(float x, float y); Point2<Integer> location;
void setLocation(int x, int y); Point2<Integer> size;
void setElementSize(int width, int height); Point2<Integer> parentSize;
void setParentSize(int width, int height); Point2<Float> relativeLocation;
void setMargins(int up, int down, int left, int right); Point4<Integer> margin;
void setMargins(int margin);
Point4<Integer> getMargins(); public Placer() {
Point2<Integer> getPosition(); margin = new Point4<>(0, 0, 0, 0);
}
public void setRelativeLocation(float x, float y) {
relativeLocation = new Point2<>(x, y);
}
public void setLocation(int x, int y) {
location = new Point2<>(x, y);
}
public Point2<Integer> getLocation() {
return location;
}
public Point2<Float> getRelativeLocation() {
return relativeLocation;
}
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
public Point4<Integer> getMargins() {
return margin;
}
abstract public Point2<Integer> getPosition();
} }

View File

@@ -1,56 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class TopCenterPlacer implements Placer{ public class TopCenterPlacer extends Placer{
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public TopCenterPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -1,52 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class TopLeftPlacer implements Placer { public class TopLeftPlacer extends Placer {
private Point4<Integer> margin;
public TopLeftPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float relativeX, float relativeY) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
}
@Override
public void setParentSize(int width, int height) {
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -1,55 +1,8 @@
package guiTree.Components.Decorations.Placers; package guiTree.Components.Decorations.Placers;
import guiTree.Helper.Point2; import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
public class TopRightPlacer implements Placer { public class TopRightPlacer extends Placer {
private Point2<Integer> size;
private Point2<Integer> parentSize;
private Point4<Integer> margin;
public TopRightPlacer() {
margin = new Point4<>(0, 0, 0, 0);
}
@Override
public void setRelativeLocation(float x, float y) {
}
@Override
public void setLocation(int x, int y) {
}
@Override
public void setElementSize(int width, int height) {
size = new Point2<>(width, height);
}
@Override
public void setParentSize(int width, int height) {
parentSize = new Point2<>(width, height);
}
@Override
public void setMargins(int up, int down, int left, int right) {
margin.a = up;
margin.b = down;
margin.c = left;
margin.d = right;
}
@Override
public void setMargins(int margin) {
this.margin.a = margin;
this.margin.b = margin;
this.margin.c = margin;
this.margin.d = margin;
}
@Override
public Point4<Integer> getMargins() {
return margin;
}
@Override @Override
public Point2<Integer> getPosition() { public Point2<Integer> getPosition() {

View File

@@ -20,7 +20,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
public class Visual { public class Visual {
@@ -72,7 +71,7 @@ public class Visual {
private static Visual entered; private static Visual entered;
private static Visual focused; private static Visual focused;
private Boolean pressed; private Boolean pressed;
private Lock validating; private static ReentrantLock validating = new ReentrantLock();
private Boolean hardwareAccelerated; private Boolean hardwareAccelerated;
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
@@ -114,7 +113,6 @@ public class Visual {
absoluteX = 0; absoluteX = 0;
absoluteY = 0; absoluteY = 0;
validating = new ReentrantLock();
hardwareAccelerated = useGPU; hardwareAccelerated = useGPU;
} }
@@ -210,6 +208,16 @@ public class Visual {
setLocation(); setLocation();
} }
public void setLocationX(Float x) {
locationPlacer.setRelativeLocation(x, getRelativeLocation().y);
setLocation();
}
public void setLocationY(Float y) {
locationPlacer.setRelativeLocation(getRelativeLocation().x, y);
setLocation();
}
public void setLocationX(Integer x) { public void setLocationX(Integer x) {
setLocation(x, getLocationY()); setLocation(x, getLocationY());
} }
@@ -287,6 +295,7 @@ public class Visual {
try { try {
this.font = Font.createFont(Font.TRUETYPE_FONT, new File("resources\\fonts\\" + font + ".ttf")); this.font = Font.createFont(Font.TRUETYPE_FONT, new File("resources\\fonts\\" + font + ".ttf"));
this.font = this.font.deriveFont(style, size); this.font = this.font.deriveFont(style, size);
update();
} catch (FontFormatException | IOException e) { } catch (FontFormatException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -371,6 +380,8 @@ public class Visual {
return new Point2<>(locationX, locationY); return new Point2<>(locationX, locationY);
} }
public Point2<Float> getRelativeLocation() {return locationPlacer.getRelativeLocation();}
public int getAbsoluteX() { public int getAbsoluteX() {
return absoluteX; return absoluteX;
} }
@@ -518,7 +529,12 @@ public class Visual {
} }
} }
if(dirty && active) { if(dirty && active) {
revalidate(); validating.lock();
try {
revalidate();
} finally {
validating.unlock();
}
} }
} }
@@ -526,25 +542,21 @@ public class Visual {
Debugger.log("Revalidating " + name, Debugger.Tag.PAINTING); Debugger.log("Revalidating " + name, Debugger.Tag.PAINTING);
Timer timer = new Timer(); Timer timer = new Timer();
validating.lock(); timer.startTiming();
try {
timer.startTiming();
clearImageBuffer(); clearImageBuffer();
paint(imageBuffer); paint(imageBuffer);
for (Visual v : children) { for (Visual v : children) {
if (v.dirty && v.active) { if (v.dirty && v.active) {
v.revalidate(); v.revalidate();
}
Graphics2D g = (Graphics2D) imageBuffer.getGraphics();
g.drawImage(v.imageBuffer, v.locationX, v.locationY, null);
g.dispose();
} }
Graphics2D g = (Graphics2D) imageBuffer.getGraphics();
dirty = false; g.drawImage(v.imageBuffer, v.locationX, v.locationY, null);
} finally { g.dispose();
validating.unlock();
} }
dirty = false;
if(!(this instanceof Window)){ if(!(this instanceof Window)){
long time = timer.stopTiming(); long time = timer.stopTiming();
Debugger.log("Finished Revalidating " + name + ": " + time, Debugger.Tag.PAINTING); Debugger.log("Finished Revalidating " + name + ": " + time, Debugger.Tag.PAINTING);
@@ -632,7 +644,6 @@ public class Visual {
} }
focused = entered; focused = entered;
Debugger.log("Pressed " + entered.name, Debugger.Tag.LISTENER); Debugger.log("Pressed " + entered.name, Debugger.Tag.LISTENER);
System.out.println(entered.name + " hardware accelerated: " + imageBuffer.getCapabilities(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()).isAccelerated());
} }
void mouseEntered(MouseEvent mouseEvent) { void mouseEntered(MouseEvent mouseEvent) {
@@ -824,19 +835,14 @@ public class Visual {
} }
public void update() { public void update() {
validating.lock(); if(!dirty) {
try { validating.lock();
dirty = true; dirty = true;
} finally {
validating.unlock(); validating.unlock();
} }
if(parent != null) { if(parent != null) {
parent.validating.lock(); parent.update();
try {
parent.update();
} finally {
parent.validating.unlock();
}
} }
} }
} }

View File

@@ -174,6 +174,7 @@ public class Window extends Visual implements Runnable{
public void setMainPanel(Panel panel) { public void setMainPanel(Panel panel) {
this.removeVisual(mainPanel); this.removeVisual(mainPanel);
mainPanel.setOverlapping(true);
contentPanel = new Panel(); contentPanel = new Panel();
contentPanel.setName("ContentPanel"); contentPanel.setName("ContentPanel");
if(titleBar != null) { if(titleBar != null) {