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:
BIN
resources/icons/heart.png
Normal file
BIN
resources/icons/heart.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
@@ -11,7 +11,7 @@
|
|||||||
<Text
|
<Text
|
||||||
Text="Ioana E Cea Mai Tare"
|
Text="Ioana E Cea Mai Tare"
|
||||||
Name="Text"
|
Name="Text"
|
||||||
Alignment="Right"
|
Alignment="left"
|
||||||
Row="0"
|
Row="0"
|
||||||
Column="0"/>
|
Column="0"/>
|
||||||
<Panel
|
<Panel
|
||||||
@@ -19,8 +19,12 @@
|
|||||||
Name="Panel"
|
Name="Panel"
|
||||||
Row="0"
|
Row="0"
|
||||||
Column="1">
|
Column="1">
|
||||||
|
<Image
|
||||||
|
Size="0.5f, 0.3f"
|
||||||
|
Location="0.5f, 0.0f"
|
||||||
|
Image="heart"/>
|
||||||
<RadioButtons
|
<RadioButtons
|
||||||
Size="1.0f, 0.3f">
|
Size="0.4f, 0.3f">
|
||||||
<RadioButton
|
<RadioButton
|
||||||
Text="Iubesc"
|
Text="Iubesc"
|
||||||
Name="Iubesc"
|
Name="Iubesc"
|
||||||
@@ -52,17 +56,20 @@
|
|||||||
<DropDown
|
<DropDown
|
||||||
BackgroundColor="#222222"
|
BackgroundColor="#222222"
|
||||||
Name="DropDown"
|
Name="DropDown"
|
||||||
Label="Drop Down Menu"
|
Label="Text Alignment"
|
||||||
Location="0.0f, 0.5f"
|
Location="0.0f, 0.5f"
|
||||||
ContentHeight="30"
|
ContentHeight="30"
|
||||||
ContentWidth="100"
|
ContentWidth="100"
|
||||||
ClosedSize="200, 30">
|
ClosedSize="200, 30">
|
||||||
<Button
|
<Button
|
||||||
Name="Button1"
|
Name="align_to_left"
|
||||||
Label="B1"/>
|
Label="Align to Left"/>
|
||||||
<Button
|
<Button
|
||||||
Name="Button2"
|
Name="align_to_right"
|
||||||
Label="B2"/>
|
Label="Align to Right"/>
|
||||||
|
<Button
|
||||||
|
Name="align_to_center"
|
||||||
|
Label="Align to Center"/>
|
||||||
<SideDropDown
|
<SideDropDown
|
||||||
Name="SideDropDown"
|
Name="SideDropDown"
|
||||||
Label="SideDropDown"
|
Label="SideDropDown"
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
|
import guiTree.Components.Button;
|
||||||
|
import guiTree.Components.DropDown;
|
||||||
|
import guiTree.Components.Text;
|
||||||
import guiTree.Window;
|
import guiTree.Window;
|
||||||
|
import guiTree.events.MouseAdapter;
|
||||||
import parser.XAMLParser;
|
import parser.XAMLParser;
|
||||||
|
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Window window = null;
|
Window window = null;
|
||||||
@@ -9,5 +15,35 @@ public class Main {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
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;
|
private int textHeight;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Point2<Integer> alignLine(String text, int line) {
|
public Point2<Integer> alignLine(int line) {
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = (line + 1) * textHeight;
|
int y = (line + 1) * textHeight;
|
||||||
y += spacing * line;
|
y += spacing * line;
|
||||||
@@ -25,14 +25,8 @@ public class LeftTextAligner implements TextAligner{
|
|||||||
public Point2<Integer> getPositionOnScreen(int x, int y){
|
public Point2<Integer> getPositionOnScreen(int x, int y){
|
||||||
String currentLine = wholeText.get(y);
|
String currentLine = wholeText.get(y);
|
||||||
y = (fontMetrics.getHeight() + spacing) * y;
|
y = (fontMetrics.getHeight() + spacing) * y;
|
||||||
int width = 0;
|
int width = fontMetrics.stringWidth(currentLine.substring(0, x));
|
||||||
for(int i = 1; i <= currentLine.length(); i++) {
|
return new Point2<>(width, y);
|
||||||
width += fontMetrics.charWidth(currentLine.charAt(i - 1));
|
|
||||||
if(x == i) {
|
|
||||||
return new Point2<>(width, y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Point2<>(0, y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ public class RightTextAligner implements TextAligner{
|
|||||||
private int width;
|
private int width;
|
||||||
|
|
||||||
@Override
|
@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 x = width - fontMetrics.stringWidth(text);
|
||||||
int y = (line + 1) * textHeight;
|
int y = (line + 1) * textHeight;
|
||||||
y += spacing * line;
|
y += spacing * line;
|
||||||
@@ -47,14 +48,9 @@ public class RightTextAligner implements TextAligner{
|
|||||||
public Point2<Integer> getPositionOnScreen(int x, int y) {
|
public Point2<Integer> getPositionOnScreen(int x, int y) {
|
||||||
String currentLine = wholeText.get(y);
|
String currentLine = wholeText.get(y);
|
||||||
y = (fontMetrics.getHeight() + spacing) * y;
|
y = (fontMetrics.getHeight() + spacing) * y;
|
||||||
int width = this.width;
|
int width = this.width - fontMetrics.stringWidth(currentLine);
|
||||||
for(int i = currentLine.length(); i > 0; i--) {
|
width += fontMetrics.stringWidth(currentLine.substring(0, x));
|
||||||
if(x == i) {
|
return new Point2<>(width, y);
|
||||||
return new Point2<>(width, y);
|
|
||||||
}
|
|
||||||
width -= fontMetrics.charWidth(currentLine.charAt(i - 1));
|
|
||||||
}
|
|
||||||
return new Point2<>(this.width - fontMetrics.stringWidth(currentLine), y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import java.awt.*;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface TextAligner {
|
public interface TextAligner {
|
||||||
Point2<Integer> alignLine(String text, int line);
|
Point2<Integer> alignLine(int line);
|
||||||
Point2<Integer> getCaretPosition(int x, int y);
|
Point2<Integer> getCaretPosition(int x, int y);
|
||||||
Point2<Integer> getPositionOnScreen(int x, int y);
|
Point2<Integer> getPositionOnScreen(int x, int y);
|
||||||
void setWholeText(List<StringBuilder> wholeText);
|
void setWholeText(List<StringBuilder> wholeText);
|
||||||
|
|||||||
@@ -207,6 +207,23 @@ public class DropDown extends MenuItem implements Menu{
|
|||||||
elementHeightSet = true;
|
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() {
|
public void open() {
|
||||||
System.out.println("Opening");
|
System.out.println("Opening");
|
||||||
isOpen = true;
|
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;
|
package guiTree.Components;
|
||||||
|
|
||||||
|
import guiTree.Components.Decoarations.CenterTextAligner;
|
||||||
import guiTree.Components.Decoarations.LeftTextAligner;
|
import guiTree.Components.Decoarations.LeftTextAligner;
|
||||||
import guiTree.Components.Decoarations.RightTextAligner;
|
import guiTree.Components.Decoarations.RightTextAligner;
|
||||||
import guiTree.Components.Decoarations.TextAligner;
|
import guiTree.Components.Decoarations.TextAligner;
|
||||||
@@ -148,21 +149,30 @@ public class Text extends Visual {
|
|||||||
|
|
||||||
public void setAlignment(String textAlignment) {
|
public void setAlignment(String textAlignment) {
|
||||||
textAlignment = textAlignment.toLowerCase();
|
textAlignment = textAlignment.toLowerCase();
|
||||||
if(textAlignment.equals("left")) {
|
switch(textAlignment) {
|
||||||
textAligner = new LeftTextAligner();
|
case "left": {
|
||||||
textAligner.setWholeText(lines);
|
textAligner = new LeftTextAligner();
|
||||||
textAligner.setSize(getWidth(), getHeight());
|
break;
|
||||||
textAligner.setSpacing(paragraphSpacing);
|
}
|
||||||
return;
|
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.setWholeText(lines);
|
||||||
textAligner = new RightTextAligner();
|
textAligner.setSize(getWidth(), getHeight());
|
||||||
textAligner.setWholeText(lines);
|
if(fontMetrics != null) {
|
||||||
textAligner.setSize(getWidth(), getHeight());
|
textAligner.setFontMetrics(fontMetrics);
|
||||||
textAligner.setSpacing(paragraphSpacing);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
System.err.println("Alignment does not exist");
|
textAligner.setSpacing(paragraphSpacing);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copyToClipboard() {
|
private void copyToClipboard() {
|
||||||
@@ -209,47 +219,6 @@ public class Text extends Visual {
|
|||||||
return textAligner.getCaretPosition(x, y);
|
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() {
|
private void setSelection() {
|
||||||
if(!selectable) {
|
if(!selectable) {
|
||||||
return;
|
return;
|
||||||
@@ -323,7 +292,7 @@ public class Text extends Visual {
|
|||||||
|
|
||||||
g.setColor(getFontColor());
|
g.setColor(getFontColor());
|
||||||
for(StringBuilder line: lines) {
|
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);
|
g.drawString(line.toString(), position.x, position.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user