mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 13:40:04 +00:00
displaying element but erasing everything on location/size change
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
151
src/Visual.java
151
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<Visual> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user