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

Binary file not shown.

After

Width:  |  Height:  |  Size: 259 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 B

View File

@@ -10,6 +10,7 @@ public class Main {
try{ try{
Window window = XAMLParser.parse("ui.xml"); Window window = XAMLParser.parse("ui.xml");
assert window != null; assert window != null;
window.revalidate();
Button button = (Button)window.findByName("button1"); Button button = (Button)window.findByName("button1");
window.addWindowListener(new WindowAdapter() { window.addWindowListener(new WindowAdapter() {
@Override @Override
@@ -17,7 +18,30 @@ public class Main {
window.dispose(); window.dispose();
} }
}); });
long now;
long prev = 0;
while(true) {
now = System.currentTimeMillis();
if(now - prev >= 1000) {
int x = button.getLocationX();
int y = button.getLocationY();
if(x + button.getWidth() >= window.getWidth()) {
x = 0;
if(y + button.getHeight() >= window.getHeight()) {
y = 0;
}
else {
y += 30;
}
}
else {
x += 30;
}
button.setLocation(x, y);
prev = now;
window.revalidate();
}
}
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -1,6 +1,6 @@
package guiTree; package guiTree;
import guiTree.events.MouseListener; import guiTree.events.MouseAdapter;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
@@ -8,6 +8,7 @@ import java.awt.image.BufferedImage;
public class Button extends Visual { public class Button extends Visual {
private String label; private String label;
private Boolean pressed;
public Button() { public Button() {
this(""); this("");
@@ -16,45 +17,17 @@ public class Button extends Visual {
public Button(String label) { public Button(String label) {
super(); super();
this.label = label; this.label = label;
this.addMouseListener(new MouseListener() { pressed = false;
@Override this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
if(getBackgroundColor() == Color.BLACK) {
setBackgroundColor(Color.RED);
}
else {
setBackgroundColor(Color.BLACK);
}
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
}
@Override @Override
public void mousePressed(MouseEvent mouseEvent) { public void mousePressed(MouseEvent mouseEvent) {
pressed = true;
revalidate();
} }
@Override @Override
public void mouseEntered(MouseEvent mouseEvent) { public void mouseReleased(MouseEvent mouseEvent) {
pressed = false;
} revalidate();
@Override
public void mouseExited(MouseEvent mouseEvent) {
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
} }
}); });
} }
@@ -62,11 +35,32 @@ public class Button extends Visual {
@Override @Override
public void paint(BufferedImage imageBuffer) public void paint(BufferedImage imageBuffer)
{ {
Graphics g = imageBuffer.getGraphics(); //Get Graphics
g.setColor(this.getBackgroundColor()); Graphics2D g = imageBuffer.createGraphics();
//Set Transparency
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, getWidth(), getHeight());
g.setComposite(AlphaComposite.Src);
//Choose background
if(pressed) {
g.setColor(Color.GRAY);
}
else {
g.setColor(this.getBackgroundColor());
}
//Draw Button
g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 50, 50); g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 50, 50);
g.setColor(this.getForegroundColor()); g.setColor(this.getForegroundColor());
g.drawString(this.label, this.getWidth()/2, this.getHeight()/2);
//Draw Label
int textWidth = g.getFontMetrics().stringWidth(label);
int textHeight = g.getFontMetrics().getHeight();
g.drawString(this.label, this.getWidth()/2 - textWidth/2, this.getHeight()/2 + textHeight/2);
g.dispose();
} }
public void setLabel(String label) { public void setLabel(String label) {

View File

@@ -3,6 +3,7 @@ package guiTree;
import guiTree.events.KeyEventGetter; import guiTree.events.KeyEventGetter;
import guiTree.events.MouseWheelGetter; import guiTree.events.MouseWheelGetter;
import javax.swing.*;
import javax.swing.event.MouseInputListener; import javax.swing.event.MouseInputListener;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@@ -10,7 +11,7 @@ import java.awt.event.KeyListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
public class CustomFrame extends Frame { public class CustomFrame extends JFrame {
private BufferedImage imageBuffer; private BufferedImage imageBuffer;
private Window parentWindow; private Window parentWindow;

View File

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

View File

@@ -4,11 +4,9 @@ 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; import java.awt.image.BufferedImage;
import java.util.List;
public class Window extends Visual { public class Window extends Visual {
public CustomFrame frame; public CustomFrame frame;
private List<Visual> listenerList;
public Window() public Window()
{ {
@@ -25,7 +23,6 @@ public class Window extends Visual {
{ {
this.frame.setSize(width, height); this.frame.setSize(width, height);
super.setSize(width, height); super.setSize(width, height);
revalidate();
} }
public void setFrameImageBuffer(BufferedImage imageBuffer){ public void setFrameImageBuffer(BufferedImage imageBuffer){
@@ -33,6 +30,10 @@ public class Window extends Visual {
this.frame.repaint(); this.frame.repaint();
} }
public void repaint() {
this.frame.repaint();
}
public void setSize(Dimension dimension) { public void setSize(Dimension dimension) {
this.setSize(dimension.width, dimension.height); this.setSize(dimension.width, dimension.height);
} }
@@ -57,6 +58,10 @@ public class Window extends Visual {
frame.dispose(); frame.dispose();
} }
public void repaint(long tm) {
frame.repaint(tm);
}
public void setPositionRelativeTo(Component c){ public void setPositionRelativeTo(Component c){
frame.setLocationRelativeTo(c); frame.setLocationRelativeTo(c);
} }
@@ -66,12 +71,4 @@ public class Window extends Visual {
} }
public void setUndecorated(Boolean undecorated){frame.setUndecorated(undecorated);} public void setUndecorated(Boolean undecorated){frame.setUndecorated(undecorated);}
public void addListener(Visual listener) {
this.listenerList.add(listener);
}
public void removeListener(Visual listener) {
this.listenerList.remove(listener);
}
} }

View File

@@ -0,0 +1,20 @@
package guiTree.events;
import java.awt.event.KeyEvent;
public abstract class KeyAdapter implements KeyListener{
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
}
@Override
public void keyReleased(KeyEvent keyEvent) {
}
}

View File

@@ -0,0 +1,46 @@
package guiTree.events;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
public abstract class MouseAdapter implements MouseListener, MouseWheelListener{
@Override
public void mouseClicked(MouseEvent mouseEvent) {
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
}
@Override
public void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) {
}
}