done panel with overlapping

This commit is contained in:
rmaco
2020-03-19 22:02:43 +02:00
parent c9a5a55618
commit 9fbfb6b3fb
12 changed files with 483 additions and 112 deletions

View File

@@ -37,36 +37,30 @@ public class Button extends Visual {
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();
}
});

View 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();
// }
}
}

View File

@@ -1,6 +1,5 @@
package guiTree.Components;
import guiTree.Helper.Point2d;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
@@ -9,9 +8,10 @@ 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;
public static final int CLOSE = 10;
public static final int MINIMIZE = 11;
public static final int NORMALIZE = 12;
public static final int MAXIMIZE = 13;
private BufferedImage icon;
private String title;
@@ -60,22 +60,39 @@ public class TitleBar extends Visual {
this.setLocation(0, 0);
setButtonLocation();
this.addVisual(close);
this.addVisual(minimize);
this.addVisual(maximize);
minimize.setName("minimize");
maximize.setName("maximize");
close.setName("close");
close.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(3);
public void mouseReleased(MouseEvent mouseEvent) {
notifyParent(CLOSE);
}
});
maximize.addMouseListener(new MouseAdapter() {
private boolean isMaximized = false;
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(2);
public void mouseReleased(MouseEvent mouseEvent) {
if(isMaximized) {
notifyParent(NORMALIZE);
maximize.setIcon("square_white");
isMaximized = false;
}
else {
maximize.setIcon("normalize_white");
notifyParent(MAXIMIZE);
isMaximized = true;
}
}
});
minimize.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(1);
public void mouseReleased(MouseEvent mouseEvent) {
notifyParent(MINIMIZE);
}
});
}
@@ -134,15 +151,9 @@ public class TitleBar extends Visual {
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);
}
}