most of mouse listeners, added title bar and title bar listeners with maximize and drag reposition

This commit is contained in:
rmaco
2020-03-16 23:00:24 +02:00
parent 35fa03b35c
commit c9a5a55618
19 changed files with 586 additions and 260 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 259 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 264 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 B

View File

Before

Width:  |  Height:  |  Size: 85 B

After

Width:  |  Height:  |  Size: 85 B

View File

Before

Width:  |  Height:  |  Size: 88 B

After

Width:  |  Height:  |  Size: 88 B

View File

Before

Width:  |  Height:  |  Size: 128 B

After

Width:  |  Height:  |  Size: 128 B

View File

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 131 B

View File

@@ -1,9 +1,10 @@
<?xml version="1.0"?>
<Window Name="window" Visible="true" Size="640, 480">
<Button Name="button1" BackgroundColor="#990000" Size="200, 200" Label="button1">
<Button Name="button4" BackgroundColor="#009999" Size="100, 100" Label="button4">
<Button Name="button2" BackgroundColor="#000099" 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 Name="button3" BackgroundColor="#009900" Location="300, 300" Label="button3"/>
<Button Name="button6" Location="350, 350" Size="100, 100"/>
<Button Name="button3" BackgroundColor="#009900" Size="100, 100" Location="300, 300" Icon="close_black"/>
</Window>

View File

@@ -1,9 +1,15 @@
import guiTree.Button;
import guiTree.Components.Button;
import guiTree.Components.TitleBar;
import guiTree.Window;
import parser.XAMLParser;
import javax.imageio.ImageIO;
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 static void main(String[] args) {
@@ -12,36 +18,42 @@ public class Main {
assert window != null;
window.revalidate();
Button button = (Button)window.findByName("button1");
window.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
window.dispose();
}
});
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);
window.setTitleBar(bar);
window.revalidate();
long now;
long prev = 0;
while(true) {
now = System.currentTimeMillis();
if(now - prev >= 1000) {
int x = button.getLocationX();
int y = button.getLocationY();
if(x + button.getWidth() >= window.getWidth()) {
x = 0;
if(y + button.getHeight() >= window.getHeight()) {
y = 0;
}
else {
y += 30;
}
}
else {
x += 30;
}
button.setLocation(x, y);
prev = now;
window.revalidate();
}
}
// while(true) {
// now = System.currentTimeMillis();
// if(now - prev >= 1000) {
// int x = button.getLocationX();
// int y = button.getLocationY();
// if(x + button.getWidth() >= window.getWidth()) {
// x = 0;
// if(y + button.getHeight() >= window.getHeight()) {
// y = 0;
// }
// else {
// y += 30;
// }
// }
// else {
// x += 30;
// }
// button.setLocation(x, y);
// prev = now;
// window.revalidate();
// }
// }
}catch (Exception e){
e.printStackTrace();
}

View File

@@ -1,73 +0,0 @@
package guiTree;
import guiTree.events.MouseAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
public class Button extends Visual {
private String label;
private Boolean pressed;
public Button() {
this("");
}
public Button(String label) {
super();
this.label = label;
pressed = false;
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
pressed = true;
revalidate();
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
pressed = false;
revalidate();
}
});
}
@Override
public void paint(BufferedImage imageBuffer)
{
//Get Graphics
Graphics2D g = imageBuffer.createGraphics();
//Set Transparency
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, getWidth(), getHeight());
g.setComposite(AlphaComposite.Src);
//Choose background
if(pressed) {
g.setColor(Color.GRAY);
}
else {
g.setColor(this.getBackgroundColor());
}
//Draw Button
g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 50, 50);
g.setColor(this.getForegroundColor());
//Draw Label
int textWidth = g.getFontMetrics().stringWidth(label);
int textHeight = g.getFontMetrics().getHeight();
g.drawString(this.label, this.getWidth()/2 - textWidth/2, this.getHeight()/2 + textHeight/2);
g.dispose();
}
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return this.label;
}
}

