mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 21:50:04 +00:00
bugs
added checkbox lists
This commit is contained in:
@@ -2,7 +2,6 @@ package guiTree.Components;
|
||||
|
||||
import guiTree.Helper.Debugger;
|
||||
import guiTree.Helper.Tag;
|
||||
import guiTree.Helper.Timer;
|
||||
import guiTree.Visual;
|
||||
import guiTree.events.MouseAdapter;
|
||||
|
||||
@@ -111,6 +110,9 @@ public class Button extends Visual {
|
||||
}
|
||||
|
||||
//Draw Label
|
||||
if(getFont() != null) {
|
||||
g.setFont(getFont());
|
||||
}
|
||||
g.setColor(this.getFontColor());
|
||||
int textWidth = 0;
|
||||
int textHeight = 0;
|
||||
|
||||
134
src/guiTree/Components/CheckBox.java
Normal file
134
src/guiTree/Components/CheckBox.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Helper.Debugger;
|
||||
import guiTree.Helper.Tag;
|
||||
import guiTree.Visual;
|
||||
import guiTree.events.MouseAdapter;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CheckBox extends Visual {
|
||||
private BufferedImage icon;
|
||||
private boolean hovered;
|
||||
private boolean marked;
|
||||
private String text;
|
||||
|
||||
public CheckBox() {
|
||||
this(false, "");
|
||||
}
|
||||
|
||||
public CheckBox(Boolean marked) {
|
||||
this(marked, "");
|
||||
}
|
||||
|
||||
public CheckBox(String text) {
|
||||
this(false, text);
|
||||
}
|
||||
|
||||
public CheckBox(Boolean value, String text) {
|
||||
super();
|
||||
this.marked = value;
|
||||
this.text = text;
|
||||
setAccentColor(new Color(0.6f, 0.6f, 0.6f, 0.5f));
|
||||
this.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
marked = !marked;
|
||||
Debugger.log("Calling repaint from pressed: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent mouseEvent) {
|
||||
hovered = true;
|
||||
Debugger.log("Calling repaint from entered: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void mouseExited(MouseEvent mouseEvent) {
|
||||
hovered = false;
|
||||
Debugger.log("Calling repaint from exited: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent mouseEvent) {
|
||||
Debugger.log("Calling repaint from moved: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isMarked() {
|
||||
return marked;
|
||||
}
|
||||
|
||||
public void setMarked(boolean marked) {
|
||||
this.marked = marked;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public void setIcon(BufferedImage icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public void setIcon(String url) {
|
||||
try{
|
||||
icon = ImageIO.read(new File("resources\\icons\\" + url + ".png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
|
||||
if(hovered && !marked) {
|
||||
g.setColor(getAccentColor());
|
||||
g.fillRect(0, 0, getHeight() - 1, getHeight() - 1);
|
||||
}
|
||||
|
||||
if(marked) {
|
||||
g.setColor(getForegroundColor());
|
||||
g.fillRect(1, 1, getHeight() - 2, getHeight() - 2);
|
||||
|
||||
if(icon != null) {
|
||||
int iconWidth = icon.getWidth();
|
||||
int iconHeight = icon.getHeight();
|
||||
|
||||
int iconX = (this.getHeight() - iconWidth) / 2;
|
||||
int iconY = (this.getHeight() - iconHeight) / 2;
|
||||
Graphics2D g2 = imageBuffer.createGraphics();
|
||||
g2.drawImage(icon, iconX, iconY, null);
|
||||
g2.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
g.setColor(getFontColor());
|
||||
if(getFont() != null) {
|
||||
g.setFont(getFont());
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
151
src/guiTree/Components/CheckBoxList.java
Normal file
151
src/guiTree/Components/CheckBoxList.java
Normal file
@@ -0,0 +1,151 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Helper.Point2d;
|
||||
import guiTree.Visual;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CheckBoxList extends Visual {
|
||||
private List<CheckBox> checkBoxList;
|
||||
int spacing;
|
||||
private BufferedImage icon;
|
||||
private Point2d checkBoxSize;
|
||||
|
||||
public CheckBoxList() {
|
||||
this(20);
|
||||
}
|
||||
|
||||
public CheckBoxList(int spacing) {
|
||||
checkBoxList = new ArrayList<>();
|
||||
this.spacing = spacing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addVisual(Visual v) {
|
||||
if(!(v instanceof CheckBox)) {
|
||||
System.out.println("Trying to insert into checkbox list something different from checkbox");
|
||||
return;
|
||||
}
|
||||
CheckBox checkbox = (CheckBox)v;
|
||||
if(checkBoxList.size() == 0) {
|
||||
checkbox.setLocation(0, 0);
|
||||
|
||||
}
|
||||
else {
|
||||
checkbox.setLocation(0, checkBoxList.get(checkBoxList.size() - 1).getLocationY() + spacing);
|
||||
}
|
||||
if(icon != null) {
|
||||
checkbox.setIcon(icon);
|
||||
}
|
||||
if(checkBoxSize != null) {
|
||||
checkbox.setSize(checkBoxSize.x, checkBoxSize.y);
|
||||
}
|
||||
checkbox.setForegroundColor(getForegroundColor());
|
||||
checkbox.setBackgroundColor(getBackgroundColor());
|
||||
checkbox.setAccentColor(getAccentColor());
|
||||
checkbox.setFontColor(getFontColor());
|
||||
checkBoxList.add(checkbox);
|
||||
super.addVisual(checkbox);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeVisual(Visual v) {
|
||||
if(v instanceof CheckBox) {
|
||||
checkBoxList.remove(v);
|
||||
super.removeVisual(v);
|
||||
}
|
||||
}
|
||||
|
||||
public void setIcon(BufferedImage icon) {
|
||||
this.icon = icon;
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setIcon(icon);
|
||||
}
|
||||
}
|
||||
|
||||
public void setIcon(String url) {
|
||||
try {
|
||||
icon = ImageIO.read(new File("resources\\icons\\" + url + ".png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setIcon(icon);
|
||||
}
|
||||
}
|
||||
|
||||
public void setCheckBoxSize(Integer width, Integer height) {
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setSize(width, height);
|
||||
}
|
||||
checkBoxSize = new Point2d(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFont(Font font) {
|
||||
super.setFont(font);
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setFont(font);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBackgroundColor(Color backgroundColor) {
|
||||
super.setBackgroundColor(backgroundColor);
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setBackgroundColor(backgroundColor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setForegroundColor(Color foregroundColor) {
|
||||
super.setForegroundColor(foregroundColor);
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setForegroundColor(foregroundColor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFontColor(Color fontColor) {
|
||||
super.setFontColor(fontColor);
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setFontColor(fontColor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAccentColor(Color accentColor) {
|
||||
super.setAccentColor(accentColor);
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setAccentColor(accentColor);
|
||||
}
|
||||
}
|
||||
|
||||
public void setSpacing(Integer spacing) {
|
||||
this.spacing = spacing;
|
||||
if(checkBoxList == null) {
|
||||
return;
|
||||
}
|
||||
int offsetY = 0;
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
cb.setLocationY(offsetY);
|
||||
offsetY += spacing;
|
||||
}
|
||||
}
|
||||
|
||||
public List<CheckBox> getActiveBoxes() {
|
||||
List<CheckBox> markedBoxes = new ArrayList<>();
|
||||
for(CheckBox cb: checkBoxList) {
|
||||
if(cb.isMarked()) {
|
||||
markedBoxes.add(cb);
|
||||
}
|
||||
}
|
||||
return markedBoxes;
|
||||
}
|
||||
}
|
||||
@@ -124,6 +124,7 @@ public class TitleBar extends Visual {
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
update();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,6 +143,9 @@ public class TitleBar extends Visual {
|
||||
iconGraphics.drawImage(icon, 5, (getHeight() - icon.getHeight())/2, null);
|
||||
iconGraphics.dispose();
|
||||
|
||||
if(getFont() != null) {
|
||||
g.setFont(getFont());
|
||||
}
|
||||
int stringOffset = icon.getWidth() + 10;
|
||||
int textHeight = 0;
|
||||
if(!title.equals("")) {
|
||||
|
||||
159
src/guiTree/Components/ToggleButton.java
Normal file
159
src/guiTree/Components/ToggleButton.java
Normal file
@@ -0,0 +1,159 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Helper.Debugger;
|
||||
import guiTree.Helper.Tag;
|
||||
import guiTree.Visual;
|
||||
import guiTree.events.MouseAdapter;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ToggleButton extends Visual {
|
||||
private String label;
|
||||
private Boolean pressed;
|
||||
private Boolean hovered;
|
||||
private BufferedImage icon;
|
||||
|
||||
public ToggleButton() {
|
||||
this("", null);
|
||||
}
|
||||
|
||||
public ToggleButton(String label) {
|
||||
this(label, null);
|
||||
}
|
||||
|
||||
public ToggleButton(BufferedImage icon) {
|
||||
this(null, icon);
|
||||
}
|
||||
|
||||
public ToggleButton(String label, BufferedImage icon) {
|
||||
super();
|
||||
this.label = label;
|
||||
this.icon = icon;
|
||||
pressed = false;
|
||||
hovered = false;
|
||||
this.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
pressed = !pressed;
|
||||
Debugger.log("Pressed: " + getName(), Tag.LISTENER);
|
||||
Debugger.log("Calling repaint from pressed: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent mouseEvent) {
|
||||
hovered = true;
|
||||
Debugger.log("Calling repaint from entered: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void mouseExited(MouseEvent mouseEvent) {
|
||||
hovered = false;
|
||||
Debugger.log("Calling repaint from exited: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent mouseEvent) {
|
||||
Debugger.log("Calling repaint from moved: " + getName(), Tag.PAINTING);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(BufferedImage imageBuffer)
|
||||
{
|
||||
//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());
|
||||
}
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
//Draw Label
|
||||
if(getFont() != null) {
|
||||
g.setFont(getFont());
|
||||
}
|
||||
g.setColor(this.getFontColor());
|
||||
int textWidth = 0;
|
||||
int textHeight = 0;
|
||||
if(!label.equals("")) {
|
||||
textWidth = g.getFontMetrics().stringWidth(label);
|
||||
textHeight = g.getFontMetrics().getHeight();
|
||||
}
|
||||
|
||||
g.drawString(this.label, (this.getWidth() - textWidth)/2, (this.getHeight() + textHeight)/2);
|
||||
|
||||
//Draw Icon
|
||||
if(icon != null) {
|
||||
int iconWidth = icon.getWidth();
|
||||
int iconHeight = icon.getHeight();
|
||||
|
||||
int iconX = (this.getWidth() - iconWidth - textWidth) / 2;
|
||||
int iconY = (this.getHeight() - iconHeight - textHeight) / 2;
|
||||
Graphics2D g2 = imageBuffer.createGraphics();
|
||||
g2.drawImage(icon, iconX, iconY, null);
|
||||
g2.dispose();
|
||||
}
|
||||
|
||||
g.dispose();
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return this.label;
|
||||
}
|
||||
|
||||
public void setIcon(BufferedImage icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public void setIcon(String url) {
|
||||
try{
|
||||
icon = ImageIO.read(new File("resources\\icons\\" + url + ".png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public BufferedImage getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
public void setPressed(Boolean pressed) {
|
||||
this.pressed = pressed;
|
||||
}
|
||||
|
||||
public Boolean getPressed() {
|
||||
return pressed;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package guiTree.Helper;
|
||||
|
||||
public enum Tag {
|
||||
LISTENER(false),
|
||||
LISTENER(true),
|
||||
PAINTING(false);
|
||||
|
||||
public boolean value;
|
||||
|
||||
@@ -48,6 +48,8 @@ public class Visual {
|
||||
private Integer absoluteY;
|
||||
private Float relativeX;
|
||||
private Float relativeY;
|
||||
private Boolean hasBorder;
|
||||
private Font font;
|
||||
private Color backgroundColor;
|
||||
private Color foregroundColor;
|
||||
private Color accentColor;
|
||||
@@ -57,7 +59,6 @@ public class Visual {
|
||||
public Boolean dirty;
|
||||
private static Visual entered;
|
||||
private Boolean focused;
|
||||
private Boolean hasBorder;
|
||||
private Boolean pressed;
|
||||
|
||||
|
||||
@@ -109,7 +110,7 @@ public class Visual {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private void setSize() {
|
||||
public void setSize() {
|
||||
if(parent != null) {
|
||||
if(relativeWidth > 0.0) {
|
||||
width = Math.round(relativeWidth * parent.width);
|
||||
@@ -180,6 +181,10 @@ public class Visual {
|
||||
this.locationY = y;
|
||||
}
|
||||
|
||||
public void setFont(Font font) {
|
||||
this.font = font;
|
||||
}
|
||||
|
||||
public void setBackgroundColor(Color backgroundColor) {
|
||||
this.backgroundColor = backgroundColor;
|
||||
propagateDirt();
|
||||
@@ -230,6 +235,10 @@ public class Visual {
|
||||
return this.locationY;
|
||||
}
|
||||
|
||||
public Font getFont() {
|
||||
return font;
|
||||
}
|
||||
|
||||
public Color getBackgroundColor() {
|
||||
return backgroundColor;
|
||||
}
|
||||
@@ -339,8 +348,11 @@ public class Visual {
|
||||
window.revalidate();
|
||||
}
|
||||
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
public void update() {
|
||||
propagateDirt();
|
||||
}
|
||||
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
@@ -508,8 +520,12 @@ public class Visual {
|
||||
if(imageBuffer == null) {
|
||||
return;
|
||||
}
|
||||
Graphics g = this.imageBuffer.getGraphics();
|
||||
g.setColor(backgroundColor);
|
||||
Graphics2D g = this.imageBuffer.createGraphics();
|
||||
|
||||
g.setComposite(AlphaComposite.Clear);
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
g.setComposite(AlphaComposite.Src);
|
||||
|
||||
g.fillRect(0, 0, width, height);
|
||||
g.dispose();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user