mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 21:50:04 +00:00
added merge tu grid Panel
moved borders to external components added text field for one line with select
This commit is contained in:
48
src/guiTree/Components/Border.java
Normal file
48
src/guiTree/Components/Border.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Visual;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Border extends Visual {
|
||||
private int thickness;
|
||||
private Color color;
|
||||
|
||||
public Border() {
|
||||
this(1, Color.BLACK);
|
||||
}
|
||||
|
||||
public Border(int thickness) {
|
||||
this(thickness, Color.BLACK);
|
||||
}
|
||||
|
||||
public Border(Color color) {
|
||||
this(1, color);
|
||||
}
|
||||
|
||||
public Border(int thickness, Color color) {
|
||||
super();
|
||||
this.thickness = thickness;
|
||||
this.color = color;
|
||||
setSize(1.0f, 1.0f);
|
||||
}
|
||||
|
||||
public void setThickness(Integer thickness) {
|
||||
this.thickness = thickness;
|
||||
update();
|
||||
}
|
||||
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
update();
|
||||
}
|
||||
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
Graphics2D g = imageBuffer.createGraphics();
|
||||
|
||||
g.setColor(color);
|
||||
g.setStroke(new BasicStroke(thickness));
|
||||
g.drawRect(0, 0, getWidth(), getHeight());
|
||||
}
|
||||
}
|
||||
@@ -82,14 +82,7 @@ public class Button extends Visual {
|
||||
g.setColor(getPaintColor());
|
||||
|
||||
//Draw Button
|
||||
if(getHasBorder()) {
|
||||
g.fillRoundRect(1, 1, getWidth() - 1, getHeight() - 1, round, round);
|
||||
g.setColor(getBorderColor());
|
||||
g.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, round, round);
|
||||
}
|
||||
else {
|
||||
g.fillRoundRect(0, 0, getWidth(), getHeight(), round, round);
|
||||
}
|
||||
g.fillRoundRect(0, 0, getWidth(), getHeight(), round, round);
|
||||
|
||||
//Draw Label
|
||||
if(getFont() != null) {
|
||||
|
||||
@@ -128,8 +128,6 @@ public class CheckBox extends Visual {
|
||||
int textHeight = g.getFontMetrics().getHeight();
|
||||
g.drawString(text, getHeight() + 10, getHeight() / 2 + textHeight / 4);
|
||||
|
||||
g.setColor(getBorderColor());
|
||||
g.drawRect(0, 0, getHeight() - 1, getHeight() - 1);
|
||||
g.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,46 +6,27 @@ import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
import guiTree.Helper.Point2;
|
||||
|
||||
public class GridPanel extends Visual {
|
||||
private int columnCount;
|
||||
private int rowsCount;
|
||||
private Map<Integer, List<Visual>> childrenCols;
|
||||
private Map<Integer, List<Visual>> childrenRows;
|
||||
private Map<Integer, Integer> rowSizes;
|
||||
private Map<Integer, Integer> columnSizes;
|
||||
private Map<Visual, Point2<Integer>>children;
|
||||
private Map<Integer, Integer> fixedRows;
|
||||
private Map<Integer, Integer> fixedColumns;
|
||||
private List<Integer> rowSizes;
|
||||
private List<Integer> columnSizes;
|
||||
private Map<Point2<Integer>, Integer> rowPadding;
|
||||
private Map<Point2<Integer>, Integer> columnPadding;
|
||||
|
||||
public GridPanel(){
|
||||
super();
|
||||
childrenCols = new TreeMap<>();
|
||||
childrenRows = new TreeMap<>();
|
||||
rowsCount = 0;
|
||||
columnCount = 0;
|
||||
rowSizes = new HashMap<>();
|
||||
columnSizes = new HashMap<>();
|
||||
children = new HashMap<>();
|
||||
rowSizes = new ArrayList<>();
|
||||
columnSizes = new ArrayList<>();
|
||||
rowPadding = new HashMap<>();
|
||||
columnPadding = new HashMap<>();
|
||||
}
|
||||
|
||||
private void refresh() {
|
||||
rowsCount = 0;
|
||||
columnCount = 0;
|
||||
for(int i: childrenRows.keySet()) {
|
||||
if(childrenRows.get(i).size() != 0 && !rowSizes.containsKey(i)) {
|
||||
rowsCount++;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i: childrenCols.keySet()) {
|
||||
if(childrenCols.get(i).size() != 0 && !columnSizes.containsKey(i)) {
|
||||
columnCount++;
|
||||
}
|
||||
}
|
||||
|
||||
updateSize();
|
||||
fixedRows = new HashMap<>();
|
||||
fixedColumns = new HashMap<>();
|
||||
}
|
||||
|
||||
public void setSize() {
|
||||
@@ -53,71 +34,132 @@ public class GridPanel extends Visual {
|
||||
updateSize();
|
||||
}
|
||||
|
||||
public void updateSize() {
|
||||
if(rowsCount == 0 && columnCount == 0) {
|
||||
private void updateSize() {
|
||||
if(rowSizes.size() == 0 && columnSizes.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int setHeights = 0;
|
||||
int setWidths = 0;
|
||||
for(int i: rowSizes.keySet()) {
|
||||
setHeights += rowSizes.get(i);
|
||||
for(int i: fixedRows.keySet()) {
|
||||
rowSizes.set(i, fixedRows.get(i));
|
||||
setHeights += fixedRows.get(i);
|
||||
}
|
||||
for(int i: columnSizes.keySet()) {
|
||||
setWidths += columnSizes.get(i);
|
||||
for(int i: fixedColumns.keySet()) {
|
||||
columnSizes.set(i, fixedColumns.get(i));
|
||||
setWidths += fixedColumns.get(i);
|
||||
}
|
||||
|
||||
int height = (getHeight() - setHeights) / rowsCount;
|
||||
int width = (getWidth() - setWidths) / columnCount;
|
||||
int height = 0;
|
||||
int width = 0;
|
||||
if(fixedRows.size() != rowSizes.size()) {
|
||||
height = (getHeight() - setHeights) / (rowSizes.size() - fixedRows.size());
|
||||
}
|
||||
if(fixedColumns.size() != columnSizes.size()) {
|
||||
width = (getWidth() - setWidths) / (columnSizes.size() - fixedColumns.size());
|
||||
}
|
||||
|
||||
int locationY = 0;
|
||||
for(int i: childrenRows.keySet()) {
|
||||
int actualHeight = rowSizes.getOrDefault(i, height);
|
||||
for(Visual v: childrenRows.get(i)) {
|
||||
|
||||
v.setHeight(actualHeight);
|
||||
v.setLocationY(locationY);
|
||||
for(int i = 0; i < rowSizes.size(); i++) {
|
||||
if(!fixedRows.containsKey(i)) {
|
||||
rowSizes.set(i, height);
|
||||
}
|
||||
locationY += actualHeight;
|
||||
}
|
||||
|
||||
int locationX = 0;
|
||||
for(int i: childrenCols.keySet()) {
|
||||
int actualWidth = columnSizes.getOrDefault(i, width);
|
||||
for(Visual v: childrenCols.get(i)) {
|
||||
v.setWidth(actualWidth);
|
||||
v.setLocationX(locationX);
|
||||
for(int i = 0; i < columnSizes.size(); i++) {
|
||||
if(!fixedColumns.containsKey(i)) {
|
||||
columnSizes.set(i, width);
|
||||
}
|
||||
locationX += actualWidth;
|
||||
}
|
||||
|
||||
for(Visual v : children.keySet()) {
|
||||
Point2<Integer> cell = children.get(v);
|
||||
Point2<Integer> location = getGridLocation(cell);
|
||||
v.setLocation(location.x, location.y);
|
||||
int cellWidth = columnSizes.get(cell.x);
|
||||
int cellHeight = rowSizes.get(cell.y);
|
||||
int rowPad = rowPadding.getOrDefault(cell, 0);
|
||||
int colPad = columnPadding.getOrDefault(cell, 0);
|
||||
|
||||
for(int i = 0; i < colPad; i++) {
|
||||
cellWidth += columnSizes.get(cell.x + i + 1);
|
||||
}
|
||||
for(int i = 0; i < rowPad; i++) {
|
||||
cellHeight += rowSizes.get(cell.y + i + 1);
|
||||
}
|
||||
v.setSize(cellWidth, cellHeight);
|
||||
if(v.getWidth() != cellWidth) {
|
||||
v.setWidth(-1.0f);
|
||||
v.setWidth(cellWidth);
|
||||
}
|
||||
if(v.getHeight() != cellHeight) {
|
||||
v.setHeight(-1.0f);
|
||||
v.setHeight(cellHeight);
|
||||
}
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
public void setRowPadding(int row, int col, int padding) {
|
||||
rowPadding.put(new Point2<>(row, col), padding);
|
||||
rowPadding.put(new Point2<>(col, row), padding);
|
||||
for(int i = rowSizes.size(); i <= row + padding; i++) {
|
||||
rowSizes.add(0);
|
||||
}
|
||||
updateSize();
|
||||
}
|
||||
|
||||
public void setColumnPadding(int row, int col, int padding) {
|
||||
columnPadding.put(new Point2<>(row, col), padding);
|
||||
columnPadding.put(new Point2<>(col, row), padding);
|
||||
for(int i = columnSizes.size(); i <= col + padding; i++) {
|
||||
columnSizes.add(0);
|
||||
}
|
||||
updateSize();
|
||||
}
|
||||
|
||||
public void setRowSize(int row, int height) {
|
||||
rowSizes.put(row, height);
|
||||
refresh();
|
||||
fixedRows.put(row, height);
|
||||
updateSize();
|
||||
}
|
||||
|
||||
public void setColumnSize(int column, int width) {
|
||||
columnSizes.put(column, width);
|
||||
refresh();
|
||||
fixedColumns.put(column, width);
|
||||
updateSize();
|
||||
}
|
||||
|
||||
private Point2<Integer> getGridLocation(Point2<Integer> grid) {
|
||||
int locationX = 0;
|
||||
int locationY = 0;
|
||||
for(int i = 0; i < grid.x; i++) {
|
||||
locationX += columnSizes.get(i);
|
||||
}
|
||||
for(int i = 0; i < grid.y; i++) {
|
||||
locationY += rowSizes.get(i);
|
||||
}
|
||||
return new Point2<>(locationX, locationY);
|
||||
}
|
||||
|
||||
public void addVisual(Visual v) {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
if(v.getAttribute("row") != null) {
|
||||
x = Integer.parseInt(v.getAttribute("row"));
|
||||
}
|
||||
if(v.getAttribute("column") != null) {
|
||||
y = Integer.parseInt(v.getAttribute("column"));
|
||||
}
|
||||
this.addVisual(v, x, y);
|
||||
}
|
||||
|
||||
public void addVisual(Visual v, int row, int col) {
|
||||
super.addVisual(v);
|
||||
childrenCols.computeIfAbsent(col, k -> new ArrayList<>());
|
||||
childrenRows.computeIfAbsent(row, k -> new ArrayList<>());
|
||||
childrenCols.get(col).add(v);
|
||||
childrenRows.get(row).add(v);
|
||||
children.put(v, new Point2<>(col, row));
|
||||
v.setLocation(-1.0f, -1.0f);
|
||||
refresh();
|
||||
for(int i = rowSizes.size(); i <= row; i++) {
|
||||
rowSizes.add(0);
|
||||
}
|
||||
for(int i = columnSizes.size(); i <= col; i++) {
|
||||
columnSizes.add(0);
|
||||
}
|
||||
updateSize();
|
||||
}
|
||||
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
|
||||
@@ -115,11 +115,6 @@ public class Panel extends Visual {
|
||||
g.setColor(getBackgroundColor());
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
if(getHasBorder()) {
|
||||
g.setColor(getBorderColor());
|
||||
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
|
||||
}
|
||||
|
||||
g.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,14 +141,8 @@ public class ScrollPanel extends Visual {
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
Graphics2D g = imageBuffer.createGraphics();
|
||||
g.setColor(getPaintColor());
|
||||
if(getHasBorder()) {
|
||||
g.fillRect(1, 1, getWidth() - 1, getHeight() - 1);
|
||||
g.setColor(getBorderColor());
|
||||
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
|
||||
}
|
||||
else {
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
}
|
||||
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
g.dispose();
|
||||
}
|
||||
|
||||
@@ -36,8 +36,6 @@ public class Slider extends Visual {
|
||||
setBackgroundColor(new Color(175, 175, 175));
|
||||
setForegroundColor(new Color(112, 112, 112));
|
||||
setAccentColor(new Color(50, 50, 50));
|
||||
button1.setHasBorder(false);
|
||||
button2.setHasBorder(false);
|
||||
|
||||
if(direction == Direction.Horizontal) {
|
||||
button1.setIcon("arrow_left_black");
|
||||
@@ -192,14 +190,6 @@ public class Slider extends Visual {
|
||||
slider.setForegroundColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBorderColor(Color color) {
|
||||
super.setBorderColor(color);
|
||||
button1.setBorderColor(color);
|
||||
button2.setBorderColor(color);
|
||||
slider.setBorderColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
super.setName(name);
|
||||
@@ -230,10 +220,6 @@ public class Slider extends Visual {
|
||||
Graphics2D g = imageBuffer.createGraphics();
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
if(getHasBorder()) {
|
||||
g.setColor(getBorderColor());
|
||||
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
|
||||
}
|
||||
g.setColor(getBackgroundColor());
|
||||
|
||||
if(direction == Direction.Vertical) {
|
||||
|
||||
185
src/guiTree/Components/Text.java
Normal file
185
src/guiTree/Components/Text.java
Normal file
@@ -0,0 +1,185 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Visual;
|
||||
import guiTree.events.MouseAdapter;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Text extends Visual {
|
||||
private String text;
|
||||
private boolean selectable;
|
||||
private int startIndex;
|
||||
private int endIndex;
|
||||
private int textWidth;
|
||||
private int textHeight;
|
||||
private boolean inside;
|
||||
private int[] characterWidthMap;
|
||||
private FontMetrics fontMetrics;
|
||||
private int textX;
|
||||
private int textY;
|
||||
|
||||
public Text() {
|
||||
this(0, 0, "");
|
||||
}
|
||||
|
||||
public Text(int x, int y, String text) {
|
||||
super(x, y);
|
||||
this.text = text;
|
||||
inside = false;
|
||||
characterWidthMap = new int[text.length()];
|
||||
startIndex = -1;
|
||||
endIndex = -1;
|
||||
textX = 0;
|
||||
textY = 0;
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
int x = mouseEvent.getX();
|
||||
int y = mouseEvent.getY();
|
||||
if(isOverText(x, y)) {
|
||||
startIndex = getCharAt(x);
|
||||
System.out.println("Start Index: " + startIndex);
|
||||
return;
|
||||
}
|
||||
startIndex = -1;
|
||||
endIndex = -1;
|
||||
update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent mouseEvent) {
|
||||
int x = mouseEvent.getX();
|
||||
int y = mouseEvent.getY();
|
||||
|
||||
if(startIndex != -1) {
|
||||
endIndex = getCharAt(x);
|
||||
System.out.println("End index: " + endIndex);
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent mouseEvent) {
|
||||
if(inside) {
|
||||
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent mouseEvent) {
|
||||
int x = mouseEvent.getX();
|
||||
int y = mouseEvent.getY();
|
||||
if(isOverText(x, y)) {
|
||||
if(!inside) {
|
||||
setCursor(new Cursor(Cursor.TEXT_CURSOR));
|
||||
inside = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(inside) {
|
||||
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
||||
inside = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
characterWidthMap = new int[text.length()];
|
||||
}
|
||||
|
||||
public void setSelectable(Boolean selectable) {
|
||||
this.selectable = selectable;
|
||||
}
|
||||
|
||||
private int getCharAt(int x) {
|
||||
int location = (getWidth() - textWidth)/2;
|
||||
for(int i = 0; i < text.length(); i++) {
|
||||
if(x < location + characterWidthMap[i] / 2) {
|
||||
return i;
|
||||
}
|
||||
location += characterWidthMap[i];
|
||||
}
|
||||
return text.length()-1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFont(Font font) {
|
||||
super.setFont(font);
|
||||
fontMetrics = null;
|
||||
}
|
||||
|
||||
private boolean isOverText(int x, int y) {
|
||||
if(x < textX || x > textX + textWidth) {
|
||||
return false;
|
||||
}
|
||||
if(y > textY || y < textY - textHeight) {
|
||||
return false;
|
||||
}
|
||||
System.out.println("Text X: " + textX + " Text Y: " + textY + " x: " + x + " y: " + y + " textWidth: " + textWidth + " textHeight: " + textHeight);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setCharacterWidthMap() {
|
||||
for(int i = 0; i < text.length(); i++) {
|
||||
int charWidth = fontMetrics.charWidth(text.charAt(i));
|
||||
characterWidthMap[i] = charWidth;
|
||||
}
|
||||
textWidth = fontMetrics.stringWidth(text);
|
||||
textHeight = fontMetrics.getHeight();
|
||||
textX = (getWidth() - textWidth)/2;
|
||||
textY = (getHeight() + textHeight)/2;
|
||||
}
|
||||
|
||||
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
Graphics2D g = imageBuffer.createGraphics();
|
||||
|
||||
if(getFont() != null) {
|
||||
g.setFont(getFont());
|
||||
}
|
||||
if(fontMetrics == null) {
|
||||
fontMetrics = g.getFontMetrics();
|
||||
setCharacterWidthMap();
|
||||
}
|
||||
|
||||
if(startIndex != -1 && endIndex != -1) {
|
||||
int startX;
|
||||
int endX;
|
||||
if(startIndex > endIndex) {
|
||||
endX = startIndex;
|
||||
startX = endIndex;
|
||||
}
|
||||
else {
|
||||
startX = startIndex;
|
||||
endX = endIndex;
|
||||
}
|
||||
int highlightStartX = textX;
|
||||
int highlightEndX = textX;
|
||||
|
||||
for(int i = 0; i <= endX; i++) {
|
||||
if(i < startX) {
|
||||
highlightStartX += characterWidthMap[i];
|
||||
}
|
||||
highlightEndX += characterWidthMap[i];
|
||||
}
|
||||
|
||||
g.setColor(Color.BLUE);
|
||||
g.fillRect(highlightStartX, textY - textHeight + 3, highlightEndX - highlightStartX, textHeight);
|
||||
}
|
||||
|
||||
g.setColor(getFontColor());
|
||||
|
||||
g.drawString(text, textX, textY);
|
||||
|
||||
g.dispose();
|
||||
}
|
||||
}
|
||||
@@ -82,14 +82,7 @@ public class ToggleButton extends Visual {
|
||||
g.setColor(getPaintColor());
|
||||
|
||||
//Draw Button
|
||||
if(getHasBorder()) {
|
||||
g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);
|
||||
g.setColor(getBorderColor());
|
||||
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
|
||||
}
|
||||
else {
|
||||
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
|
||||
}
|
||||
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
|
||||
|
||||
//Draw Label
|
||||
if(getFont() != null) {
|
||||
|
||||
Reference in New Issue
Block a user