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();
|
Graphics g = imageBuffer.getGraphics();
|
||||||
g.setColor(Color.BLUE);
|
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()
|
public CustomFrame()
|
||||||
{
|
{
|
||||||
super();
|
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)
|
public CustomFrame(String name)
|
||||||
@@ -24,20 +14,8 @@ public class CustomFrame extends Frame {
|
|||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void setImageBuffer(BufferedImage imageBuffer) {
|
||||||
public void setSize(Dimension d)
|
this.imageBuffer = imageBuffer;
|
||||||
{
|
|
||||||
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
|
@Override
|
||||||
|
|||||||
@@ -16,5 +16,23 @@ public class Main {
|
|||||||
|
|
||||||
Button button = new Button();
|
Button button = new Button();
|
||||||
win.addVisual(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.awt.image.BufferedImage;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Visual {
|
public class Visual {
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------
|
||||||
|
Tree Elements
|
||||||
|
---------------------------------------------------------------------*/
|
||||||
private List<Visual> children;
|
private List<Visual> children;
|
||||||
private Visual parent;
|
private Visual parent;
|
||||||
private BufferedImage imageBuffer;
|
private BufferedImage imageBuffer;
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------
|
||||||
|
Attributes
|
||||||
|
---------------------------------------------------------------------*/
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
private int height;
|
||||||
private int locationX;
|
private int locationX;
|
||||||
private int locationY;
|
private int locationY;
|
||||||
|
private Color backgroundColor;
|
||||||
|
private Color foregroundColor;
|
||||||
|
private Boolean valid;
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------
|
||||||
|
Constructors
|
||||||
|
---------------------------------------------------------------------*/
|
||||||
public Visual() {
|
public Visual() {
|
||||||
this.children = new ArrayList<>();
|
this.children = new ArrayList<>();
|
||||||
this.parent = null;
|
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()
|
public int getWidth()
|
||||||
{
|
{
|
||||||
return this.width;
|
return this.width;
|
||||||
@@ -31,17 +53,51 @@ public class Visual {
|
|||||||
public void setSize(int width, int height){
|
public void setSize(int width, int height){
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
|
|
||||||
// if(this.parent != null) {
|
initializeImageBuffer(width, height);
|
||||||
// this.imageBuffer = this.parent.getImageBuffer();
|
|
||||||
// }
|
this.valid = false;
|
||||||
this.repaint();
|
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() {
|
private void calculateInitialSize() {
|
||||||
this.width = this.parent.getWidth() - this.getLocationX();
|
this.width = 20;
|
||||||
this.height = this.parent.getHeight() - this.getLocationY();
|
this.height = 20;
|
||||||
this.imageBuffer = this.parent.getImageBuffer().getSubimage(this.locationX, this.locationY, this.width, this.height);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void calculateInitialLocation(){
|
private void calculateInitialLocation(){
|
||||||
@@ -49,13 +105,20 @@ public class Visual {
|
|||||||
this.locationY = 200;
|
this.locationY = 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------
|
||||||
|
Tree Methods
|
||||||
|
---------------------------------------------------------------------*/
|
||||||
|
|
||||||
public void addVisual(Visual child) {
|
public void addVisual(Visual child) {
|
||||||
this.children.add(child);
|
this.children.add(child);
|
||||||
child.setParent(this);
|
child.setParent(this);
|
||||||
child.calculateInitialLocation();
|
child.calculateInitialLocation();
|
||||||
child.calculateInitialSize();
|
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)
|
private void setParent(Visual parent)
|
||||||
@@ -72,56 +135,44 @@ public class Visual {
|
|||||||
this.parent.handleNotification();
|
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()
|
public void callRepaint()
|
||||||
{
|
{
|
||||||
this.repaint();
|
this.paint(this.imageBuffer);
|
||||||
|
this.parent.revalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLocationX(){
|
public void revalidate() {
|
||||||
return this.locationX;
|
initializeImageBuffer(width, height);
|
||||||
}
|
this.paint(imageBuffer);
|
||||||
|
for(Visual v:children){
|
||||||
public int getLocationY(){
|
if(!v.valid){
|
||||||
return this.locationY;
|
v.paint(v.imageBuffer);
|
||||||
}
|
}
|
||||||
|
this.imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
|
||||||
public void setLocationX(int locationX){
|
v.valid = true;
|
||||||
this.locationX = locationX;
|
}
|
||||||
}
|
this.valid = true;
|
||||||
|
if(!(this instanceof Window)){
|
||||||
public void setLocationY(int locationY){
|
this.parent.revalidate();
|
||||||
this.locationY = locationY;
|
}
|
||||||
|
else{
|
||||||
|
Window window = (Window)this;
|
||||||
|
window.setFrameImageBuffer(this.imageBuffer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(BufferedImage imageBuffer) {
|
public void paint(BufferedImage imageBuffer) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setImageBuffer(BufferedImage imageBuffer){
|
/*--------------------------------------------------------------------
|
||||||
this.imageBuffer = imageBuffer;
|
Helper Methods
|
||||||
}
|
---------------------------------------------------------------------*/
|
||||||
|
|
||||||
private BufferedImage getImageBuffer() {
|
private void initializeImageBuffer(int width, int height){
|
||||||
return this.imageBuffer;
|
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
|
||||||
}
|
this.imageBuffer.getGraphics().setColor(backgroundColor);
|
||||||
|
this.imageBuffer.getGraphics().fillRect(0, 0, width, height);
|
||||||
public void triggerBufferChange() {
|
this.imageBuffer.getGraphics().setColor(foregroundColor);
|
||||||
for(Visual v: children){
|
|
||||||
v.imageBuffer = this.imageBuffer;
|
|
||||||
v.triggerBufferChange();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.WindowListener;
|
import java.awt.event.WindowListener;
|
||||||
import java.awt.event.WindowStateListener;
|
import java.awt.event.WindowStateListener;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
public class Window extends Visual {
|
public class Window extends Visual {
|
||||||
public CustomFrame frame;
|
public CustomFrame frame;
|
||||||
@@ -10,9 +11,8 @@ public class Window extends Visual {
|
|||||||
super();
|
super();
|
||||||
this.frame = new CustomFrame();
|
this.frame = new CustomFrame();
|
||||||
this.addWindowStateListener(e -> {
|
this.addWindowStateListener(e -> {
|
||||||
setImageBuffer(frame.getImageBuffer());
|
this.setSize(getWidth(), getHeight());
|
||||||
triggerBufferChange();
|
revalidate();
|
||||||
repaint();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,8 +20,12 @@ public class Window extends Visual {
|
|||||||
public void setSize(int width, int height)
|
public void setSize(int width, int height)
|
||||||
{
|
{
|
||||||
this.frame.setSize(width, height);
|
this.frame.setSize(width, height);
|
||||||
this.setImageBuffer(this.frame.getImageBuffer());
|
|
||||||
super.setSize(width, height);
|
super.setSize(width, height);
|
||||||
|
revalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFrameImageBuffer(BufferedImage imageBuffer){
|
||||||
|
this.frame.setImageBuffer(imageBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSize(Dimension dimension) {
|
public void setSize(Dimension dimension) {
|
||||||
|
|||||||
Reference in New Issue
Block a user