mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 13:40:04 +00:00
made text aligners
created image support fixed find By Name in dropdowns
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import guiTree.Components.Button;
|
||||
import guiTree.Components.DropDown;
|
||||
import guiTree.Components.Text;
|
||||
import guiTree.Window;
|
||||
import guiTree.events.MouseAdapter;
|
||||
import parser.XAMLParser;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Window window = null;
|
||||
@@ -9,5 +15,35 @@ public class Main {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
assert window != null;
|
||||
Button alignToLeft = (Button) window.findByName("align_to_left");
|
||||
Button alignToRight = (Button) window.findByName("align_to_right");
|
||||
Button alignToCenter = (Button) window.findByName("align_to_center");
|
||||
Text textField = (Text) window.findByName("Text");
|
||||
|
||||
alignToLeft.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
textField.setAlignment("left");
|
||||
textField.update();
|
||||
}
|
||||
});
|
||||
|
||||
alignToCenter.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
textField.setAlignment("center");
|
||||
textField.update();
|
||||
}
|
||||
});
|
||||
|
||||
alignToRight.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
textField.setAlignment("right");
|
||||
textField.update();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
81
src/guiTree/Components/Decoarations/CenterTextAligner.java
Normal file
81
src/guiTree/Components/Decoarations/CenterTextAligner.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package guiTree.Components.Decoarations;
|
||||
|
||||
import guiTree.Helper.Point2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CenterTextAligner implements TextAligner{
|
||||
private int width;
|
||||
private int height;
|
||||
private FontMetrics fontMetrics;
|
||||
private int spacing;
|
||||
private int textHeight;
|
||||
private List<String> wholeText;
|
||||
|
||||
@Override
|
||||
public Point2<Integer> alignLine(int line) {
|
||||
int x = (width - fontMetrics.stringWidth(wholeText.get(line))) / 2;
|
||||
int y = (line + 1) * textHeight;
|
||||
y += spacing * line;
|
||||
return new Point2<>(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point2<Integer> getCaretPosition(int x, int y) {
|
||||
y -= wholeText.size() * spacing;
|
||||
y /= fontMetrics.getHeight();
|
||||
if(y > wholeText.size() - 1) {
|
||||
return new Point2<>(wholeText.get(wholeText.size() - 1).length(), wholeText.size() - 1);
|
||||
}
|
||||
if(y < 0) {
|
||||
return new Point2<>(0, 0);
|
||||
}
|
||||
|
||||
String currentLine = wholeText.get(y);
|
||||
x -= (width - fontMetrics.stringWidth(currentLine)) / 2;
|
||||
for(int i = 0; i < currentLine.length(); i++) {
|
||||
if(x < (fontMetrics.charWidth(currentLine.charAt(i))) / 2) {
|
||||
return new Point2<>(i, y);
|
||||
}
|
||||
x -= fontMetrics.charWidth(currentLine.charAt(i));
|
||||
}
|
||||
return new Point2<>(0, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point2<Integer> getPositionOnScreen(int x, int y) {
|
||||
String currentLine = wholeText.get(y);
|
||||
y = (fontMetrics.getHeight() + spacing) * y;
|
||||
int textStart = (width - fontMetrics.stringWidth(currentLine)) / 2;
|
||||
int width = textStart;
|
||||
width += fontMetrics.stringWidth(currentLine.substring(0, x));
|
||||
return new Point2<>(width, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWholeText(List<StringBuilder> wholeText) {
|
||||
this.wholeText = new ArrayList<>();
|
||||
for(StringBuilder line: wholeText) {
|
||||
this.wholeText.add(line.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpacing(int spacing) {
|
||||
this.spacing = spacing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFontMetrics(FontMetrics fontMetrics) {
|
||||
this.fontMetrics = fontMetrics;
|
||||
this.textHeight = fontMetrics.getHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(int width, int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public class LeftTextAligner implements TextAligner{
|
||||
private int textHeight;
|
||||
|
||||
@Override
|
||||
public Point2<Integer> alignLine(String text, int line) {
|
||||
public Point2<Integer> alignLine(int line) {
|
||||
int x = 0;
|
||||
int y = (line + 1) * textHeight;
|
||||
y += spacing * line;
|
||||
@@ -25,14 +25,8 @@ public class LeftTextAligner implements TextAligner{
|
||||
public Point2<Integer> getPositionOnScreen(int x, int y){
|
||||
String currentLine = wholeText.get(y);
|
||||
y = (fontMetrics.getHeight() + spacing) * y;
|
||||
int width = 0;
|
||||
for(int i = 1; i <= currentLine.length(); i++) {
|
||||
width += fontMetrics.charWidth(currentLine.charAt(i - 1));
|
||||
if(x == i) {
|
||||
return new Point2<>(width, y);
|
||||
}
|
||||
}
|
||||
return new Point2<>(0, y);
|
||||
int width = fontMetrics.stringWidth(currentLine.substring(0, x));
|
||||
return new Point2<>(width, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,7 +14,8 @@ public class RightTextAligner implements TextAligner{
|
||||
private int width;
|
||||
|
||||
@Override
|
||||
public Point2<Integer> alignLine(String text, int line) {
|
||||
public Point2<Integer> alignLine(int line) {
|
||||
String text = wholeText.get(line);
|
||||
int x = width - fontMetrics.stringWidth(text);
|
||||
int y = (line + 1) * textHeight;
|
||||
y += spacing * line;
|
||||
@@ -47,14 +48,9 @@ public class RightTextAligner implements TextAligner{
|
||||
public Point2<Integer> getPositionOnScreen(int x, int y) {
|
||||
String currentLine = wholeText.get(y);
|
||||
y = (fontMetrics.getHeight() + spacing) * y;
|
||||
int width = this.width;
|
||||
for(int i = currentLine.length(); i > 0; i--) {
|
||||
if(x == i) {
|
||||
return new Point2<>(width, y);
|
||||
}
|
||||
width -= fontMetrics.charWidth(currentLine.charAt(i - 1));
|
||||
}
|
||||
return new Point2<>(this.width - fontMetrics.stringWidth(currentLine), y);
|
||||
int width = this.width - fontMetrics.stringWidth(currentLine);
|
||||
width += fontMetrics.stringWidth(currentLine.substring(0, x));
|
||||
return new Point2<>(width, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
public interface TextAligner {
|
||||
Point2<Integer> alignLine(String text, int line);
|
||||
Point2<Integer> alignLine(int line);
|
||||
Point2<Integer> getCaretPosition(int x, int y);
|
||||
Point2<Integer> getPositionOnScreen(int x, int y);
|
||||
void setWholeText(List<StringBuilder> wholeText);
|
||||
|
||||
@@ -207,6 +207,23 @@ public class DropDown extends MenuItem implements Menu{
|
||||
elementHeightSet = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Visual findByName(String name) {
|
||||
if(getName().equals(name)){
|
||||
return this;
|
||||
}
|
||||
else{
|
||||
for(Visual child: items){
|
||||
Visual visual;
|
||||
visual = child.findByName(name);
|
||||
if(visual != null){
|
||||
return visual;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void open() {
|
||||
System.out.println("Opening");
|
||||
isOpen = true;
|
||||
|
||||
42
src/guiTree/Components/Image.java
Normal file
42
src/guiTree/Components/Image.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Visual;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Image extends Visual {
|
||||
private BufferedImage bufferedImage;
|
||||
|
||||
public Image() {
|
||||
}
|
||||
|
||||
public Image(String url) {
|
||||
setImage(url);
|
||||
}
|
||||
|
||||
public Image(BufferedImage image) {
|
||||
setImage(image);
|
||||
}
|
||||
|
||||
public void setImage(BufferedImage image) {
|
||||
this.bufferedImage = image;
|
||||
}
|
||||
|
||||
public void setImage(String url) {
|
||||
try{
|
||||
bufferedImage = ImageIO.read(new File("resources\\icons\\" + url + ".png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
Graphics2D g = imageBuffer.createGraphics();
|
||||
g.drawImage(bufferedImage, 0, 0, getWidth(), getHeight(), null);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Components.Decoarations.CenterTextAligner;
|
||||
import guiTree.Components.Decoarations.LeftTextAligner;
|
||||
import guiTree.Components.Decoarations.RightTextAligner;
|
||||
import guiTree.Components.Decoarations.TextAligner;
|
||||
@@ -148,21 +149,30 @@ public class Text extends Visual {
|
||||
|
||||
public void setAlignment(String textAlignment) {
|
||||
textAlignment = textAlignment.toLowerCase();
|
||||
if(textAlignment.equals("left")) {
|
||||
textAligner = new LeftTextAligner();
|
||||
textAligner.setWholeText(lines);
|
||||
textAligner.setSize(getWidth(), getHeight());
|
||||
textAligner.setSpacing(paragraphSpacing);
|
||||
return;
|
||||
switch(textAlignment) {
|
||||
case "left": {
|
||||
textAligner = new LeftTextAligner();
|
||||
break;
|
||||
}
|
||||
case "right": {
|
||||
textAligner = new RightTextAligner();
|
||||
break;
|
||||
}
|
||||
case "center": {
|
||||
textAligner = new CenterTextAligner();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
System.err.println("Alignment does not exist");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(textAlignment.equals("right")) {
|
||||
textAligner = new RightTextAligner();
|
||||
textAligner.setWholeText(lines);
|
||||
textAligner.setSize(getWidth(), getHeight());
|
||||
textAligner.setSpacing(paragraphSpacing);
|
||||
return;
|
||||
textAligner.setWholeText(lines);
|
||||
textAligner.setSize(getWidth(), getHeight());
|
||||
if(fontMetrics != null) {
|
||||
textAligner.setFontMetrics(fontMetrics);
|
||||
}
|
||||
System.err.println("Alignment does not exist");
|
||||
textAligner.setSpacing(paragraphSpacing);
|
||||
}
|
||||
|
||||
private void copyToClipboard() {
|
||||
@@ -209,47 +219,6 @@ public class Text extends Visual {
|
||||
return textAligner.getCaretPosition(x, y);
|
||||
}
|
||||
|
||||
private void deleteSelection() {
|
||||
selectionRectangles.clear();
|
||||
selectedText.clear();
|
||||
|
||||
if(caretPosition.equals(startDragPosition)) {
|
||||
return;
|
||||
}
|
||||
Point2<Integer> selectionStart;
|
||||
Point2<Integer> selectionEnd;
|
||||
if(caretPosition.compareTo(startDragPosition) < 0) {
|
||||
selectionStart = new Point2<>(caretPosition);
|
||||
selectionEnd = new Point2<>(startDragPosition);
|
||||
}
|
||||
else {
|
||||
selectionStart = new Point2<>(startDragPosition);
|
||||
selectionEnd = new Point2<>(caretPosition);
|
||||
}
|
||||
caretPosition = selectionStart;
|
||||
|
||||
if(selectionStart.y.equals(selectionEnd.y)) {
|
||||
StringBuilder currentLine = lines.get(selectionStart.y);
|
||||
int lineLength = currentLine.length();
|
||||
int newLength = lineLength - selectionEnd.x + selectionStart.x;
|
||||
currentLine.insert(selectionStart.x, currentLine.substring(selectionEnd.x));
|
||||
currentLine = new StringBuilder(currentLine.substring(0, newLength));
|
||||
lines.set(selectionStart.y, currentLine);
|
||||
return;
|
||||
}
|
||||
|
||||
while(selectionStart.y + 1 < selectionEnd.y) {
|
||||
lines.remove(selectionStart.y + 1);
|
||||
selectionEnd.y--;
|
||||
}
|
||||
|
||||
StringBuilder currentLine = lines.get(selectionStart.y);
|
||||
currentLine.insert(selectionStart.x, lines.get(selectionEnd.y).substring(selectionEnd.x));
|
||||
currentLine = new StringBuilder(currentLine.substring(0, selectionStart.x + lines.get(selectionEnd.y).substring(selectionEnd.x).length()));
|
||||
lines.remove((int)selectionEnd.y);
|
||||
lines.set(selectionStart.y, currentLine);
|
||||
}
|
||||
|
||||
private void setSelection() {
|
||||
if(!selectable) {
|
||||
return;
|
||||
@@ -323,7 +292,7 @@ public class Text extends Visual {
|
||||
|
||||
g.setColor(getFontColor());
|
||||
for(StringBuilder line: lines) {
|
||||
Point2<Integer> position = textAligner.alignLine(line.toString(), lines.indexOf(line));
|
||||
Point2<Integer> position = textAligner.alignLine(lines.indexOf(line));
|
||||
g.drawString(line.toString(), position.x, position.y);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user