View File

@@ -0,0 +1,146 @@
package guiTree.Components;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Button extends Visual {
private String label;
private Boolean pressed;
private Boolean hovered;
private BufferedImage icon;
public Button() {
this("", null);
}
public Button(String label) {
this(label, null);
}
public Button(BufferedImage icon) {
this(null, icon);
}
public Button(String label, BufferedImage icon) {
super();
this.label = label;
this.icon = icon;
pressed = false;
hovered = false;
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
System.out.println("Pressed: " + getName());
pressed = true;
revalidate();
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
System.out.println("Released: " + getName());
pressed = false;
revalidate();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
System.out.println("Entered: " + getName());
hovered = true;
revalidate();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
System.out.println("Exited: " + getName());
hovered = false;
revalidate();
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
System.out.println("DRAGGING: " + getName());
revalidate();
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
System.out.println("Moved: " + getName());
revalidate();
}
});
}
@Override
public void paint(BufferedImage imageBuffer)
{
//Get Graphics
Graphics2D g = imageBuffer.createGraphics();
//Set Transparency
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, getWidth(), getHeight());
g.setComposite(AlphaComposite.Src);
//Choose background
if(hovered || pressed) {
g.setColor(this.getForegroundColor());
}
else {
g.setColor(this.getBackgroundColor());
}
//Draw Button
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(this.getFontColor());
//Draw Label
int textWidth = 0;
int textHeight = 0;
if(!label.equals("")) {
textWidth = g.getFontMetrics().stringWidth(label);
textHeight = g.getFontMetrics().getHeight();
}
g.drawString(this.label, (this.getWidth() - textWidth)/2, (this.getHeight() + textHeight)/2);
//Draw Icon
if(icon != null) {
int iconWidth = icon.getWidth();
int iconHeight = icon.getHeight();
int iconX = (this.getWidth() - iconWidth - textWidth) / 2;
int iconY = (this.getHeight() - iconHeight - textHeight) / 2;
Graphics2D g2 = imageBuffer.createGraphics();
g2.drawImage(icon, iconX, iconY, null);
g2.dispose();
}
g.dispose();
}
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return this.label;
}
public void setIcon(BufferedImage icon) {
this.icon = icon;
}
public void setIcon(String url) {
try{
icon = ImageIO.read(new File("resources\\icons\\" + url + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getIcon() {
return icon;
}
}

View File

@@ -0,0 +1,148 @@
package guiTree.Components;
import guiTree.Helper.Point2d;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
public class TitleBar extends Visual {
public static final int MINIMIZE = 1;
public static final int MAXIMIZE = 2;
public static final int CLOSE = 3;
private BufferedImage icon;
private String title;
private Button minimize;
private Button maximize;
private Button close;
/*--------------------------------------------------------------------
Constructors
---------------------------------------------------------------------*/
public TitleBar() {
this(null, null);
}
public TitleBar(String title) {
this(title, null);
}
public TitleBar(BufferedImage icon) {
this(null, icon);
}
public TitleBar(String title, BufferedImage icon) {
super();
this.icon = icon;
this.title = title;
this.close = new Button();
this.minimize = new Button();
this.maximize = new Button();
close.setIcon("close_white");
minimize.setIcon("minimize_white");
maximize.setIcon("square_white");
close.setSize(60, 30);
minimize.setSize(60, 30);
maximize.setSize(60, 30);
close.setBackgroundColor(Color.GRAY);
maximize.setBackgroundColor(Color.GRAY);
minimize.setBackgroundColor(Color.GRAY);
close.setForegroundColor(Color.RED);
maximize.setForegroundColor(Color.LIGHT_GRAY);
minimize.setForegroundColor(Color.LIGHT_GRAY);
this.setSize(1, 30);
this.setLocation(0, 0);
setButtonLocation();
close.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(3);
}
});
maximize.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(2);
}
});
minimize.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(1);
}
});
}
/*--------------------------------------------------------------------
Getters
---------------------------------------------------------------------*/
public BufferedImage getIcon() {
return icon;
}
public String geTitle() {
return title;
}
/*--------------------------------------------------------------------
Setters
---------------------------------------------------------------------*/
public void setIcon(BufferedImage icon) {
this.icon = icon;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public void setSize(Integer width, Integer height) {
super.setSize(width, height);
setButtonLocation();
}
@Override
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
g.setColor(getBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight());
Graphics2D iconGraphics = imageBuffer.createGraphics();
iconGraphics.drawImage(icon, 5, (getHeight() - icon.getHeight())/2, null);
iconGraphics.dispose();
int stringOffset = icon.getWidth() + 10;
int textHeight = 0;
if(!title.equals("")) {
textHeight = g.getFontMetrics().getHeight();
g.setColor(Color.WHITE);
}
g.drawString(title, stringOffset, getHeight()/2 + textHeight/4);
}
private void setButtonLocation() {
int buttonOffset = this.getWidth() - close.getWidth();
close.setLocation(buttonOffset, 0);
close.setName("close");
buttonOffset -= maximize.getWidth();
maximize.setLocation(buttonOffset, 0);
maximize.setName("maximize");
buttonOffset -= minimize.getWidth();
minimize.setLocation(buttonOffset, 0);
minimize.setName("minimize");
this.addVisual(close);
this.addVisual(minimize);
this.addVisual(maximize);
}
}

