mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 13:40:04 +00:00
done panel with overlapping
This commit is contained in:
BIN
resources/icons/normalize_black.png
Normal file
BIN
resources/icons/normalize_black.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 277 B |
BIN
resources/icons/normalize_white.png
Normal file
BIN
resources/icons/normalize_white.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 302 B |
@@ -1,10 +1,12 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<Window Name="window" Visible="true" Size="640, 480">
|
<Window Name="window" Visible="true" Size="640, 480">
|
||||||
<Button Name="button1" BackgroundColor="#990000" Size="200, 200" Label="button1">
|
<!-- <Panel Name="MainPanel" BackgroundColor="#FFFFFF" Size="1.0, 1.0" Overlapping="false">-->
|
||||||
<Button Name="button4" BackgroundColor="#009999" Size="100, 100" Icon="square_white">
|
<Button Name="button1" BackgroundColor="#990000" Size="200, 200" Label="button1">
|
||||||
<Button Name="button2" BackgroundColor="#000099" Size="50, 50" Label="button2"/>
|
<Button Name="button4" BackgroundColor="#009999" Size="100, 100" Icon="square_white">
|
||||||
|
<Button Name="button2" BackgroundColor="#000099" Size="50, 50" Label="button2"/>
|
||||||
|
</Button>
|
||||||
</Button>
|
</Button>
|
||||||
</Button>
|
<Button Name="button6" BackgroundColor="#555555" Location="350, 350" Size="100, 100"/>
|
||||||
<Button Name="button6" Location="350, 350" Size="100, 100"/>
|
<Button Name="button3" BackgroundColor="#009900" Size="100, 100" Location="300, 300" Icon="close_black"/>
|
||||||
<Button Name="button3" BackgroundColor="#009900" Size="100, 100" Location="300, 300" Icon="close_black"/>
|
<!-- </Panel>-->
|
||||||
</Window>
|
</Window>
|
||||||
@@ -1,15 +1,11 @@
|
|||||||
import guiTree.Components.Button;
|
import guiTree.Components.Button;
|
||||||
import guiTree.Components.TitleBar;
|
import guiTree.Components.Panel;
|
||||||
import guiTree.Window;
|
import guiTree.Window;
|
||||||
|
import guiTree.events.MouseAdapter;
|
||||||
import parser.XAMLParser;
|
import parser.XAMLParser;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.WindowAdapter;
|
|
||||||
import java.awt.event.WindowEvent;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -17,17 +13,20 @@ public class Main {
|
|||||||
Window window = XAMLParser.parse("ui.xml");
|
Window window = XAMLParser.parse("ui.xml");
|
||||||
assert window != null;
|
assert window != null;
|
||||||
window.revalidate();
|
window.revalidate();
|
||||||
Button button = (Button)window.findByName("button1");
|
Button button = (Button)window.findByName("button3");
|
||||||
|
button.addMouseListener(new MouseAdapter() {
|
||||||
BufferedImage icon = null;
|
@Override
|
||||||
try {
|
public void mouseClicked(MouseEvent mouseEvent) {
|
||||||
icon = ImageIO.read(new File("resources\\icons\\square_white.png"));
|
Panel panel = window.getMainPanel();
|
||||||
} catch (IOException e) {
|
if(panel.getOverlapping()) {
|
||||||
e.printStackTrace();
|
panel.setOverlapping(false);
|
||||||
}
|
}
|
||||||
TitleBar bar = new TitleBar("Working Title", icon);
|
else {
|
||||||
bar.setBackgroundColor(Color.GRAY);
|
panel.setOverlapping(true);
|
||||||
window.setTitleBar(bar);
|
}
|
||||||
|
window.revalidate();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
window.revalidate();
|
window.revalidate();
|
||||||
long now;
|
long now;
|
||||||
|
|||||||
@@ -37,36 +37,30 @@ public class Button extends Visual {
|
|||||||
this.addMouseListener(new MouseAdapter() {
|
this.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent mouseEvent) {
|
public void mousePressed(MouseEvent mouseEvent) {
|
||||||
System.out.println("Pressed: " + getName());
|
|
||||||
pressed = true;
|
pressed = true;
|
||||||
revalidate();
|
revalidate();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void mouseReleased(MouseEvent mouseEvent) {
|
public void mouseReleased(MouseEvent mouseEvent) {
|
||||||
System.out.println("Released: " + getName());
|
|
||||||
pressed = false;
|
pressed = false;
|
||||||
revalidate();
|
revalidate();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void mouseEntered(MouseEvent mouseEvent) {
|
public void mouseEntered(MouseEvent mouseEvent) {
|
||||||
System.out.println("Entered: " + getName());
|
|
||||||
hovered = true;
|
hovered = true;
|
||||||
revalidate();
|
revalidate();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void mouseExited(MouseEvent mouseEvent) {
|
public void mouseExited(MouseEvent mouseEvent) {
|
||||||
System.out.println("Exited: " + getName());
|
|
||||||
hovered = false;
|
hovered = false;
|
||||||
revalidate();
|
revalidate();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void mouseDragged(MouseEvent mouseEvent) {
|
public void mouseDragged(MouseEvent mouseEvent) {
|
||||||
System.out.println("DRAGGING: " + getName());
|
|
||||||
revalidate();
|
revalidate();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved(MouseEvent mouseEvent) {
|
public void mouseMoved(MouseEvent mouseEvent) {
|
||||||
System.out.println("Moved: " + getName());
|
|
||||||
revalidate();
|
revalidate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
107
src/guiTree/Components/Panel.java
Normal file
107
src/guiTree/Components/Panel.java
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package guiTree.Components;
|
||||||
|
|
||||||
|
import guiTree.Visual;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Panel extends Visual {
|
||||||
|
List<Visual> visuals;
|
||||||
|
private Boolean overlapping;
|
||||||
|
|
||||||
|
public Panel() {
|
||||||
|
overlapping = false;
|
||||||
|
visuals = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOverlapping(Boolean overlapping) {
|
||||||
|
this.overlapping = overlapping;
|
||||||
|
reposition();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getOverlapping() {
|
||||||
|
return this.overlapping;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculateSize(Visual v) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculatePosition(Visual v) {
|
||||||
|
if(!overlapping) {
|
||||||
|
boolean ok = false;
|
||||||
|
while(!ok) {
|
||||||
|
ok = true;
|
||||||
|
for (int i = 0; i < visuals.size(); i++) {
|
||||||
|
Visual v2 = visuals.get(i);
|
||||||
|
if (isOverlapping(v, v2) && v != v2) {
|
||||||
|
System.out.println(v + " Overlapping with: " + v2);
|
||||||
|
if(v.getLocationX() + v.getWidth() + v2.getWidth() > this.getWidth()) {
|
||||||
|
if(v.getLocationY() + v.getHeight() + v2.getHeight() > this.getHeight()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
v.setLocationY(v2.getLocationY() + v2.getHeight());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
v.setLocationX(v2.getLocationX() + v2.getWidth());
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(v.getLocationX() + v.getWidth() > this.getWidth()) {
|
||||||
|
ok = false;
|
||||||
|
v.setLocationY(v.getLocationY() + 10);
|
||||||
|
}
|
||||||
|
if(v.getLocationY() > this.getHeight()) {
|
||||||
|
v.setLocation(0, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reposition() {
|
||||||
|
for(int i = visuals.size() - 1; i >= 0; i--) {
|
||||||
|
calculatePosition(visuals.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addVisual(Visual v) {
|
||||||
|
calculatePosition(v);
|
||||||
|
super.addVisual(v);
|
||||||
|
visuals.add(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Boolean isOverlapping(Visual v1, Visual v2) {
|
||||||
|
int l1x = v1.getLocationX();
|
||||||
|
int r1x = v1.getLocationX() + v1.getWidth();
|
||||||
|
int l1y = v1.getLocationY();
|
||||||
|
int r1y = v1.getLocationY() + v1.getHeight();
|
||||||
|
int l2x = v2.getLocationX();
|
||||||
|
int r2x = v2.getLocationX() + v2.getWidth();
|
||||||
|
int l2y = v2.getLocationY();
|
||||||
|
int r2y = v2.getLocationY() + v2.getHeight();
|
||||||
|
|
||||||
|
if(l1x > r2x || l2x > r1x) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return l1y <= r2y && l2y <= r1y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleNotification(int notify) {
|
||||||
|
if(notify == TitleBar.CLOSE || notify == TitleBar.MAXIMIZE ||
|
||||||
|
notify == TitleBar.MINIMIZE || notify == TitleBar.NORMALIZE) {
|
||||||
|
notifyParent(notify);
|
||||||
|
// return;
|
||||||
|
}
|
||||||
|
// if(notify == SIZE_CHANGED || notify == LOCATION_CHANGED) {
|
||||||
|
// reposition();
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package guiTree.Components;
|
package guiTree.Components;
|
||||||
|
|
||||||
import guiTree.Helper.Point2d;
|
|
||||||
import guiTree.Visual;
|
import guiTree.Visual;
|
||||||
import guiTree.events.MouseAdapter;
|
import guiTree.events.MouseAdapter;
|
||||||
|
|
||||||
@@ -9,9 +8,10 @@ import java.awt.event.MouseEvent;
|
|||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
public class TitleBar extends Visual {
|
public class TitleBar extends Visual {
|
||||||
public static final int MINIMIZE = 1;
|
public static final int CLOSE = 10;
|
||||||
public static final int MAXIMIZE = 2;
|
public static final int MINIMIZE = 11;
|
||||||
public static final int CLOSE = 3;
|
public static final int NORMALIZE = 12;
|
||||||
|
public static final int MAXIMIZE = 13;
|
||||||
|
|
||||||
private BufferedImage icon;
|
private BufferedImage icon;
|
||||||
private String title;
|
private String title;
|
||||||
@@ -60,22 +60,39 @@ public class TitleBar extends Visual {
|
|||||||
this.setLocation(0, 0);
|
this.setLocation(0, 0);
|
||||||
|
|
||||||
setButtonLocation();
|
setButtonLocation();
|
||||||
|
|
||||||
|
this.addVisual(close);
|
||||||
|
this.addVisual(minimize);
|
||||||
|
this.addVisual(maximize);
|
||||||
|
minimize.setName("minimize");
|
||||||
|
maximize.setName("maximize");
|
||||||
|
close.setName("close");
|
||||||
close.addMouseListener(new MouseAdapter() {
|
close.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent mouseEvent) {
|
public void mouseReleased(MouseEvent mouseEvent) {
|
||||||
notifyParent(3);
|
notifyParent(CLOSE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
maximize.addMouseListener(new MouseAdapter() {
|
maximize.addMouseListener(new MouseAdapter() {
|
||||||
|
private boolean isMaximized = false;
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent mouseEvent) {
|
public void mouseReleased(MouseEvent mouseEvent) {
|
||||||
notifyParent(2);
|
if(isMaximized) {
|
||||||
|
notifyParent(NORMALIZE);
|
||||||
|
maximize.setIcon("square_white");
|
||||||
|
isMaximized = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
maximize.setIcon("normalize_white");
|
||||||
|
notifyParent(MAXIMIZE);
|
||||||
|
isMaximized = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
minimize.addMouseListener(new MouseAdapter() {
|
minimize.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent mouseEvent) {
|
public void mouseReleased(MouseEvent mouseEvent) {
|
||||||
notifyParent(1);
|
notifyParent(MINIMIZE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -134,15 +151,9 @@ public class TitleBar extends Visual {
|
|||||||
private void setButtonLocation() {
|
private void setButtonLocation() {
|
||||||
int buttonOffset = this.getWidth() - close.getWidth();
|
int buttonOffset = this.getWidth() - close.getWidth();
|
||||||
close.setLocation(buttonOffset, 0);
|
close.setLocation(buttonOffset, 0);
|
||||||
close.setName("close");
|
|
||||||
buttonOffset -= maximize.getWidth();
|
buttonOffset -= maximize.getWidth();
|
||||||
maximize.setLocation(buttonOffset, 0);
|
maximize.setLocation(buttonOffset, 0);
|
||||||
maximize.setName("maximize");
|
|
||||||
buttonOffset -= minimize.getWidth();
|
buttonOffset -= minimize.getWidth();
|
||||||
minimize.setLocation(buttonOffset, 0);
|
minimize.setLocation(buttonOffset, 0);
|
||||||
minimize.setName("minimize");
|
|
||||||
this.addVisual(close);
|
|
||||||
this.addVisual(minimize);
|
|
||||||
this.addVisual(maximize);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/guiTree/Listeners/Direction.java
Normal file
9
src/guiTree/Listeners/Direction.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package guiTree.Listeners;
|
||||||
|
|
||||||
|
public enum Direction {
|
||||||
|
NORTH,
|
||||||
|
SOUTH,
|
||||||
|
WEST,
|
||||||
|
EAST,
|
||||||
|
ALL
|
||||||
|
}
|
||||||
186
src/guiTree/Listeners/ResizeListener.java
Normal file
186
src/guiTree/Listeners/ResizeListener.java
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
package guiTree.Listeners;
|
||||||
|
|
||||||
|
import guiTree.Helper.Point2d;
|
||||||
|
import guiTree.events.MouseAdapter;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
|
||||||
|
public class ResizeListener extends MouseAdapter {
|
||||||
|
public Point2d location;
|
||||||
|
public Point2d size;
|
||||||
|
public Cursor cursor;
|
||||||
|
private Boolean moving = false;
|
||||||
|
private Boolean resizing = false;
|
||||||
|
private Point2d initialLocation;
|
||||||
|
private int resizeDelta = 5;
|
||||||
|
private Boolean NORTH;
|
||||||
|
private Boolean SOUTH;
|
||||||
|
private Boolean WEST;
|
||||||
|
private Boolean EAST;
|
||||||
|
|
||||||
|
public ResizeListener(Direction[] directions, Point2d size, Point2d location) {
|
||||||
|
this.cursor = new Cursor(Cursor.DEFAULT_CURSOR);
|
||||||
|
this.size = size;
|
||||||
|
this.location = location;
|
||||||
|
NORTH = false;
|
||||||
|
SOUTH = false;
|
||||||
|
WEST = false;
|
||||||
|
EAST = false;
|
||||||
|
for(Direction dir:directions) {
|
||||||
|
if(dir == Direction.NORTH) {
|
||||||
|
NORTH = true;
|
||||||
|
}
|
||||||
|
if(dir == Direction.SOUTH) {
|
||||||
|
SOUTH = true;
|
||||||
|
}
|
||||||
|
if(dir == Direction.WEST) {
|
||||||
|
WEST = true;
|
||||||
|
}
|
||||||
|
if(dir == Direction.EAST) {
|
||||||
|
EAST = true;
|
||||||
|
}
|
||||||
|
if(dir == Direction.ALL) {
|
||||||
|
NORTH = true;
|
||||||
|
SOUTH = true;
|
||||||
|
WEST = true;
|
||||||
|
EAST = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocation(int x, int y) {
|
||||||
|
location.x = x;
|
||||||
|
location.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSize(int x, int y) {
|
||||||
|
size.x = x;
|
||||||
|
size.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent mouseEvent) {
|
||||||
|
this.cursor = new Cursor(Cursor.DEFAULT_CURSOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent mouseEvent) {
|
||||||
|
if(moving) {
|
||||||
|
setLocation(mouseEvent.getXOnScreen() - initialLocation.x, mouseEvent.getYOnScreen() - initialLocation.y);
|
||||||
|
}
|
||||||
|
else if(!resizing){
|
||||||
|
this.resizing = true;
|
||||||
|
this.resize(mouseEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent mouseEvent) {
|
||||||
|
if(cursor.getType() != Cursor.DEFAULT_CURSOR){
|
||||||
|
this.moving = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.moving = true;
|
||||||
|
this.resizing = false;
|
||||||
|
}
|
||||||
|
initialLocation = new Point2d(mouseEvent.getX(), mouseEvent.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent mouseEvent) {
|
||||||
|
moving = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseMoved(MouseEvent mouseEvent) {
|
||||||
|
if(mouseEvent.getX() < resizeDelta || mouseEvent.getX() > size.x - resizeDelta ||
|
||||||
|
mouseEvent.getY() < resizeDelta || mouseEvent.getY() > size.y - resizeDelta) {
|
||||||
|
this.setResizeCursor(mouseEvent);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cursor = new Cursor(Cursor.DEFAULT_CURSOR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setResizeCursor(MouseEvent e){
|
||||||
|
if(e.getX() <= resizeDelta) {
|
||||||
|
if(e.getY() <= resizeDelta && (NORTH && WEST)) {
|
||||||
|
cursor = new Cursor(Cursor.NW_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
else if(e.getY() >= size.y - resizeDelta && (SOUTH && WEST)){
|
||||||
|
cursor = new Cursor(Cursor.SW_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
else if(WEST){
|
||||||
|
cursor = new Cursor(Cursor.W_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(e.getX() >= size.x - resizeDelta){
|
||||||
|
if(e.getY() <= resizeDelta && (NORTH && EAST)){
|
||||||
|
cursor = new Cursor(Cursor.NE_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
else if(e.getY() >= size.y - resizeDelta && (SOUTH && EAST)){
|
||||||
|
cursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
else if(EAST){
|
||||||
|
cursor = new Cursor(Cursor.E_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(e.getY() <= resizeDelta && (NORTH)){
|
||||||
|
cursor = new Cursor(Cursor.N_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
else if(e.getY() >= size.y - resizeDelta && (SOUTH)){
|
||||||
|
cursor = new Cursor(Cursor.S_RESIZE_CURSOR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize(MouseEvent e) {
|
||||||
|
switch (cursor.getType()) {
|
||||||
|
case Cursor.N_RESIZE_CURSOR:
|
||||||
|
setSize(size.x, size.y + initialLocation.y - e.getYOnScreen());
|
||||||
|
setLocation(location.x, e.getYOnScreen());
|
||||||
|
initialLocation.y = e.getYOnScreen();
|
||||||
|
break;
|
||||||
|
case Cursor.NE_RESIZE_CURSOR:
|
||||||
|
setSize(size.x + e.getXOnScreen() - initialLocation.x, size.y + initialLocation.y - e.getYOnScreen());
|
||||||
|
setLocation(location.x, e.getYOnScreen());
|
||||||
|
initialLocation.x = e.getXOnScreen();
|
||||||
|
initialLocation.y = e.getYOnScreen();
|
||||||
|
break;
|
||||||
|
case Cursor.NW_RESIZE_CURSOR:
|
||||||
|
setSize(size.x + initialLocation.x - e.getXOnScreen(), size.y + initialLocation.y - e.getYOnScreen());
|
||||||
|
setLocation(e.getXOnScreen(), e.getYOnScreen());
|
||||||
|
initialLocation.x = e.getXOnScreen();
|
||||||
|
initialLocation.y = e.getYOnScreen();
|
||||||
|
break;
|
||||||
|
case Cursor.E_RESIZE_CURSOR:
|
||||||
|
setSize(size.x + e.getXOnScreen() - initialLocation.x, size.y);
|
||||||
|
initialLocation.x = e.getXOnScreen();
|
||||||
|
break;
|
||||||
|
case Cursor.W_RESIZE_CURSOR:
|
||||||
|
setSize(size.x + initialLocation.x - e.getXOnScreen(), size.y);
|
||||||
|
setLocation(e.getXOnScreen(), location.y);
|
||||||
|
initialLocation.x = e.getXOnScreen();
|
||||||
|
break;
|
||||||
|
case Cursor.SE_RESIZE_CURSOR:
|
||||||
|
setSize(size.x + e.getXOnScreen() - initialLocation.x, size.y + e.getYOnScreen() - initialLocation.y);
|
||||||
|
initialLocation.x = e.getXOnScreen();
|
||||||
|
initialLocation.y = e.getYOnScreen();
|
||||||
|
break;
|
||||||
|
case Cursor.S_RESIZE_CURSOR:
|
||||||
|
setSize(size.x, size.y + e.getYOnScreen() - initialLocation.y);
|
||||||
|
initialLocation.y = e.getYOnScreen();
|
||||||
|
break;
|
||||||
|
case Cursor.SW_RESIZE_CURSOR:
|
||||||
|
setSize(size.x + initialLocation.x - e.getXOnScreen(), size.y + e.getYOnScreen() - initialLocation.y);
|
||||||
|
setLocation(e.getXOnScreen(), location.y);
|
||||||
|
initialLocation.x = e.getXOnScreen();
|
||||||
|
initialLocation.y = e.getYOnScreen();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
this.resizing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,11 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Visual {
|
public class Visual {
|
||||||
|
/*--------------------------------------------------------------------
|
||||||
|
Constant Values
|
||||||
|
---------------------------------------------------------------------*/
|
||||||
|
public static final int SIZE_CHANGED = 1;
|
||||||
|
public static final int LOCATION_CHANGED = 2;
|
||||||
|
|
||||||
/*--------------------------------------------------------------------
|
/*--------------------------------------------------------------------
|
||||||
Tree Elements
|
Tree Elements
|
||||||
@@ -57,6 +62,7 @@ public class Visual {
|
|||||||
this.mouseListeners = new ArrayList<>();
|
this.mouseListeners = new ArrayList<>();
|
||||||
this.keyListeners = new ArrayList<>();
|
this.keyListeners = new ArrayList<>();
|
||||||
this.parent = null;
|
this.parent = null;
|
||||||
|
this.name = "";
|
||||||
this.backgroundColor = Color.WHITE;
|
this.backgroundColor = Color.WHITE;
|
||||||
this.foregroundColor = Color.BLUE;
|
this.foregroundColor = Color.BLUE;
|
||||||
this.fontColor = Color.BLACK;
|
this.fontColor = Color.BLACK;
|
||||||
@@ -88,12 +94,27 @@ public class Visual {
|
|||||||
initializeImageBuffer();
|
initializeImageBuffer();
|
||||||
|
|
||||||
this.dirty = true;
|
this.dirty = true;
|
||||||
|
this.notifyParent(SIZE_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public void setSize(Float width, Float height) {
|
||||||
|
// this.width = Math.round(this.parent.width * width);
|
||||||
|
// this.height = Math.round(this.parent.height * height);
|
||||||
|
// }
|
||||||
|
|
||||||
public void setLocation(Integer x, Integer y) {
|
public void setLocation(Integer x, Integer y) {
|
||||||
this.locationX = x;
|
this.locationX = x;
|
||||||
this.locationY = y;
|
this.locationY = y;
|
||||||
this.dirty = true;
|
this.dirty = true;
|
||||||
|
notifyParent(LOCATION_CHANGED);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocationX(Integer x) {
|
||||||
|
this.locationX = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocationY(Integer y) {
|
||||||
|
this.locationY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBackgroundColor(Color backgroundColor) {
|
public void setBackgroundColor(Color backgroundColor) {
|
||||||
@@ -120,11 +141,11 @@ public class Visual {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void calculateInitialLocation() {
|
private void calculateInitialLocation() {
|
||||||
if(this.locationX == 0) {
|
if(this.locationX <= 0) {
|
||||||
this.locationX = 1;
|
this.locationX = 0;
|
||||||
}
|
}
|
||||||
if(this.locationY == 0){
|
if(this.locationY <= 0){
|
||||||
this.locationY = 1;
|
this.locationY = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,15 +206,9 @@ public class Visual {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addVisual(Visual child) {
|
public void addVisual(Visual child) {
|
||||||
this.addVisual(child, -1, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addVisual(Visual child, int x, int y) {
|
|
||||||
this.children.add(child);
|
this.children.add(child);
|
||||||
child.setParent(this);
|
child.setParent(this);
|
||||||
if(x == -1 && y == -1) {
|
child.calculateInitialLocation();
|
||||||
child.calculateInitialLocation();
|
|
||||||
}
|
|
||||||
child.calculateInitialSize();
|
child.calculateInitialSize();
|
||||||
|
|
||||||
if(this.active) {
|
if(this.active) {
|
||||||
@@ -220,7 +235,9 @@ public class Visual {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void notifyParent(int notify) {
|
public void notifyParent(int notify) {
|
||||||
this.parent.handleNotification(notify);
|
if(parent != null) {
|
||||||
|
this.parent.handleNotification(notify);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void repaint() {
|
private void repaint() {
|
||||||
@@ -410,6 +427,9 @@ public class Visual {
|
|||||||
Helper Methods
|
Helper Methods
|
||||||
---------------------------------------------------------------------*/
|
---------------------------------------------------------------------*/
|
||||||
private void initializeImageBuffer(){
|
private void initializeImageBuffer(){
|
||||||
|
if(this.width == 0 || this.height == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||||
clearImageBuffer();
|
clearImageBuffer();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,21 +2,29 @@ package guiTree;
|
|||||||
|
|
||||||
import guiTree.Components.TitleBar;
|
import guiTree.Components.TitleBar;
|
||||||
import guiTree.Helper.Point2d;
|
import guiTree.Helper.Point2d;
|
||||||
|
import guiTree.Listeners.Direction;
|
||||||
|
import guiTree.Listeners.ResizeListener;
|
||||||
import guiTree.events.MouseAdapter;
|
import guiTree.events.MouseAdapter;
|
||||||
|
import guiTree.Components.Panel;
|
||||||
|
|
||||||
import javax.tools.Tool;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.WindowListener;
|
import java.awt.event.WindowListener;
|
||||||
import java.awt.event.WindowStateListener;
|
import java.awt.event.WindowStateListener;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Window extends Visual {
|
public class Window extends Visual {
|
||||||
public CustomFrame frame;
|
public CustomFrame frame;
|
||||||
private TitleBar titleBar;
|
private TitleBar titleBar;
|
||||||
|
private Panel mainPanel;
|
||||||
|
private ResizeListener windowResizeListener;
|
||||||
|
private Point2d oldSize;
|
||||||
|
private Point2d oldLocation;
|
||||||
|
|
||||||
public Window()
|
public Window() {
|
||||||
{
|
|
||||||
super();
|
super();
|
||||||
this.frame = new CustomFrame(this);
|
this.frame = new CustomFrame(this);
|
||||||
this.setUndecorated(true);
|
this.setUndecorated(true);
|
||||||
@@ -24,16 +32,48 @@ public class Window extends Visual {
|
|||||||
this.setSize(getWidth(), getHeight());
|
this.setSize(getWidth(), getHeight());
|
||||||
revalidate();
|
revalidate();
|
||||||
});
|
});
|
||||||
|
Direction[] directions = {Direction.SOUTH, Direction.EAST, Direction.WEST};
|
||||||
|
windowResizeListener = new ResizeListener(directions, new Point2d(getWidth(), getHeight()), new Point2d(getLocationX(), getLocationY()));
|
||||||
|
this.addMouseListener(windowResizeListener);
|
||||||
|
this.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent mouseEvent) {
|
||||||
|
setSize(windowResizeListener.size.x, windowResizeListener.size.y);
|
||||||
|
setLocation(windowResizeListener.location.x, windowResizeListener.location.y);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void mouseMoved(MouseEvent mouseEvent) {
|
||||||
|
frame.setCursor(windowResizeListener.cursor);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent mouseEvent) {
|
||||||
|
frame.setCursor(windowResizeListener.cursor);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.mainPanel = new Panel();
|
||||||
|
|
||||||
|
super.addVisual(mainPanel);
|
||||||
|
|
||||||
|
BufferedImage icon = null;
|
||||||
|
try {
|
||||||
|
icon = ImageIO.read(new File("resources\\icons\\square_white.png"));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
TitleBar bar = new TitleBar("Working Title", icon);
|
||||||
|
bar.setBackgroundColor(Color.GRAY);
|
||||||
|
this.setTitleBar(bar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setSize(Integer width, Integer height)
|
public void setSize(Integer width, Integer height) {
|
||||||
{
|
|
||||||
this.frame.setSize(width, height);
|
this.frame.setSize(width, height);
|
||||||
super.setSize(width, height);
|
super.setSize(width, height);
|
||||||
if(this.titleBar != null) {
|
if(this.titleBar != null) {
|
||||||
this.titleBar.setSize(this.getWidth(), titleBar.getHeight());
|
this.titleBar.setSize(this.getWidth(), titleBar.getHeight());
|
||||||
}
|
}
|
||||||
|
windowResizeListener.setSize(width, height);
|
||||||
|
mainPanel.setSize(width, height);
|
||||||
revalidate();
|
revalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,12 +118,25 @@ public class Window extends Visual {
|
|||||||
titleBar.setSize(this.getWidth(), titleBar.getHeight());
|
titleBar.setSize(this.getWidth(), titleBar.getHeight());
|
||||||
|
|
||||||
if(this.getTitleBar() != null) {
|
if(this.getTitleBar() != null) {
|
||||||
this.removeVisual(this.titleBar);
|
mainPanel.removeVisual(this.titleBar);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.titleBar = titleBar;
|
this.titleBar = titleBar;
|
||||||
this.addVisual(titleBar, 0, 0);
|
mainPanel.addVisual(titleBar);
|
||||||
this.titleBar.addMouseListener(new TitleBarMouseListener());
|
this.titleBar.addMouseListener(new MouseAdapter() {
|
||||||
|
private int startX;
|
||||||
|
private int startY;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent mouseEvent) {
|
||||||
|
startX = mouseEvent.getX();
|
||||||
|
startY = mouseEvent.getY();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent mouseEvent) {
|
||||||
|
setLocation(mouseEvent.getXOnScreen() - startX, mouseEvent.getYOnScreen() - startY);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public TitleBar getTitleBar() {
|
public TitleBar getTitleBar() {
|
||||||
@@ -104,66 +157,57 @@ public class Window extends Visual {
|
|||||||
frame.setState(state);
|
frame.setState(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveTo(int x, int y) {
|
public int getState() {
|
||||||
|
return frame.getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocation(int x, int y) {
|
||||||
this.frame.setLocation(x, y);
|
this.frame.setLocation(x, y);
|
||||||
|
windowResizeListener.setLocation(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMainPanel(Panel panel) {
|
||||||
|
this.removeVisual(mainPanel);
|
||||||
|
this.addVisual(panel);
|
||||||
|
this.mainPanel = panel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Panel getMainPanel() {
|
||||||
|
return this.mainPanel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleNotification(int notify) {
|
public void handleNotification(int notify) {
|
||||||
switch(notify) {
|
switch(notify) {
|
||||||
case TitleBar.CLOSE:
|
case TitleBar.CLOSE: {
|
||||||
dispose();
|
dispose();
|
||||||
break;
|
break;
|
||||||
case TitleBar.MINIMIZE:
|
}
|
||||||
|
case TitleBar.MINIMIZE: {
|
||||||
setState(Frame.ICONIFIED);
|
setState(Frame.ICONIFIED);
|
||||||
break;
|
break;
|
||||||
case TitleBar.MAXIMIZE:
|
}
|
||||||
|
case TitleBar.MAXIMIZE: {
|
||||||
Rectangle screenBounds = frame.getGraphicsConfiguration().getBounds();
|
Rectangle screenBounds = frame.getGraphicsConfiguration().getBounds();
|
||||||
|
oldSize = new Point2d(getWidth(), getHeight());
|
||||||
|
oldLocation = new Point2d(frame.getX(), frame.getY());
|
||||||
this.setSize(screenBounds.width, screenBounds.height);
|
this.setSize(screenBounds.width, screenBounds.height);
|
||||||
this.moveTo(screenBounds.x, screenBounds.y);
|
this.setLocation(screenBounds.x, screenBounds.y);
|
||||||
setState(Frame.MAXIMIZED_BOTH);
|
setState(Frame.MAXIMIZED_BOTH);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
case TitleBar.NORMALIZE: {
|
||||||
|
Rectangle screenBounds = frame.getGraphicsConfiguration().getBounds();
|
||||||
private class WindowMouseListener extends MouseAdapter{
|
this.setSize(oldSize.x, oldSize.y);
|
||||||
private Boolean resizing;
|
this.setLocation(oldLocation.x, oldLocation.y);
|
||||||
private Boolean moving;
|
setState(Frame.NORMAL);
|
||||||
private Point2d initialLocation;
|
break;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mousePressed(MouseEvent mouseEvent) {
|
|
||||||
this.initialLocation = new Point2d(mouseEvent.getX(), mouseEvent.getY());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseReleased(MouseEvent mouseEvent) {
|
|
||||||
moving = false;
|
|
||||||
resizing = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private class TitleBarMouseListener extends MouseAdapter {
|
|
||||||
private Boolean moving = false;
|
|
||||||
private Point2d initialLocation;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseDragged(MouseEvent mouseEvent) {
|
|
||||||
if(moving) {
|
|
||||||
moveTo(mouseEvent.getXOnScreen() - initialLocation.x, mouseEvent.getYOnScreen() - initialLocation.y);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent mouseEvent) {
|
public void addVisual(Visual v) {
|
||||||
moving = true;
|
mainPanel.addVisual(v);
|
||||||
initialLocation = new Point2d(mouseEvent.getX(), mouseEvent.getY());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseReleased(MouseEvent mouseEvent) {
|
|
||||||
moving = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,7 +143,6 @@ public class XAMLParser {
|
|||||||
if(parentObject instanceof Visual && childObject instanceof Visual) {
|
if(parentObject instanceof Visual && childObject instanceof Visual) {
|
||||||
addVisual((Visual) parentObject, (Visual) childObject);
|
addVisual((Visual) parentObject, (Visual) childObject);
|
||||||
}
|
}
|
||||||
System.out.println("\nCurrent Element :" + childNode.getNodeName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return parentObject;
|
return parentObject;
|
||||||
|
|||||||
Reference in New Issue
Block a user