From 01bd4ce2e6845f3d6f67b458ac880f07c7a8821c Mon Sep 17 00:00:00 2001 From: Macocian Adrian Radu <34056556+macocianradu@users.noreply.github.com> Date: Wed, 10 Jun 2020 21:41:05 +0300 Subject: [PATCH] changed lock changed placer to abstract class --- resources/ui.xml | 6 +- src/Main.java | 2 + .../Placers/BottomCenterPlacer.java | 49 +------------- .../Decorations/Placers/BottomLeftPlacer.java | 49 +------------- .../Placers/BottomRightPlacer.java | 49 +------------- .../Decorations/Placers/GeneralPlacer.java | 52 +-------------- .../Placers/MiddleCenterPlacer.java | 49 +------------- .../Decorations/Placers/MiddleLeftPlacer.java | 50 +------------- .../Placers/MiddleRightPlacer.java | 50 +------------- .../Decorations/Placers/Placer.java | 63 +++++++++++++++--- .../Decorations/Placers/TopCenterPlacer.java | 50 +------------- .../Decorations/Placers/TopLeftPlacer.java | 46 +------------ .../Decorations/Placers/TopRightPlacer.java | 49 +------------- src/guiTree/Visual.java | 66 ++++++++++--------- src/guiTree/Window.java | 1 + 15 files changed, 106 insertions(+), 525 deletions(-) diff --git a/resources/ui.xml b/resources/ui.xml index 2f2e175..3d9514c 100644 --- a/resources/ui.xml +++ b/resources/ui.xml @@ -3,8 +3,6 @@ Name="Window" Visible="True" Title="Sudoku 1.0" - EnableGPU="True" - HardwareAccelerated="True" Size="1024, 576"> > diff --git a/src/Main.java b/src/Main.java index d1df8fa..c5bb66b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,5 @@ import guiTree.Components.Button; +import guiTree.Components.Panel; import guiTree.Components.Picture; import guiTree.Window; import guiTree.events.MouseAdapter; @@ -25,6 +26,7 @@ public class Main { Button bottomCenter = (Button) window.findByName("bottomCenter"); Button bottomRight = (Button) window.findByName("bottomRight"); Picture picture = (Picture) window.findByName("Image"); + Panel panel = (Panel) window.findByName("Panel"); topLeft.addMouseListener(new MouseAdapter() { @Override diff --git a/src/guiTree/Components/Decorations/Placers/BottomCenterPlacer.java b/src/guiTree/Components/Decorations/Placers/BottomCenterPlacer.java index b629a5c..bfae8b0 100644 --- a/src/guiTree/Components/Decorations/Placers/BottomCenterPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/BottomCenterPlacer.java @@ -1,55 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class BottomCenterPlacer implements Placer { - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class BottomCenterPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/BottomLeftPlacer.java b/src/guiTree/Components/Decorations/Placers/BottomLeftPlacer.java index 7e3227b..e7dc288 100644 --- a/src/guiTree/Components/Decorations/Placers/BottomLeftPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/BottomLeftPlacer.java @@ -1,55 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class BottomLeftPlacer implements Placer { - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class BottomLeftPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/BottomRightPlacer.java b/src/guiTree/Components/Decorations/Placers/BottomRightPlacer.java index 538a991..c216bd1 100644 --- a/src/guiTree/Components/Decorations/Placers/BottomRightPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/BottomRightPlacer.java @@ -1,55 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class BottomRightPlacer implements Placer { - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class BottomRightPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/GeneralPlacer.java b/src/guiTree/Components/Decorations/Placers/GeneralPlacer.java index b1db4b1..8296294 100644 --- a/src/guiTree/Components/Decorations/Placers/GeneralPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/GeneralPlacer.java @@ -3,57 +3,7 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; import guiTree.Helper.Point4; -public class GeneralPlacer implements Placer { - private Point2 location; - private Point2 size; - private Point2 parentSize; - private Point2 relativeLocation; - private Point4 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 getMargins() { - return margin; - } +public class GeneralPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/MiddleCenterPlacer.java b/src/guiTree/Components/Decorations/Placers/MiddleCenterPlacer.java index 9266064..2712a22 100644 --- a/src/guiTree/Components/Decorations/Placers/MiddleCenterPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/MiddleCenterPlacer.java @@ -1,55 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class MiddleCenterPlacer implements Placer { - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class MiddleCenterPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/MiddleLeftPlacer.java b/src/guiTree/Components/Decorations/Placers/MiddleLeftPlacer.java index b73f9d8..d80d99b 100644 --- a/src/guiTree/Components/Decorations/Placers/MiddleLeftPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/MiddleLeftPlacer.java @@ -1,56 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class MiddleLeftPlacer implements Placer { - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class MiddleLeftPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/MiddleRightPlacer.java b/src/guiTree/Components/Decorations/Placers/MiddleRightPlacer.java index d491107..1e680b8 100644 --- a/src/guiTree/Components/Decorations/Placers/MiddleRightPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/MiddleRightPlacer.java @@ -1,56 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class MiddleRightPlacer implements Placer { - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class MiddleRightPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/Placer.java b/src/guiTree/Components/Decorations/Placers/Placer.java index c5dad88..5b89736 100644 --- a/src/guiTree/Components/Decorations/Placers/Placer.java +++ b/src/guiTree/Components/Decorations/Placers/Placer.java @@ -3,13 +3,58 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; import guiTree.Helper.Point4; -public interface Placer { - void setRelativeLocation(float x, float y); - void setLocation(int x, int y); - void setElementSize(int width, int height); - void setParentSize(int width, int height); - void setMargins(int up, int down, int left, int right); - void setMargins(int margin); - Point4 getMargins(); - Point2 getPosition(); +public abstract class Placer { + Point2 location; + Point2 size; + Point2 parentSize; + Point2 relativeLocation; + Point4 margin; + + public Placer() { + 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 getLocation() { + return location; + } + + public Point2 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 getMargins() { + return margin; + } + + abstract public Point2 getPosition(); } diff --git a/src/guiTree/Components/Decorations/Placers/TopCenterPlacer.java b/src/guiTree/Components/Decorations/Placers/TopCenterPlacer.java index 385a2be..d6df786 100644 --- a/src/guiTree/Components/Decorations/Placers/TopCenterPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/TopCenterPlacer.java @@ -1,56 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class TopCenterPlacer implements Placer{ - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class TopCenterPlacer extends Placer{ @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/TopLeftPlacer.java b/src/guiTree/Components/Decorations/Placers/TopLeftPlacer.java index a60d632..4532b30 100644 --- a/src/guiTree/Components/Decorations/Placers/TopLeftPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/TopLeftPlacer.java @@ -1,52 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class TopLeftPlacer implements Placer { - private Point4 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 getMargins() { - return margin; - } +public class TopLeftPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Components/Decorations/Placers/TopRightPlacer.java b/src/guiTree/Components/Decorations/Placers/TopRightPlacer.java index f155ef9..5458d33 100644 --- a/src/guiTree/Components/Decorations/Placers/TopRightPlacer.java +++ b/src/guiTree/Components/Decorations/Placers/TopRightPlacer.java @@ -1,55 +1,8 @@ package guiTree.Components.Decorations.Placers; import guiTree.Helper.Point2; -import guiTree.Helper.Point4; -public class TopRightPlacer implements Placer { - private Point2 size; - private Point2 parentSize; - private Point4 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 getMargins() { - return margin; - } +public class TopRightPlacer extends Placer { @Override public Point2 getPosition() { diff --git a/src/guiTree/Visual.java b/src/guiTree/Visual.java index c547217..63edb08 100644 --- a/src/guiTree/Visual.java +++ b/src/guiTree/Visual.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.IOException; import java.util.*; import java.util.List; -import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Visual { @@ -72,7 +71,7 @@ public class Visual { private static Visual entered; private static Visual focused; private Boolean pressed; - private Lock validating; + private static ReentrantLock validating = new ReentrantLock(); private Boolean hardwareAccelerated; /*-------------------------------------------------------------------- @@ -114,7 +113,6 @@ public class Visual { absoluteX = 0; absoluteY = 0; - validating = new ReentrantLock(); hardwareAccelerated = useGPU; } @@ -210,6 +208,16 @@ public class Visual { 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) { setLocation(x, getLocationY()); } @@ -287,6 +295,7 @@ public class Visual { try { this.font = Font.createFont(Font.TRUETYPE_FONT, new File("resources\\fonts\\" + font + ".ttf")); this.font = this.font.deriveFont(style, size); + update(); } catch (FontFormatException | IOException e) { e.printStackTrace(); } @@ -371,6 +380,8 @@ public class Visual { return new Point2<>(locationX, locationY); } + public Point2 getRelativeLocation() {return locationPlacer.getRelativeLocation();} + public int getAbsoluteX() { return absoluteX; } @@ -518,7 +529,12 @@ public class Visual { } } 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); Timer timer = new Timer(); - validating.lock(); - try { - timer.startTiming(); + timer.startTiming(); - clearImageBuffer(); - paint(imageBuffer); - for (Visual v : children) { - if (v.dirty && v.active) { - v.revalidate(); - } - Graphics2D g = (Graphics2D) imageBuffer.getGraphics(); - g.drawImage(v.imageBuffer, v.locationX, v.locationY, null); - g.dispose(); + clearImageBuffer(); + paint(imageBuffer); + for (Visual v : children) { + if (v.dirty && v.active) { + v.revalidate(); } - - dirty = false; - } finally { - validating.unlock(); + Graphics2D g = (Graphics2D) imageBuffer.getGraphics(); + g.drawImage(v.imageBuffer, v.locationX, v.locationY, null); + g.dispose(); } + + dirty = false; + if(!(this instanceof Window)){ long time = timer.stopTiming(); Debugger.log("Finished Revalidating " + name + ": " + time, Debugger.Tag.PAINTING); @@ -632,7 +644,6 @@ public class Visual { } focused = entered; 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) { @@ -824,19 +835,14 @@ public class Visual { } public void update() { - validating.lock(); - try { + if(!dirty) { + validating.lock(); dirty = true; - } finally { validating.unlock(); } + if(parent != null) { - parent.validating.lock(); - try { - parent.update(); - } finally { - parent.validating.unlock(); - } + parent.update(); } } } diff --git a/src/guiTree/Window.java b/src/guiTree/Window.java index bb5585d..b150249 100644 --- a/src/guiTree/Window.java +++ b/src/guiTree/Window.java @@ -174,6 +174,7 @@ public class Window extends Visual implements Runnable{ public void setMainPanel(Panel panel) { this.removeVisual(mainPanel); + mainPanel.setOverlapping(true); contentPanel = new Panel(); contentPanel.setName("ContentPanel"); if(titleBar != null) {