View File

@@ -37,6 +37,6 @@ public class CustomFrame extends JFrame {
@Override
public void paint(Graphics g)
{
g.drawImage(imageBuffer, 5, 0, this.getWidth(), this.getHeight(), null);
g.drawImage(imageBuffer, 0, 0, this.getWidth(), this.getHeight(), null);
}
}

View File

@@ -0,0 +1,11 @@
package guiTree.Helper;
public class Point2d {
public int x;
public int y;
public Point2d(int x,int y) {
this.x = x;
this.y = y;
}
}

View File

@@ -12,41 +12,36 @@ public class MouseEventGetter implements MouseInputListener {
@Override
public void mouseDragged(MouseEvent mouseEvent) {
callingWindow.mouseDragged(mouseEvent, 0, 0);
callingWindow.mouseDragged(mouseEvent);
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
callingWindow.mouseMoved(mouseEvent, 0, 0);
System.out.println("MOVED");
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
callingWindow.mouseClicked(mouseEvent, 0, 0);
System.out.println("CLICKED");
callingWindow.mouseClicked(mouseEvent);
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
callingWindow.mousePressed(mouseEvent, 0, 0);
System.out.println("PRESSED");
callingWindow.mousePressed(mouseEvent);
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
callingWindow.mouseReleased(mouseEvent, 0, 0);
callingWindow.mouseReleased(mouseEvent);
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
callingWindow.mouseEntered(mouseEvent, 0, 0);
System.out.println("ENTERED");
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
callingWindow.mouseExited(mouseEvent, 0, 0);
System.out.println("EXITED");
callingWindow.mouseExited(mouseEvent);
}
}

View File

