added relative size,

added frame resize
This commit is contained in:
rmaco
2020-03-20 19:52:28 +02:00
parent f77e0b8c8a
commit 35c7a7721b
5 changed files with 175 additions and 28 deletions

View File

@@ -2,11 +2,11 @@
<Window Name="window" Visible="true" Size="640, 480"> <Window Name="window" Visible="true" Size="640, 480">
<!-- <Panel Name="MainPanel" BackgroundColor="#FFFFFF" Size="1.0, 1.0" Overlapping="false">--> <!-- <Panel Name="MainPanel" BackgroundColor="#FFFFFF" Size="1.0, 1.0" Overlapping="false">-->
<Button Name="button1" BackgroundColor="#990000" Size="0.5, 0.5" Label="button1"> <Button Name="button1" BackgroundColor="#990000" Size="0.5, 0.5" Label="button1">
<Button Name="button4" BackgroundColor="#009999" Size="100, 100" Icon="square_white"> <Button Name="button4" BackgroundColor="#009999" Size="0.3, 0.3" Icon="square_white">
<Button Name="button2" BackgroundColor="#000099" Size="50, 50" Label="button2"/> <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" BackgroundColor="#555555" Location="320, 0" Size="0.5, 0.5"/>
<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>--> <!-- </Panel>-->
</Window> </Window>

View File

