From 2e354745785fa20bfe74753aade17408044ae6c0 Mon Sep 17 00:00:00 2001 From: Macocian Radu Date: Tue, 17 Dec 2019 02:23:51 +0200 Subject: [PATCH] displaying element but erasing everything on location/size change --- src/Button.java | 2 +- src/CustomFrame.java | 26 +------- src/Main.java | 18 ++++++ src/Visual.java | 151 +++++++++++++++++++++++++++++-------------- src/Window.java | 12 ++-- 5 files changed, 130 insertions(+), 79 deletions(-) diff --git a/src/Button.java b/src/Button.java index 4848c9c..0d9ea49 100644 --- a/src/Button.java +++ b/src/Button.java @@ -13,6 +13,6 @@ public class Button extends Visual { { Graphics g = imageBuffer.getGraphics(); g.setColor(Color.BLUE); - g.fillRect(10, 33, 500, 500); + g.fillRect(0, 0, getWidth(), getHeight()); } } diff --git a/src/CustomFrame.java b/src/CustomFrame.java index 1b89dc8..6a6bfb5 100644 --- a/src/CustomFrame.java +++ b/src/CustomFrame.java @@ -7,16 +7,6 @@ public class CustomFrame extends Frame { 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) @@ -24,20 +14,8 @@ public class CustomFrame extends Frame { 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); + public void setImageBuffer(BufferedImage imageBuffer) { + this.imageBuffer = imageBuffer; } @Override diff --git a/src/Main.java b/src/Main.java index cdca9cd..406768d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -16,5 +16,23 @@ public class Main { Button button = new Button(); win.addVisual(button); + int x = 20; + int y = 20; + while(true){ + if(x > win.getWidth()){ + x = 0; + y+=20; + } + if(y > win.getHeight()){ + y = 0; + } + button.setLocation(x, y); + x+=10; + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } } diff --git a/src/Visual.java b/src/Visual.java index f520303..5fac94b 100644 --- a/src/Visual.java +++ b/src/Visual.java @@ -1,23 +1,45 @@ +import java.awt.*; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; public class Visual { + + /*-------------------------------------------------------------------- + Tree Elements + ---------------------------------------------------------------------*/ private List children; private Visual parent; private BufferedImage imageBuffer; + + + /*-------------------------------------------------------------------- + Attributes + ---------------------------------------------------------------------*/ private int width; private int height; private int locationX; private int locationY; + private Color backgroundColor; + private Color foregroundColor; + private Boolean valid; + /*-------------------------------------------------------------------- + Constructors + ---------------------------------------------------------------------*/ public Visual() { this.children = new ArrayList<>(); this.parent = null; - this.imageBuffer = new BufferedImage(0, 0, BufferedImage.TYPE_3BYTE_BGR); + this.backgroundColor = Color.WHITE; + this.foregroundColor = Color.BLACK; + valid = false; } + + /*-------------------------------------------------------------------- + Attributes Methods + ---------------------------------------------------------------------*/ public int getWidth() { return this.width; @@ -31,17 +53,51 @@ public class Visual { public void setSize(int width, int height){ this.width = width; this.height = height; - -// if(this.parent != null) { -// this.imageBuffer = this.parent.getImageBuffer(); -// } - this.repaint(); + + initializeImageBuffer(width, height); + + this.valid = false; + this.revalidate(); + } + + public int getLocationX(){ + return this.locationX; + } + + public int getLocationY(){ + return this.locationY; + } + + public void setLocation(int x, int y){ + this.locationX = x; + this.locationY = y; + this.valid = false; + this.revalidate(); + } + + public Color getBackgroundColor() { + return backgroundColor; + } + + public void setBackgroundColor(Color backgroundColor) { + this.backgroundColor = backgroundColor; + this.valid = false; + this.revalidate(); + } + + public Color getForegroundColor() { + return foregroundColor; + } + + public void setForegroundColor(Color foregroundColor) { + this.foregroundColor = foregroundColor; + this.valid = false; + this.revalidate(); } 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); + this.width = 20; + this.height = 20; } private void calculateInitialLocation(){ @@ -49,13 +105,20 @@ public class Visual { this.locationY = 200; } + + /*-------------------------------------------------------------------- + Tree Methods + ---------------------------------------------------------------------*/ + public void addVisual(Visual child) { this.children.add(child); child.setParent(this); child.calculateInitialLocation(); child.calculateInitialSize(); + child.imageBuffer = new BufferedImage(child.getWidth(), child.getHeight(), BufferedImage.TYPE_3BYTE_BGR); + child.valid = false; - this.repaint(); + this.revalidate(); } private void setParent(Visual parent) @@ -72,56 +135,44 @@ public class Visual { 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(); + this.paint(this.imageBuffer); + this.parent.revalidate(); } - 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 revalidate() { + initializeImageBuffer(width, height); + this.paint(imageBuffer); + for(Visual v:children){ + if(!v.valid){ + v.paint(v.imageBuffer); + } + this.imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null); + v.valid = true; + } + this.valid = true; + if(!(this instanceof Window)){ + this.parent.revalidate(); + } + else{ + Window window = (Window)this; + window.setFrameImageBuffer(this.imageBuffer); + } } public void paint(BufferedImage imageBuffer) { } - public void setImageBuffer(BufferedImage imageBuffer){ - this.imageBuffer = imageBuffer; - } + /*-------------------------------------------------------------------- + Helper Methods + ---------------------------------------------------------------------*/ - private BufferedImage getImageBuffer() { - return this.imageBuffer; - } - - public void triggerBufferChange() { - for(Visual v: children){ - v.imageBuffer = this.imageBuffer; - v.triggerBufferChange(); - } + private void initializeImageBuffer(int width, int height){ + this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); + this.imageBuffer.getGraphics().setColor(backgroundColor); + this.imageBuffer.getGraphics().fillRect(0, 0, width, height); + this.imageBuffer.getGraphics().setColor(foregroundColor); } } diff --git a/src/Window.java b/src/Window.java index de1608a..fba4f0e 100644 --- a/src/Window.java +++ b/src/Window.java @@ -1,6 +1,7 @@ import java.awt.*; import java.awt.event.WindowListener; import java.awt.event.WindowStateListener; +import java.awt.image.BufferedImage; public class Window extends Visual { public CustomFrame frame; @@ -10,9 +11,8 @@ public class Window extends Visual { super(); this.frame = new CustomFrame(); this.addWindowStateListener(e -> { - setImageBuffer(frame.getImageBuffer()); - triggerBufferChange(); - repaint(); + this.setSize(getWidth(), getHeight()); + revalidate(); }); } @@ -20,8 +20,12 @@ public class Window extends Visual { public void setSize(int width, int height) { this.frame.setSize(width, height); - this.setImageBuffer(this.frame.getImageBuffer()); super.setSize(width, height); + revalidate(); + } + + public void setFrameImageBuffer(BufferedImage imageBuffer){ + this.frame.setImageBuffer(imageBuffer); } public void setSize(Dimension dimension) {