diff --git a/.gitignore b/.gitignore index a1c2a23..18571a4 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,9 @@ *.zip *.tar.gz *.rar +*.module # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +.idea/ +out/ diff --git a/AWT startup.iml b/AWT startup.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/AWT startup.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Button.java b/src/Button.java new file mode 100644 index 0000000..4848c9c --- /dev/null +++ b/src/Button.java @@ -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); + } +} diff --git a/src/CustomFrame.java b/src/CustomFrame.java new file mode 100644 index 0000000..1b89dc8 --- /dev/null +++ b/src/CustomFrame.java @@ -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); + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..cdca9cd --- /dev/null +++ b/src/Main.java @@ -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); + } +} diff --git a/src/Visual.java b/src/Visual.java new file mode 100644 index 0000000..f520303 --- /dev/null +++ b/src/Visual.java @@ -0,0 +1,127 @@ +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.List; + +public class Visual { + private List 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(); + } + } +} diff --git a/src/Window.java b/src/Window.java new file mode 100644 index 0000000..de1608a --- /dev/null +++ b/src/Window.java @@ -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); + } +}