@@ -10,6 +10,7 @@ public class Panel extends Visual {
private Boolean overlapping; private Boolean overlapping;
public Panel() { public Panel() {
super();
overlapping = false; overlapping = false;
visuals = new ArrayList<>(); visuals = new ArrayList<>();
} }

View File

@@ -6,14 +6,13 @@ import guiTree.events.MouseWheelGetter;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.MouseInputListener; import javax.swing.event.MouseInputListener;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent; import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
public class CustomFrame extends JFrame { public class CustomFrame extends JFrame {
private BufferedImage imageBuffer; private BufferedImage imageBuffer;
private Window parentWindow; private Window parentWindow;
private final int resizeDelta = 5;
public CustomFrame(Window parent) public CustomFrame(Window parent)
{ {
@@ -24,10 +23,13 @@ public class CustomFrame extends JFrame {
{ {
super(name); super(name);
this.parentWindow = parent; this.parentWindow = parent;
this.addMouseMotionListener(new MouseEventGetter(parent)); this.addMouseMotionListener(new MouseEventGetter(parent, resizeDelta));
this.addMouseListener(new MouseEventGetter(parent)); this.addMouseListener(new MouseEventGetter(parent, resizeDelta));
this.addKeyListener(new KeyEventGetter(parent)); this.addKeyListener(new KeyEventGetter(parent));
this.addMouseWheelListener(new MouseWheelGetter(parent)); this.addMouseWheelListener(new MouseWheelGetter(parent));
MouseResizeListener listener = new MouseResizeListener();
this.addMouseMotionListener(listener);
this.addMouseListener(listener);
} }
public void setImageBuffer(BufferedImage imageBuffer) { public void setImageBuffer(BufferedImage imageBuffer) {
@@ -39,4 +41,135 @@ public class CustomFrame extends JFrame {
{ {
g.drawImage(imageBuffer, 0, 0, this.getWidth(), this.getHeight(), null); g.drawImage(imageBuffer, 0, 0, this.getWidth(), this.getHeight(), null);
} }
private class MouseResizeListener implements MouseMotionListener, MouseListener {
private int startX;
private int startY;
private boolean resizing = false;
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
this.startX = e.getXOnScreen();
this.startY = e.getYOnScreen();
}
@Override
public void mouseReleased(MouseEvent e) {
resizing = false;
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
}
@Override
public void mouseDragged(MouseEvent e) {
if(getCursor().getType() != Cursor.DEFAULT_CURSOR && !this.resizing){
this.resizing = true;
this.resize(e);
}
}
@Override
public void mouseMoved(MouseEvent e) {
if(e.getX() < resizeDelta || e.getX() > getWidth() - resizeDelta ||
e.getY() < resizeDelta || e.getY() > getHeight() - resizeDelta) {
this.setResizeCursor(e);
}
else{
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
private void setResizeCursor(MouseEvent e){
if(e.getX() <= resizeDelta){
if(e.getY() <= resizeDelta){
setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
}
else if(e.getY() >= getHeight() - resizeDelta){
setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
}
else{
setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
}
}
else if(e.getX() >= getWidth() - resizeDelta){
if(e.getY() <= resizeDelta){
setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
}
else if(e.getY() >= getHeight() - resizeDelta){
setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
}
else{
setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
}
}
else{
if(e.getY() <= resizeDelta){
setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
}
else if(e.getY() >= getHeight() - resizeDelta){
setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
}
}
}
private void resize(MouseEvent e){
switch (getCursor().getType()){
case Cursor.N_RESIZE_CURSOR:
parentWindow.setSize(getWidth(), getHeight() + startY - e.getYOnScreen());
parentWindow.setLocation(getX(), e.getYOnScreen());
startY = e.getYOnScreen();
break;
case Cursor.NE_RESIZE_CURSOR:
parentWindow.setSize(getWidth() + e.getXOnScreen() - startX, getHeight() + startY - e.getYOnScreen());
parentWindow.setLocation(getX(), e.getYOnScreen());
startX = e.getXOnScreen();
startY = e.getYOnScreen();
break;
case Cursor.E_RESIZE_CURSOR:
parentWindow.setSize(getWidth() + e.getXOnScreen() - startX, getHeight());
startX = e.getXOnScreen();
break;
case Cursor.SE_RESIZE_CURSOR:
parentWindow.setSize(getWidth() + e.getXOnScreen() - startX, getHeight() + e.getYOnScreen() - startY);
startX = e.getXOnScreen();
startY = e.getYOnScreen();
break;
case Cursor.S_RESIZE_CURSOR:
parentWindow.setSize(getWidth(), getHeight() + e.getYOnScreen() - startY);
startY = e.getYOnScreen();
break;
case Cursor.SW_RESIZE_CURSOR:
parentWindow.setSize(getWidth() + startX - e.getXOnScreen(), getHeight() + e.getYOnScreen() - startY);
parentWindow.setLocation(e.getXOnScreen(), getY());
startX = e.getXOnScreen();
startY = e.getYOnScreen();
break;
case Cursor.W_RESIZE_CURSOR:
parentWindow.setSize(getWidth() + startX - e.getXOnScreen(), getHeight());
parentWindow.setLocation(e.getXOnScreen(), getY());
startX = e.getXOnScreen();
break;
case Cursor.NW_RESIZE_CURSOR:
parentWindow.setSize(getWidth() + startX - e.getXOnScreen(), getHeight() + startY - e.getYOnScreen());
parentWindow.setLocation(e.getXOnScreen(), e.getYOnScreen());
startX = e.getXOnScreen();
startY = e.getYOnScreen();
break;
}
this.resizing = false;
}
}
} }

View File

@@ -5,9 +5,11 @@ import java.awt.event.MouseEvent;
public class MouseEventGetter implements MouseInputListener { public class MouseEventGetter implements MouseInputListener {
private Window callingWindow; private Window callingWindow;
private int delta;
public MouseEventGetter(Window callingWindow) { public MouseEventGetter(Window callingWindow, int delta) {
this.callingWindow = callingWindow; this.callingWindow = callingWindow;
this.delta = delta;
} }
@Override @Override

View File

@@ -36,6 +36,8 @@ public class Visual {
private Integer width; private Integer width;
private Integer height; private Integer height;
private Float relativeWidth;
private Float relativeHeight;
private Integer locationX; private Integer locationX;
private Integer locationY; private Integer locationY;
private Color backgroundColor; private Color backgroundColor;
@@ -53,7 +55,7 @@ public class Visual {
---------------------------------------------------------------------*/ ---------------------------------------------------------------------*/
public Visual() { public Visual() {
this(0, 0); this(1, 1);
} }
public Visual(int width, int height) { public Visual(int width, int height) {
@@ -74,6 +76,8 @@ public class Visual {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.relativeWidth = -1.0f;
this.relativeHeight = -1.0f;
this.locationX = 0; this.locationX = 0;
this.locationY = 0; this.locationY = 0;
@@ -87,20 +91,38 @@ public class Visual {
this.name = name; this.name = name;
} }
public void setSize(Integer width, Integer height) { private void setSize() {
this.width = width; if(parent != null) {
this.height = height; if(relativeWidth > 0.0) {
width = Math.round(relativeWidth * parent.width);
}
if(relativeHeight > 0.0) {
height = Math.round(relativeHeight * parent.height);
}
}
initializeImageBuffer(); initializeImageBuffer();
for(Visual v: children) {
if(v.relativeHeight > 0.0 || v.relativeWidth > 0.0) {
v.setSize();
}
}
this.dirty = true; this.dirty = true;
this.notifyParent(SIZE_CHANGED); this.notifyParent(SIZE_CHANGED);
} }
// public void setSize(Float width, Float height) { public void setSize(Integer width, Integer height) {
// this.width = Math.round(this.parent.width * width); this.width = width;
// this.height = Math.round(this.parent.height * height); this.height = height;
// } setSize();
}
public void setSize(Float width, Float height) {
relativeWidth = width;
relativeHeight = height;
setSize();
}
public void setLocation(Integer x, Integer y) { public void setLocation(Integer x, Integer y) {
this.locationX = x; this.locationX = x;
@@ -131,15 +153,6 @@ public class Visual {
this.fontColor = fontColor; this.fontColor = fontColor;
} }
private void calculateInitialSize() {
if(this.width <= 0) {
this.width = 1;
}
if(this.height <= 0){
this.height = 1;
}
}
private void calculateInitialLocation() { private void calculateInitialLocation() {
if(this.locationX <= 0) { if(this.locationX <= 0) {
this.locationX = 0; this.locationX = 0;
@@ -209,7 +222,7 @@ public class Visual {
this.children.add(child); this.children.add(child);
child.setParent(this); child.setParent(this);
child.calculateInitialLocation(); child.calculateInitialLocation();
child.calculateInitialSize(); child.setSize();
if(this.active) { if(this.active) {
child.activate(); child.activate();
@@ -219,8 +232,6 @@ public class Visual {
public void removeVisual(Visual child) { public void removeVisual(Visual child) {
this.children.remove(child); this.children.remove(child);
child.setParent(null); child.setParent(null);
child.setSize(0, 0);
child.setLocation(0, 0);
child.imageBuffer = null; child.imageBuffer = null;
child.deactivate(); child.deactivate();
this.dirty = true; this.dirty = true;