mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 13:40:04 +00:00
initial setup with top-bottom approach
This commit is contained in:
18
src/Button.java
Normal file
18
src/Button.java
Normal file
@@ -0,0 +1,18 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Button extends Visual {
|
||||
|
||||
public Button()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(BufferedImage imageBuffer)
|
||||
{
|
||||
Graphics g = imageBuffer.getGraphics();
|
||||
g.setColor(Color.BLUE);
|
||||
g.fillRect(10, 33, 500, 500);
|
||||
}
|
||||
}
|
||||
48
src/CustomFrame.java
Normal file
48
src/CustomFrame.java
Normal file
@@ -0,0 +1,48 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class CustomFrame extends Frame {
|
||||
private BufferedImage imageBuffer;
|
||||
|
||||
public CustomFrame()
|
||||
{
|
||||
super();
|
||||
this.addWindowStateListener(e -> {
|
||||
imageBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
|
||||
imageBuffer.getGraphics().setColor(Color.WHITE);
|
||||
imageBuffer.getGraphics().fillRect(0, 0, getWidth(), getHeight());
|
||||
imageBuffer.getGraphics().setColor(Color.BLACK);
|
||||
});
|
||||
}
|
||||
|
||||
public BufferedImage getImageBuffer() {
|
||||
return this.imageBuffer;
|
||||
}
|
||||
|
||||
public CustomFrame(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(Dimension d)
|
||||
{
|
||||
this.setSize(d.width, d.height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(int width, int height)
|
||||
{
|
||||
super.setSize(width, height);
|
||||
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
|
||||
this.imageBuffer.getGraphics().setColor(Color.WHITE);
|
||||
this.imageBuffer.getGraphics().fillRect(0, 0, this.getWidth(), this.getHeight());
|
||||
this.imageBuffer.getGraphics().setColor(Color.BLACK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g)
|
||||
{
|
||||
g.drawImage(imageBuffer, 5, 0, this.getWidth(), this.getHeight(), null);
|
||||
}
|
||||
}
|
||||
20
src/Main.java
Normal file
20
src/Main.java
Normal file
@@ -0,0 +1,20 @@
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Window win = new Window();
|
||||
win.setVisible(true);
|
||||
win.setSize(640, 480);
|
||||
win.setPositionRelativeTo(null);
|
||||
win.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
win.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
Button button = new Button();
|
||||
win.addVisual(button);
|
||||
}
|
||||
}
|
||||
127
src/Visual.java
Normal file
127
src/Visual.java
Normal file
@@ -0,0 +1,127 @@
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Visual {
|
||||
private List<Visual> children;
|
||||
private Visual parent;
|
||||
private BufferedImage imageBuffer;
|
||||
private int width;
|
||||
private int height;
|
||||
private int locationX;
|
||||
private int locationY;
|
||||
|
||||
|
||||
public Visual() {
|
||||
this.children = new ArrayList<>();
|
||||
this.parent = null;
|
||||
this.imageBuffer = new BufferedImage(0, 0, BufferedImage.TYPE_3BYTE_BGR);
|
||||
}
|
||||
|
||||
public int getWidth()
|
||||
{
|
||||
return this.width;
|
||||
}
|
||||
|
||||
public int getHeight()
|
||||
{
|
||||
return this.height;
|
||||
}
|
||||
|
||||
public void setSize(int width, int height){
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
// if(this.parent != null) {
|
||||
// this.imageBuffer = this.parent.getImageBuffer();
|
||||
// }
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
private void calculateInitialSize() {
|
||||
this.width = this.parent.getWidth() - this.getLocationX();
|
||||
this.height = this.parent.getHeight() - this.getLocationY();
|
||||
this.imageBuffer = this.parent.getImageBuffer().getSubimage(this.locationX, this.locationY, this.width, this.height);
|
||||
}
|
||||
|
||||
private void calculateInitialLocation(){
|
||||
this.locationX = 200;
|
||||
this.locationY = 200;
|
||||
}
|
||||
|
||||
public void addVisual(Visual child) {
|
||||
this.children.add(child);
|
||||
child.setParent(this);
|
||||
child.calculateInitialLocation();
|
||||
child.calculateInitialSize();
|
||||
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
private void setParent(Visual parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
private void handleNotification() {
|
||||
|
||||
}
|
||||
|
||||
private void notifyParent()
|
||||
{
|
||||
this.parent.handleNotification();
|
||||
}
|
||||
|
||||
public void repaint() {
|
||||
if(this instanceof Window)
|
||||
{
|
||||
for(Visual v: children)
|
||||
{
|
||||
v.paint(this.imageBuffer);
|
||||
}
|
||||
}
|
||||
else{
|
||||
this.parent.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void callRepaint()
|
||||
{
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public int getLocationX(){
|
||||
return this.locationX;
|
||||
}
|
||||
|
||||
public int getLocationY(){
|
||||
return this.locationY;
|
||||
}
|
||||
|
||||
public void setLocationX(int locationX){
|
||||
this.locationX = locationX;
|
||||
}
|
||||
|
||||
public void setLocationY(int locationY){
|
||||
this.locationY = locationY;
|
||||
}
|
||||
|
||||
public void paint(BufferedImage imageBuffer) {
|
||||
|
||||
}
|
||||
|
||||
public void setImageBuffer(BufferedImage imageBuffer){
|
||||
this.imageBuffer = imageBuffer;
|
||||
}
|
||||
|
||||
private BufferedImage getImageBuffer() {
|
||||
return this.imageBuffer;
|
||||
}
|
||||
|
||||
public void triggerBufferChange() {
|
||||
for(Visual v: children){
|
||||
v.imageBuffer = this.imageBuffer;
|
||||
v.triggerBufferChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
54
src/Window.java
Normal file
54
src/Window.java
Normal file
@@ -0,0 +1,54 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.WindowListener;
|
||||
import java.awt.event.WindowStateListener;
|
||||
|
||||
public class Window extends Visual {
|
||||
public CustomFrame frame;
|
||||
|
||||
public Window()
|
||||
{
|
||||
super();
|
||||
this.frame = new CustomFrame();
|
||||
this.addWindowStateListener(e -> {
|
||||
setImageBuffer(frame.getImageBuffer());
|
||||
triggerBufferChange();
|
||||
repaint();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(int width, int height)
|
||||
{
|
||||
this.frame.setSize(width, height);
|
||||
this.setImageBuffer(this.frame.getImageBuffer());
|
||||
super.setSize(width, height);
|
||||
}
|
||||
|
||||
public void setSize(Dimension dimension) {
|
||||
this.setSize(dimension.width, dimension.height);
|
||||
}
|
||||
|
||||
public void setVisible(Boolean visible)
|
||||
{
|
||||
frame.setVisible(visible);
|
||||
}
|
||||
|
||||
public void addWindowListener(WindowListener listener)
|
||||
{
|
||||
frame.addWindowListener(listener);
|
||||
}
|
||||
|
||||
public void addWindowStateListener(WindowStateListener listener)
|
||||
{
|
||||
frame.addWindowStateListener(listener);
|
||||
}
|
||||
|
||||
public void dispose()
|
||||
{
|
||||
frame.dispose();
|
||||
}
|
||||
|
||||
public void setPositionRelativeTo(Component c){
|
||||
frame.setLocationRelativeTo(c);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user