working with dirty bit, and manual update

This commit is contained in:
rmaco
2020-03-14 20:59:43 +02:00
parent c0f062c565
commit 35fa03b35c
13 changed files with 270 additions and 143 deletions

View File

@@ -34,6 +34,8 @@ public class Visual {
private Color backgroundColor;
private Color foregroundColor;
private Boolean active;
private Boolean dirty;
private Boolean pressed;
/*--------------------------------------------------------------------
@@ -48,71 +50,42 @@ public class Visual {
this.backgroundColor = Color.WHITE;
this.foregroundColor = Color.BLACK;
this.dirty = true;
this.active = this instanceof Window;
}
/*--------------------------------------------------------------------
Attributes Methods
Attributes Setters
---------------------------------------------------------------------*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
public void setSize(Integer width, Integer height){
this.width = width;
this.height = height;
initializeImageBuffer(width, height);
initializeImageBuffer();
this.revalidate();
}
public int getLocationX(){
return this.locationX;
}
public int getLocationY(){
return this.locationY;
this.dirty = true;
}
public void setLocation(Integer x, Integer y){
this.locationX = x;
this.locationY = y;
this.revalidate();
}
public Color getBackgroundColor() {
return backgroundColor;
this.dirty = true;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
this.revalidate();
}
public Color getForegroundColor() {
return foregroundColor;
this.dirty = true;
}
public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor;
this.revalidate();
this.dirty = true;
}
private void calculateInitialSize() {
@@ -133,6 +106,39 @@ public class Visual {
}
}
/*--------------------------------------------------------------------
Attributes Getters
---------------------------------------------------------------------*/
public String getName() {
return name;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
public int getLocationX(){
return this.locationX;
}
public int getLocationY(){
return this.locationY;
}
public Color getBackgroundColor() {
return backgroundColor;
}
public Color getForegroundColor() {
return foregroundColor;
}
/*--------------------------------------------------------------------
Tree Methods
@@ -159,10 +165,10 @@ public class Visual {
child.setParent(this);
child.calculateInitialLocation();
child.calculateInitialSize();
child.imageBuffer = new BufferedImage(child.getWidth(), child.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
child.active = true;
child.revalidate();
if(this.active) {
child.activate();
}
}
public void removeVisual(Visual child) {
@@ -171,8 +177,8 @@ public class Visual {
child.setSize(0, 0);
child.setLocation(0, 0);
child.imageBuffer = null;
child.active = false;
revalidate();
child.deactivate();
this.dirty = true;
}
private void setParent(Visual parent)
@@ -189,28 +195,36 @@ public class Visual {
this.parent.handleNotification();
}
public void callRepaint()
{
this.paint(this.imageBuffer);
this.parent.revalidate();
private void repaint() {
this.paint(imageBuffer);
if(this.dirty) {
this.revalidate();
return;
}
for(Visual v: children) {
v.repaint();
imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
}
}
public void revalidate() {
if(!this.active){
return;
}
initializeImageBuffer(width, height);
clearImageBuffer();
this.paint(imageBuffer);
for(Visual v:children){
for (Visual v : children) {
v.repaint();
this.imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
}
this.dirty = false;
if(!(this instanceof Window)){
this.parent.revalidate();
return;
}
else{
Window window = (Window)this;
window.setFrameImageBuffer(this.imageBuffer);
}
Window window = (Window)this;
window.setFrameImageBuffer(imageBuffer);
window.repaint();
}
public void paint(BufferedImage imageBuffer) {
@@ -261,18 +275,19 @@ public class Visual {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseClicked(mouseEvent);
}
dirty = true;
}
}
void mouseReleased(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() + offsetX;
int mouseY = mouseEvent.getY() + offsetY;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY < v.getLocationY() &&
mouseX < v.getWidth() &&
mouseY > v.getHeight()) {
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseReleased(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
@@ -281,18 +296,19 @@ public class Visual {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseReleased(mouseEvent);
}
dirty = true;
}
}
void mousePressed(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() + offsetX;
int mouseY = mouseEvent.getY() + offsetY;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY < v.getLocationY() &&
mouseX < v.getWidth() &&
mouseY > v.getHeight()) {
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mousePressed(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
@@ -301,18 +317,19 @@ public class Visual {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mousePressed(mouseEvent);
}
dirty = true;
}
}
void mouseEntered(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() + offsetX;
int mouseY = mouseEvent.getY() + offsetY;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY < v.getLocationY() &&
mouseX < v.getWidth() &&
mouseY > v.getHeight()) {
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()){
front = false;
v.mouseEntered(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
@@ -321,18 +338,19 @@ public class Visual {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseEntered(mouseEvent);
}
dirty = true;
}
}
void mouseExited(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() + offsetX;
int mouseY = mouseEvent.getY() + offsetY;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY < v.getLocationY() &&
mouseX < v.getWidth() &&
mouseY > v.getHeight()) {
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseExited(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
@@ -341,18 +359,19 @@ public class Visual {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseExited(mouseEvent);
}
dirty = true;
}
}
void mouseDragged(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() + offsetX;
int mouseY = mouseEvent.getY() + offsetY;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY < v.getLocationY() &&
mouseX < v.getWidth() &&
mouseY > v.getHeight()) {
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseDragged(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
@@ -361,38 +380,40 @@ public class Visual {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseDragged(mouseEvent);
}
dirty = true;
}
}
void mouseMoved(MouseEvent mouseEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseEvent.getX() + offsetX;
int mouseY = mouseEvent.getY() + offsetY;
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY < v.getLocationY() &&
mouseX < v.getWidth() &&
mouseY > v.getHeight()) {
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseMoved(mouseEvent, offsetX + v.locationX, offsetY + locationY);
v.mouseMoved(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
}
}
if(front) {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseMoved(mouseEvent);
}
dirty = true;
}
}
void mouseWheelMoved(MouseWheelEvent mouseWheelEvent, int offsetX, int offsetY) {
boolean front = true;
int mouseX = mouseWheelEvent.getX() + offsetX;
int mouseY = mouseWheelEvent.getY() + offsetY;
int mouseX = mouseWheelEvent.getX() - offsetX;
int mouseY = mouseWheelEvent.getY() - offsetY;
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY < v.getLocationY() &&
mouseX < v.getWidth() &&
mouseY > v.getHeight()) {
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()) {
front = false;
v.mouseWheelMoved(mouseWheelEvent, offsetX + v.locationX, offsetY + v.locationY);
}
@@ -401,15 +422,39 @@ public class Visual {
for(MouseWheelListener mouseWheelListener: mouseWheelListeners) {
mouseWheelListener.mouseWheelMoved(mouseWheelEvent);
}
dirty = true;
}
}
/*--------------------------------------------------------------------
Helper Methods
---------------------------------------------------------------------*/
private void initializeImageBuffer(Integer width, Integer height){
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
this.imageBuffer.getGraphics().setColor(backgroundColor);
this.imageBuffer.getGraphics().fillRect(0, 0, width, height);
private void initializeImageBuffer(){
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
clearImageBuffer();
}
private void clearImageBuffer() {
Graphics g = this.imageBuffer.getGraphics();
g.setColor(backgroundColor);
g.fillRect(0, 0, width, height);
g.dispose();
}
private void activate() {
this.active = true;
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for(Visual child: children) {
child.activate();
}
this.dirty = true;
}
private void deactivate() {
this.active = false;
this.imageBuffer = null;
for(Visual child: children) {
child.deactivate();
}
}
}