@@ -16,6 +16,7 @@ public class Visual {
/*--------------------------------------------------------------------
Tree Elements
---------------------------------------------------------------------*/
private List<Visual> children;
private Visual parent;
private BufferedImage imageBuffer;
@@ -27,33 +28,50 @@ public class Visual {
/*--------------------------------------------------------------------
Attributes
---------------------------------------------------------------------*/
private Integer width;
private Integer height;
private Integer locationX;
private Integer locationY;
private Color backgroundColor;
private Color foregroundColor;
private Color fontColor;
private Boolean active;
private Boolean dirty;
private static Visual entered;
private Boolean focused;
private Boolean pressed;
/*--------------------------------------------------------------------
Constructors
---------------------------------------------------------------------*/
public Visual() {
this(0, 0);
}
public Visual(int width, int height) {
this.children = new ArrayList<>();
this.mouseWheelListeners = new ArrayList<>();
this.mouseListeners = new ArrayList<>();
this.keyListeners = new ArrayList<>();
this.parent = null;
this.backgroundColor = Color.WHITE;
this.foregroundColor = Color.BLACK;
this.foregroundColor = Color.BLUE;
this.fontColor = Color.BLACK;
this.dirty = true;
this.active = this instanceof Window;
}
this.focused = false;
this.pressed = false;
this.width = width;
this.height = height;
this.locationX = 0;
this.locationY = 0;
}
/*--------------------------------------------------------------------
Attributes Setters
@@ -63,7 +81,7 @@ public class Visual {
this.name = name;
}
public void setSize(Integer width, Integer height){
public void setSize(Integer width, Integer height) {
this.width = width;
this.height = height;
@@ -72,7 +90,7 @@ public class Visual {
this.dirty = true;
}
public void setLocation(Integer x, Integer y){
public void setLocation(Integer x, Integer y) {
this.locationX = x;
this.locationY = y;
this.dirty = true;
@@ -88,21 +106,25 @@ public class Visual {
this.dirty = true;
}
public void setFontColor(Color fontColor) {
this.fontColor = fontColor;
}
private void calculateInitialSize() {
if(this.width == null) {
this.width = 20;
if(this.width <= 0) {
this.width = 1;
}
if(this.height == null){
this.height = 20;
if(this.height <= 0){
this.height = 1;
}
}
private void calculateInitialLocation(){
if(this.locationX == null) {
this.locationX = 20;
private void calculateInitialLocation() {
if(this.locationX == 0) {
this.locationX = 1;
}
if(this.locationY == null){
this.locationY = 50;
if(this.locationY == 0){
this.locationY = 1;
}
}
@@ -114,21 +136,19 @@ public class Visual {
return name;
}
public int getWidth()
{
public int getWidth() {
return this.width;
}
public int getHeight()
{
public int getHeight() {
return this.height;
}
public int getLocationX(){
public int getLocationX() {
return this.locationX;
}
public int getLocationY(){
public int getLocationY() {
return this.locationY;
}
@@ -140,11 +160,15 @@ public class Visual {
return foregroundColor;
}
public Color getFontColor() {
return fontColor;
}
/*--------------------------------------------------------------------
Tree Methods
---------------------------------------------------------------------*/
public Visual findByName(String name){
public Visual findByName(String name) {
if(this.name.equals(name)){
return this;
}
@@ -161,9 +185,15 @@ public class Visual {
}
public void addVisual(Visual child) {
this.addVisual(child, -1, -1);
}
public void addVisual(Visual child, int x, int y) {
this.children.add(child);
child.setParent(this);
child.calculateInitialLocation();
if(x == -1 && y == -1) {
child.calculateInitialLocation();
}
child.calculateInitialSize();
if(this.active) {
@@ -181,18 +211,16 @@ public class Visual {
this.dirty = true;
}
private void setParent(Visual parent)
{
private void setParent(Visual parent) {
this.parent = parent;
}
private void handleNotification() {
public void handleNotification(int notify) {
}
private void notifyParent()
{
this.parent.handleNotification();
public void notifyParent(int notify) {
this.parent.handleNotification(notify);
}
private void repaint() {
@@ -234,94 +262,58 @@ public class Visual {
/*--------------------------------------------------------------------
Listener Methods
---------------------------------------------------------------------*/
void addMouseListener(MouseListener mouseListener) {
public void addMouseListener(MouseListener mouseListener) {
this.mouseListeners.add(mouseListener);
}
void removeMouseListener(MouseListener mouseListener) {
public void removeMouseListener(MouseListener mouseListener) {
this.mouseListeners.remove(mouseListener);
}
void addMouseWheelListener(MouseWheelListener mouseWheelListener) {
public void addMouseWheelListener(MouseWheelListener mouseWheelListener) {
this.mouseWheelListeners.add(mouseWheelListener);
}
void removeMouseWheelListener(MouseWheelListener mouseWheelListener) {
public void removeMouseWheelListener(MouseWheelListener mouseWheelListener) {
this.mouseWheelListeners.remove(mouseWheelListener);
}
void addKeyListener(KeyListener keyListener) {
public void addKeyListener(KeyListener keyListener) {
this.keyListeners.add(keyListener);
}
void removeKeyListener(KeyListener keyListener) {
public void removeKeyListener(KeyListener keyListener) {
this.keyListeners.remove(keyListener);
}
void mouseClicked(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseClicked(mouseEvent, v.locationX + offsetX, v.locationY + offsetY);
}
}
if(front) {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseClicked(mouseEvent);
}
dirty = true;
void mouseClicked(MouseEvent mouseEvent) {
for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mouseClicked(mouseEvent);
}
dirty = true;
entered.focused = true;
}
void mouseReleased(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseReleased(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
}
if(front) {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseReleased(mouseEvent);
}
dirty = true;
void mouseReleased(MouseEvent mouseEvent) {
for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mouseReleased(mouseEvent);
}
dirty = true;
entered.pressed = false;
}
void mousePressed(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mousePressed(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
}
if(front) {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mousePressed(mouseEvent);
}
dirty = true;
void mousePressed(MouseEvent mouseEvent) {
for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mousePressed(mouseEvent);
}
dirty = true;
entered.pressed = true;
}
void mouseEntered(MouseEvent mouseEvent, int offsetX, int offsetY) {
if(entered != null && entered.pressed){
return;
}
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
@@ -335,6 +327,7 @@ public class Visual {
}
}
if(front) {
entered = this;
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseEntered(mouseEvent);
}
@@ -342,87 +335,74 @@ public class Visual {
}
}
void mouseExited(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseExited(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
void mouseExited(MouseEvent mouseEvent) {
if(entered == null) {
return;
}
if(front) {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseExited(mouseEvent);
}
dirty = true;
if(entered.pressed) {
return;
}
for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseExited(mouseEvent);
}
entered = null;
dirty = true;
}
void mouseDragged(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseDragged(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
}
if(front) {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseDragged(mouseEvent);
}
dirty = true;
void mouseDragged(MouseEvent mouseEvent) {
for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseDragged(mouseEvent);
}
entered.dirty = true;
}
void mouseMoved(MouseEvent mouseEvent, int offsetX, int offsetY) {
if(entered != null && entered.pressed){
return;
}
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
if(entered != null) {
if (!entered.isInside(mouseEvent.getX(), mouseEvent.getY())) {
for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseExited(mouseEvent);
}
entered = this;
}
}
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
if(v.isInside(mouseX, mouseY)) {
front = false;
v.mouseMoved(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
}
if(front) {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseMoved(mouseEvent);
if(this.isInside(mouseEvent.getX(), mouseEvent.getY())) {
if (this != entered && entered != null) {
for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseExited(mouseEvent);
}
entered = this;
for (MouseListener mouseListener : mouseListeners) {
mouseListener.mouseEntered(mouseEvent);
}
}
else {
for (MouseListener mouseListener : mouseListeners) {
mouseListener.mouseMoved(mouseEvent);
}
}
}
dirty = true;
}
}
void mouseWheelMoved(MouseWheelEvent mouseWheelEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseWheelEvent.getX() - offsetX;
int mouseY = mouseWheelEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseWheelMoved(mouseWheelEvent, offsetX + v.locationX, offsetY + v.locationY);
}
}
if(front) {
if(focused) {
for(MouseWheelListener mouseWheelListener: mouseWheelListeners) {
mouseWheelListener.mouseWheelMoved(mouseWheelEvent);
}
dirty = true;
}
}
@@ -457,4 +437,8 @@ public class Visual {
child.deactivate();
}
}
private boolean isInside(int x, int y) {
return x > locationX && x < locationX + width && y > locationY && y < locationY + height;
}
}

View File

@@ -1,17 +1,25 @@
package guiTree;
import guiTree.Components.TitleBar;
import guiTree.Helper.Point2d;
import guiTree.events.MouseAdapter;
import javax.tools.Tool;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
public class Window extends Visual {
public CustomFrame frame;
private TitleBar titleBar;
public Window()
{
super();
this.frame = new CustomFrame(this);
this.setUndecorated(true);
this.addWindowStateListener(e -> {
this.setSize(getWidth(), getHeight());
revalidate();
@@ -23,6 +31,10 @@ public class Window extends Visual {
{
this.frame.setSize(width, height);
super.setSize(width, height);
if(this.titleBar != null) {
this.titleBar.setSize(this.getWidth(), titleBar.getHeight());
}
revalidate();
}
public void setFrameImageBuffer(BufferedImage imageBuffer){
@@ -62,6 +74,22 @@ public class Window extends Visual {
frame.repaint(tm);
}
public void setTitleBar(TitleBar titleBar) {
titleBar.setSize(this.getWidth(), titleBar.getHeight());
if(this.getTitleBar() != null) {
this.removeVisual(this.titleBar);
}
this.titleBar = titleBar;
this.addVisual(titleBar, 0, 0);
this.titleBar.addMouseListener(new TitleBarMouseListener());
}
public TitleBar getTitleBar() {
return this.titleBar;
}
public void setPositionRelativeTo(Component c){
frame.setLocationRelativeTo(c);
}
@@ -71,4 +99,71 @@ public class Window extends Visual {
}
public void setUndecorated(Boolean undecorated){frame.setUndecorated(undecorated);}
public void setState(int state) {
frame.setState(state);
}
private void moveTo(int x, int y) {
this.frame.setLocation(x, y);
}
@Override
public void handleNotification(int notify) {
switch(notify) {
case TitleBar.CLOSE:
dispose();
break;
case TitleBar.MINIMIZE:
setState(Frame.ICONIFIED);
break;
case TitleBar.MAXIMIZE:
Rectangle screenBounds = frame.getGraphicsConfiguration().getBounds();
this.setSize(screenBounds.width, screenBounds.height);
this.moveTo(screenBounds.x, screenBounds.y);
setState(Frame.MAXIMIZED_BOTH);
break;
}
}
private class WindowMouseListener extends MouseAdapter{
private Boolean resizing;
private Boolean moving;
private Point2d initialLocation;
@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
public void mousePressed(MouseEvent mouseEvent) {
moving = true;
initialLocation = new Point2d(mouseEvent.getX(), mouseEvent.getY());
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
moving = false;
}
}
}

View File

@@ -14,7 +14,8 @@ import java.util.ArrayList;
import java.util.List;
public class XAMLParser {
private final static String packageName = "guiTree.";
private final static String packageGuiTree = "guiTree.";
private final static String packageComponents = "guiTree.Components.";
private static Converter valueConverter = new Converter();
private static void setAttributes(Object object, NamedNodeMap attributeList){
@@ -119,7 +120,13 @@ public class XAMLParser {
}
private static Object parseNode(Node parentNode)throws Exception{
Class<?> parentClass = Class.forName(packageName.concat(parentNode.getNodeName()));
Class<?> parentClass;
try {
parentClass = Class.forName(packageComponents.concat(parentNode.getNodeName()));
}
catch (ClassNotFoundException e) {
parentClass = Class.forName(packageGuiTree.concat(parentNode.getNodeName()));
}
Object parentObject = parentClass.getDeclaredConstructor().newInstance();
setAttributes(parentObject, parentNode.getAttributes());