mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 13:40:04 +00:00
added relative size,
added frame resize
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
<Window Name="window" Visible="true" Size="640, 480">
|
||||
<!-- <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="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>
|
||||
</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"/>
|
||||
<!-- </Panel>-->
|
||||
</Window>
|
||||
@@ -10,6 +10,7 @@ public class Panel extends Visual {
|
||||
private Boolean overlapping;
|
||||
|
||||
public Panel() {
|
||||
super();
|
||||
overlapping = false;
|
||||
visuals = new ArrayList<>();
|
||||
}
|
||||
|
||||
@@ -6,14 +6,13 @@ import guiTree.events.MouseWheelGetter;
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.MouseInputListener;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class CustomFrame extends JFrame {
|
||||
private BufferedImage imageBuffer;
|
||||
private Window parentWindow;
|
||||
private final int resizeDelta = 5;
|
||||
|
||||
public CustomFrame(Window parent)
|
||||
{
|
||||
@@ -24,10 +23,13 @@ public class CustomFrame extends JFrame {
|
||||
{
|
||||
super(name);
|
||||
this.parentWindow = parent;
|
||||
this.addMouseMotionListener(new MouseEventGetter(parent));
|
||||
this.addMouseListener(new MouseEventGetter(parent));
|
||||
this.addMouseMotionListener(new MouseEventGetter(parent, resizeDelta));
|
||||
this.addMouseListener(new MouseEventGetter(parent, resizeDelta));
|
||||
this.addKeyListener(new KeyEventGetter(parent));
|
||||
this.addMouseWheelListener(new MouseWheelGetter(parent));
|
||||
MouseResizeListener listener = new MouseResizeListener();
|
||||
this.addMouseMotionListener(listener);
|
||||
this.addMouseListener(listener);
|
||||
}
|
||||
|
||||
public void setImageBuffer(BufferedImage imageBuffer) {
|
||||
@@ -39,4 +41,135 @@ public class CustomFrame extends JFrame {
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ import java.awt.event.MouseEvent;
|
||||
|
||||
public class MouseEventGetter implements MouseInputListener {
|
||||
private Window callingWindow;
|
||||
private int delta;
|
||||
|
||||
public MouseEventGetter(Window callingWindow) {
|
||||
public MouseEventGetter(Window callingWindow, int delta) {
|
||||
this.callingWindow = callingWindow;
|
||||
this.delta = delta;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -36,6 +36,8 @@ public class Visual {
|
||||
|
||||
private Integer width;
|
||||
private Integer height;
|
||||
private Float relativeWidth;
|
||||
private Float relativeHeight;
|
||||
private Integer locationX;
|
||||
private Integer locationY;
|
||||
private Color backgroundColor;
|
||||
@@ -53,7 +55,7 @@ public class Visual {
|
||||
---------------------------------------------------------------------*/
|
||||
|
||||
public Visual() {
|
||||
this(0, 0);
|
||||
this(1, 1);
|
||||
}
|
||||
|
||||
public Visual(int width, int height) {
|
||||
@@ -74,6 +76,8 @@ public class Visual {
|
||||
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.relativeWidth = -1.0f;
|
||||
this.relativeHeight = -1.0f;
|
||||
|
||||
this.locationX = 0;
|
||||
this.locationY = 0;
|
||||
@@ -87,20 +91,38 @@ public class Visual {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setSize(Integer width, Integer height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
private void setSize() {
|
||||
if(parent != null) {
|
||||
if(relativeWidth > 0.0) {
|
||||
width = Math.round(relativeWidth * parent.width);
|
||||
}
|
||||
if(relativeHeight > 0.0) {
|
||||
height = Math.round(relativeHeight * parent.height);
|
||||
}
|
||||
|
||||
}
|
||||
initializeImageBuffer();
|
||||
|
||||
for(Visual v: children) {
|
||||
if(v.relativeHeight > 0.0 || v.relativeWidth > 0.0) {
|
||||
v.setSize();
|
||||
}
|
||||
}
|
||||
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 setSize(Integer width, Integer height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
setSize();
|
||||
}
|
||||
|
||||
public void setSize(Float width, Float height) {
|
||||
relativeWidth = width;
|
||||
relativeHeight = height;
|
||||
setSize();
|
||||
}
|
||||
|
||||
public void setLocation(Integer x, Integer y) {
|
||||
this.locationX = x;
|
||||
@@ -131,15 +153,6 @@ public class Visual {
|
||||
this.fontColor = fontColor;
|
||||
}
|
||||
|
||||
private void calculateInitialSize() {
|
||||
if(this.width <= 0) {
|
||||
this.width = 1;
|
||||
}
|
||||
if(this.height <= 0){
|
||||
this.height = 1;
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateInitialLocation() {
|
||||
if(this.locationX <= 0) {
|
||||
this.locationX = 0;
|
||||
@@ -209,7 +222,7 @@ public class Visual {
|
||||
this.children.add(child);
|
||||
child.setParent(this);
|
||||
child.calculateInitialLocation();
|
||||
child.calculateInitialSize();
|
||||
child.setSize();
|
||||
|
||||
if(this.active) {
|
||||
child.activate();
|
||||
@@ -219,8 +232,6 @@ public class Visual {
|
||||
public void removeVisual(Visual child) {
|
||||
this.children.remove(child);
|
||||
child.setParent(null);
|
||||
child.setSize(0, 0);
|
||||
child.setLocation(0, 0);
|
||||
child.imageBuffer = null;
|
||||
child.deactivate();
|
||||
this.dirty = true;
|
||||
|
||||
Reference in New Issue
Block a user