mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 21:50:04 +00:00
most of mouse listeners, added title bar and title bar listeners with maximize and drag reposition
This commit is contained in:
146
src/guiTree/Components/Button.java
Normal file
146
src/guiTree/Components/Button.java
Normal file
@@ -0,0 +1,146 @@
|
||||
package guiTree.Components;
|
||||
|
||||
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 Button extends Visual {
|
||||
private String label;
|
||||
private Boolean pressed;
|
||||
private Boolean hovered;
|
||||
private BufferedImage icon;
|
||||
|
||||
public Button() {
|
||||
this("", null);
|
||||
}
|
||||
|
||||
public Button(String label) {
|
||||
this(label, null);
|
||||
}
|
||||
|
||||
public Button(BufferedImage icon) {
|
||||
this(null, icon);
|
||||
}
|
||||
|
||||
public Button(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) {
|
||||
System.out.println("Pressed: " + getName());
|
||||
pressed = true;
|
||||
revalidate();
|
||||
}
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent mouseEvent) {
|
||||
System.out.println("Released: " + getName());
|
||||
pressed = false;
|
||||
revalidate();
|
||||
}
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent mouseEvent) {
|
||||
System.out.println("Entered: " + getName());
|
||||
hovered = true;
|
||||
revalidate();
|
||||
}
|
||||
@Override
|
||||
public void mouseExited(MouseEvent mouseEvent) {
|
||||
System.out.println("Exited: " + getName());
|
||||
hovered = false;
|
||||
revalidate();
|
||||
}
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent mouseEvent) {
|
||||
System.out.println("DRAGGING: " + getName());
|
||||
revalidate();
|
||||
}
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent mouseEvent) {
|
||||
System.out.println("Moved: " + getName());
|
||||
revalidate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@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 || pressed) {
|
||||
g.setColor(this.getForegroundColor());
|
||||
}
|
||||
else {
|
||||
g.setColor(this.getBackgroundColor());
|
||||
}
|
||||
|
||||
//Draw Button
|
||||
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||||
g.setColor(this.getFontColor());
|
||||
|
||||
//Draw Label
|
||||
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;
|
||||
}
|
||||
}
|
||||
148
src/guiTree/Components/TitleBar.java
Normal file
148
src/guiTree/Components/TitleBar.java
Normal file
@@ -0,0 +1,148 @@
|
||||
package guiTree.Components;
|
||||
|
||||
import guiTree.Helper.Point2d;
|
||||
import guiTree.Visual;
|
||||
import guiTree.events.MouseAdapter;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class TitleBar extends Visual {
|
||||
public static final int MINIMIZE = 1;
|
||||
public static final int MAXIMIZE = 2;
|
||||
public static final int CLOSE = 3;
|
||||
|
||||
private BufferedImage icon;
|
||||
private String title;
|
||||
private Button minimize;
|
||||
private Button maximize;
|
||||
private Button close;
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
Constructors
|
||||
---------------------------------------------------------------------*/
|
||||
public TitleBar() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
public TitleBar(String title) {
|
||||
this(title, null);
|
||||
}
|
||||
|
||||
public TitleBar(BufferedImage icon) {
|
||||
this(null, icon);
|
||||
}
|
||||
|
||||
public TitleBar(String title, BufferedImage icon) {
|
||||
super();
|
||||
this.icon = icon;
|
||||
this.title = title;
|
||||
|
||||
this.close = new Button();
|
||||
this.minimize = new Button();
|
||||
this.maximize = new Button();
|
||||
close.setIcon("close_white");
|
||||
minimize.setIcon("minimize_white");
|
||||
maximize.setIcon("square_white");
|
||||
close.setSize(60, 30);
|
||||
minimize.setSize(60, 30);
|
||||
maximize.setSize(60, 30);
|
||||
close.setBackgroundColor(Color.GRAY);
|
||||
maximize.setBackgroundColor(Color.GRAY);
|
||||
minimize.setBackgroundColor(Color.GRAY);
|
||||
close.setForegroundColor(Color.RED);
|
||||
maximize.setForegroundColor(Color.LIGHT_GRAY);
|
||||
minimize.setForegroundColor(Color.LIGHT_GRAY);
|
||||
|
||||
this.setSize(1, 30);
|
||||
this.setLocation(0, 0);
|
||||
|
||||
setButtonLocation();
|
||||
close.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent mouseEvent) {
|
||||
notifyParent(3);
|
||||
}
|
||||
});
|
||||
maximize.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent mouseEvent) {
|
||||
notifyParent(2);
|
||||
}
|
||||
});
|
||||
minimize.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent mouseEvent) {
|
||||
notifyParent(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
Getters
|
||||
---------------------------------------------------------------------*/
|
||||
|
||||
public BufferedImage getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
public String geTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
Setters
|
||||
---------------------------------------------------------------------*/
|
||||
|
||||
public void setIcon(BufferedImage icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(Integer width, Integer height) {
|
||||
super.setSize(width, height);
|
||||
setButtonLocation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
Graphics2D g = imageBuffer.createGraphics();
|
||||
g.setColor(getBackgroundColor());
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
Graphics2D iconGraphics = imageBuffer.createGraphics();
|
||||
iconGraphics.drawImage(icon, 5, (getHeight() - icon.getHeight())/2, null);
|
||||
iconGraphics.dispose();
|
||||
|
||||
int stringOffset = icon.getWidth() + 10;
|
||||
int textHeight = 0;
|
||||
if(!title.equals("")) {
|
||||
textHeight = g.getFontMetrics().getHeight();
|
||||
g.setColor(Color.WHITE);
|
||||
}
|
||||
g.drawString(title, stringOffset, getHeight()/2 + textHeight/4);
|
||||
}
|
||||
|
||||
private void setButtonLocation() {
|
||||
int buttonOffset = this.getWidth() - close.getWidth();
|
||||
close.setLocation(buttonOffset, 0);
|
||||
close.setName("close");
|
||||
buttonOffset -= maximize.getWidth();
|
||||
maximize.setLocation(buttonOffset, 0);
|
||||
maximize.setName("maximize");
|
||||
buttonOffset -= minimize.getWidth();
|
||||
minimize.setLocation(buttonOffset, 0);
|
||||
minimize.setName("minimize");
|
||||
this.addVisual(close);
|
||||
this.addVisual(minimize);
|
||||
this.addVisual(maximize);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user