removed recursive call in paint

added scroll panes without relative position
This commit is contained in:
Macocian Adrian Radu
2020-03-31 20:36:08 +03:00
parent 62f3d8d46c
commit eb4ce47a5c
17 changed files with 306 additions and 204 deletions

View File

@@ -1,5 +1,6 @@
package guiTree.Components;
import guiTree.Animations.ColorAnimation;
import guiTree.Helper.Debugger;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
@@ -13,8 +14,6 @@ import java.io.IOException;
public class Button extends Visual {
private String label;
private Boolean pressed;
private Boolean hovered;
private BufferedImage icon;
private int round = -1;
@@ -34,36 +33,33 @@ public class Button extends Visual {
super();
this.label = label;
this.icon = icon;
pressed = false;
hovered = false;
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
pressed = false;
update();
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
pressed = true;
addAnimation(new ColorAnimation(Button.this, getAccentColor(), getForegroundColor(), 100));
update();
Debugger.log("Pressed: " + getName(), Debugger.Tag.LISTENER);
Debugger.log("Calling repaint from pressed: " + getName(), Debugger.Tag.PAINTING);
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
pressed = false;
addAnimation(new ColorAnimation(Button.this, getForegroundColor(), getAccentColor(), 100));
update();
Debugger.log("Calling repaint from released: " + getName(), Debugger.Tag.PAINTING);
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
hovered = true;
addAnimation(new ColorAnimation(Button.this, getBackgroundColor(), getAccentColor(), 100));
update();
Debugger.log("Calling repaint from entered: " + getName(), Debugger.Tag.PAINTING);
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
hovered = false;
addAnimation(new ColorAnimation(Button.this, getAccentColor(), getBackgroundColor(), 100));
update();
Debugger.log("Calling repaint from exited: " + getName(), Debugger.Tag.PAINTING);
}
@@ -83,17 +79,7 @@ public class Button extends Visual {
//Get Graphics
Graphics2D g = imageBuffer.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//Choose background
if(hovered) {
g.setColor(this.getAccentColor());
}
else {
g.setColor(this.getBackgroundColor());
}
if(pressed) {
g.setColor(this.getForegroundColor());
}
g.setColor(getPaintColor());
//Draw Button
if(getHasBorder()) {

View File

@@ -1,5 +1,6 @@
package guiTree.Components;
import guiTree.Animations.ColorAnimation;
import guiTree.Helper.Debugger;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
@@ -37,19 +38,29 @@ public class CheckBox extends Visual {
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
if(!marked) {
addAnimation(new ColorAnimation(CheckBox.this, getAccentColor(), getForegroundColor(), 100));
}
else {
addAnimation(new ColorAnimation(CheckBox.this, getForegroundColor(), getAccentColor(), 100));
}
marked = !marked;
Debugger.log("Calling repaint from pressed: " + getName(), Debugger.Tag.PAINTING);
update();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
hovered = true;
if(!marked) {
addAnimation(new ColorAnimation(CheckBox.this, getBackgroundColor(), getAccentColor(), 100));
}
Debugger.log("Calling repaint from entered: " + getName(), Debugger.Tag.PAINTING);
update();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
hovered = false;
if(!marked) {
addAnimation(new ColorAnimation(CheckBox.this, getAccentColor(), getBackgroundColor(), 100));
}
Debugger.log("Calling repaint from exited: " + getName(), Debugger.Tag.PAINTING);
update();
}
@@ -92,19 +103,11 @@ public class CheckBox extends Visual {
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
//Set Transparency
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, getWidth(), getHeight());
g.setComposite(AlphaComposite.Src);
g.setColor(getPaintColor());
if(hovered && !marked) {
g.setColor(getAccentColor());
g.fillRect(0, 0, getHeight() - 1, getHeight() - 1);
}
g.fillRect(1, 1, getHeight() - 1, getHeight() - 1);
if(marked) {
g.setColor(getForegroundColor());
g.fillRect(1, 1, getHeight() - 2, getHeight() - 2);
if(icon != null) {
int iconWidth = icon.getWidth();

View File

@@ -1,13 +1,20 @@
package guiTree.Components;
import guiTree.Helper.Debugger;
import guiTree.Animations.LocationAnimation;
import guiTree.Helper.Point2;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
public class ScrollPanel extends Visual {
private List<VisualLocation> children;
private float positionX;
private float positionY;
@@ -19,48 +26,187 @@ public class ScrollPanel extends Visual {
public ScrollPanel() {
super();
setName("ScrollPanel");
horizontalScrollBar = new Slider(Slider.Direction.Horizontal);
verticalScrollBar = new Slider(Slider.Direction.Vertical);
verticalScrollBar.setHasBorder(true);
horizontalScrollBar.setHasBorder(true);
addVisual(verticalScrollBar);
addVisual(horizontalScrollBar);
verticalScrollBar.setName("vertical scroll bar");
horizontalScrollBar.setName("horizontal scroll bar");
children = new ArrayList<>();
addMouseListener(new BarListener());
addMouseWheelListener(new MouseWheelListener());
}
@Override
public void setSize() {
super.setSize();
if(verticalScrollBar != null && horizontalScrollBar != null) {
verticalScrollBar.setSize(20, getHeight() - 20);
horizontalScrollBar.setSize(getWidth() - 20, 20);
return;
if (getFarthestX() > getWidth()) {
if (horizontalScrollBar == null) {
horizontalScrollBar = new Slider(Slider.Direction.Horizontal);
horizontalScrollBar.setName(getName() + " horizontal scroll bar");
super.addVisual(horizontalScrollBar);
horizontalScrollBar.setWidth(1.0f);
horizontalScrollBar.setHeight(20);
}
horizontalScrollBar.setSliderSize((float)getWidth() / getFarthestX());
horizontalScrollBar.setLocation(0, getHeight());
}
if(horizontalScrollBar != null) {
horizontalScrollBar.setSize(getWidth(), 20);
if (getFarthestY() > getHeight()) {
if (verticalScrollBar == null) {
verticalScrollBar = new Slider(Slider.Direction.Vertical);
verticalScrollBar.setName(getName() + " vertical scroll bar");
super.addVisual(verticalScrollBar);
verticalScrollBar.setHeight(1.0f);
verticalScrollBar.setWidth(20);
}
verticalScrollBar.setSliderSize((float)getHeight() / getFarthestY());
verticalScrollBar.setLocation(getWidth(), 0);
}
}
@Override
public void addVisual(Visual v) {
if(v.getWidth() + v.getLocationX() > getFarthestX()) {
if (v.getWidth() + v.getLocationX() > getWidth()) {
if (horizontalScrollBar == null) {
horizontalScrollBar = new Slider(Slider.Direction.Horizontal);
horizontalScrollBar.setName(getName() + " horizontal scroll bar");
horizontalScrollBar.setWidth(1.0f);
horizontalScrollBar.setHeight(20);
super.addVisual(horizontalScrollBar);
}
horizontalScrollBar.setSliderSize((float)getWidth() / v.getWidth() + v.getLocationX());
}
}
if(v.getHeight() + v.getLocationY() > getFarthestY()) {
if (v.getHeight() + v.getLocationY() > getHeight()) {
if (verticalScrollBar == null) {
verticalScrollBar = new Slider(Slider.Direction.Vertical);
verticalScrollBar.setName(getName() + " vertical scroll bar");
verticalScrollBar.setHeight(1.0f);
verticalScrollBar.setWidth(20);
super.addVisual(verticalScrollBar);
}
verticalScrollBar.setSliderSize((float)getHeight() / v.getHeight() + v.getLocationY());
}
}
if(verticalScrollBar != null) {
verticalScrollBar.setSize(20, getHeight());
super.addVisual(v);
children.add(new VisualLocation(v));
}
private int getFarthestX() {
int max = 0;
for(VisualLocation visualLocation: children) {
if(max < visualLocation.v.getWidth() + visualLocation.originalLocation.x) {
max = visualLocation.v.getWidth() + visualLocation.originalLocation.x;
}
}
return max;
}
private int getFarthestY() {
int max = 0;
for(VisualLocation visualLocation: children) {
if(max < visualLocation.v.getHeight() + visualLocation.originalLocation.y) {
max = visualLocation.v.getHeight() + visualLocation.originalLocation.y;
}
}
return max;
}
private void setLocations() {
for(VisualLocation visualLocation:children) {
visualLocation.v.setLocation(visualLocation.originalLocation.x - Math.round(horizontalScrollBar.getSliderLocation() * (getFarthestX() - getWidth() + 20)),
visualLocation.originalLocation.y - Math.round(verticalScrollBar.getSliderLocation() * (getFarthestY() - getHeight() + 20)));
System.out.println("Moved: " + visualLocation.v + " from x: " + visualLocation.originalLocation + " to " + visualLocation.v.getLocation());
}
}
@Override
public void handleNotification(int notify) {
if(notify == Slider.SLIDER_MOVED) {
setLocations();
}
}
@Override
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
g.setColor(getPaintColor());
if(getHasBorder()) {
g.setColor(getBackgroundColor());
g.fillRect(1, 1, getWidth() - 2, getHeight() - 2);
g.fillRect(1, 1, getWidth() - 1, getHeight() - 1);
g.setColor(getBorderColor());
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
else {
g.setColor(getBackgroundColor());
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
g.fillRect(0, 0, getWidth(), getHeight());
}
g.dispose();
}
private class BarListener extends MouseAdapter {
private boolean outHorizontal = false;
private boolean outVertical = false;
private LocationAnimation outAnimationHorizontal;
private LocationAnimation outAnimationVertical;
private LocationAnimation inAnimationHorizontal;
private LocationAnimation inAnimationVertical;
@Override
public void mouseMoved(MouseEvent mouseEvent) {
if(verticalScrollBar != null) {
if (mouseEvent.getX() > getWidth() - verticalScrollBar.getWidth() && mouseEvent.getY() > verticalScrollBar.getLocationY() && mouseEvent.getY() < verticalScrollBar.getHeight()) {
if (!outVertical) {
outAnimationVertical = new LocationAnimation(verticalScrollBar, verticalScrollBar.getLocation(), new Point2<>(getWidth() - verticalScrollBar.getWidth(), verticalScrollBar.getLocationY()), 300);
removeAnimation(inAnimationVertical);
addAnimation(outAnimationVertical);
outVertical = true;
}
} else {
if (outVertical) {
inAnimationVertical = new LocationAnimation(verticalScrollBar, verticalScrollBar.getLocation(), new Point2<>(getWidth(), verticalScrollBar.getLocationY()), 300);
removeAnimation(outAnimationVertical);
addAnimation(inAnimationVertical);
outVertical = false;
}
}
}
if(horizontalScrollBar != null) {
if (mouseEvent.getY() > getHeight() - horizontalScrollBar.getHeight() && mouseEvent.getX() > horizontalScrollBar.getLocationX() && mouseEvent.getX() < horizontalScrollBar.getWidth()) {
if (!outHorizontal) {
outAnimationHorizontal = new LocationAnimation(horizontalScrollBar, horizontalScrollBar.getLocation(), new Point2<>(horizontalScrollBar.getLocationX(), getHeight() - horizontalScrollBar.getHeight()), 300);
removeAnimation(inAnimationHorizontal);
addAnimation(outAnimationHorizontal);
outHorizontal = true;
}
} else {
if (outHorizontal) {
inAnimationHorizontal = new LocationAnimation(horizontalScrollBar, horizontalScrollBar.getLocation(), new Point2<>(horizontalScrollBar.getLocationX(), getHeight()), 300);
removeAnimation(outAnimationHorizontal);
addAnimation(inAnimationHorizontal);
outHorizontal = false;
}
}
}
}
}
private class MouseWheelListener extends MouseAdapter {
@Override
public void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) {
if(mouseWheelEvent.isShiftDown()) {
horizontalScrollBar.moveSlider(mouseWheelEvent.getWheelRotation() * 10);
return;
}
verticalScrollBar.moveSlider(mouseWheelEvent.getWheelRotation() * 10);
}
}
private static class VisualLocation {
Visual v;
Point2<Integer> originalLocation;
public VisualLocation(Visual v) {
this.v = v;
originalLocation = v.getLocation();
}
}
}

View File

@@ -8,6 +8,8 @@ import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
public class Slider extends Visual {
public static final int SLIDER_MOVED = 3;
public enum Direction {
Vertical,
Horizontal
@@ -84,19 +86,33 @@ public class Slider extends Visual {
}
public float getSliderLocation() {
int availableSpace;
if(direction == Direction.Vertical) {
return (float)slider.getLocationY() / getHeight();
}
else {
return (float)slider.getLocationX() / getWidth();
availableSpace = getHeight() - button1.getHeight() - slider.getHeight() - button2.getHeight();
int sliderY = slider.getLocationY() - button1.getHeight();
return (float)sliderY / availableSpace;
}
availableSpace = getWidth() - button1.getWidth() - slider.getWidth() - button2.getWidth();
int sliderX = slider.getLocationX() - button1.getWidth();
return (float)sliderX / availableSpace;
}
public void setSliderSize(int width, int height) {
slider.setSize(width, height);
public float getSliderSize() {
if(direction == Direction.Vertical) {
return (float)slider.getHeight() / getHeight();
}
return (float)slider.getWidth() / getWidth();
}
private void moveSlider(int offset) {
public void setSliderSize(Float size) {
if(direction == Direction.Vertical) {
slider.setHeight(Math.round(size * getHeight()));
return;
}
slider.setWidth(Math.round(size * getWidth()));
}
public void moveSlider(Integer offset) {
if(direction == Direction.Vertical) {
if(slider.getLocationY() + slider.getHeight() + offset > getHeight() - button2.getHeight()) {
slider.setLocationY(getHeight() - slider.getHeight() - button2.getHeight());
@@ -119,6 +135,7 @@ public class Slider extends Visual {
slider.setLocationX(Math.round(slider.getLocationX() + offset));
}
}
notifyParent(SLIDER_MOVED);
}
public void setDirection(Direction direction) {
@@ -143,7 +160,7 @@ public class Slider extends Visual {
}
}
private void moveSlider(float offset) {
public void moveSlider(Float offset) {
if(direction == Direction.Vertical) {
moveSlider(Math.round(offset * getHeight()));
return;
@@ -198,13 +215,13 @@ public class Slider extends Visual {
button1.setSize(Math.round(0.7f * getWidth()), Math.round(0.7f * getWidth()));
button2.setSize(button1.getWidth(), button1.getHeight());
button2.setLocation(0, getHeight() - button2.getHeight());
slider.setSize(getWidth() / 2, 40);
slider.setWidth(getWidth() / 2);
}
else {
button1.setSize(Math.round(0.7f * getHeight()), Math.round(0.7f * getHeight()));
button2.setSize(button1.getWidth(), button1.getHeight());
button2.setLocation(getWidth() - button2.getWidth(), 0);
slider.setSize(40, getHeight() / 2);
slider.setHeight(getHeight() / 2);
}
}
@@ -218,6 +235,7 @@ public class Slider extends Visual {
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
g.setColor(getBackgroundColor());
if(direction == Direction.Vertical) {
int x1 = Math.round(0.15f * getWidth());
int y1 = button1.getHeight();

View File

@@ -1,5 +1,6 @@
package guiTree.Components;
import guiTree.Animations.ColorAnimation;
import guiTree.Helper.Debugger;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
@@ -38,20 +39,31 @@ public class ToggleButton extends Visual {
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
if(pressed) {
addAnimation(new ColorAnimation(ToggleButton.this, getForegroundColor(), getAccentColor(), 100));
}
else {
addAnimation(new ColorAnimation(ToggleButton.this, getAccentColor(), getForegroundColor(), 100));
}
pressed = !pressed;
Debugger.log("Pressed: " + getName(), Debugger.Tag.LISTENER);
Debugger.log("Calling repaint from pressed: " + getName(), Debugger.Tag.PAINTING);
update();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
hovered = true;
if(!pressed) {
addAnimation(new ColorAnimation(ToggleButton.this, getBackgroundColor(), getAccentColor(), 100));
}
Debugger.log("Calling repaint from entered: " + getName(), Debugger.Tag.PAINTING);
update();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
hovered = false;
if(!pressed) {
addAnimation(new ColorAnimation(ToggleButton.this, getAccentColor(), getBackgroundColor(), 100));
}
Debugger.log("Calling repaint from exited: " + getName(), Debugger.Tag.PAINTING);
update();
}
@@ -67,22 +79,7 @@ public class ToggleButton extends Visual {
{
//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) {
g.setColor(this.getAccentColor());
}
else {
g.setColor(this.getBackgroundColor());
}
if(pressed) {
g.setColor(this.getForegroundColor());
}
g.setColor(getPaintColor());
//Draw Button
if(getHasBorder()) {