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{
Window window = XAMLParser.parse("ui.xml");
assert window != null;
window.revalidate();
Button button = (Button)window.findByName("button1");
window.addWindowListener(new WindowAdapter() {
@Override
@@ -17,7 +18,30 @@ public class Main {
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){
e.printStackTrace();
}

View File

@@ -1,6 +1,6 @@
package guiTree;
import guiTree.events.MouseListener;
import guiTree.events.MouseAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
@@ -8,6 +8,7 @@ import java.awt.image.BufferedImage;
public class Button extends Visual {
private String label;
private Boolean pressed;
public Button() {
this("");
@@ -16,45 +17,17 @@ public class Button extends Visual {
public Button(String label) {
super();
this.label = label;
this.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
if(getBackgroundColor() == Color.BLACK) {
setBackgroundColor(Color.RED);
}
else {
setBackgroundColor(Color.BLACK);
}
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
}
pressed = false;
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
pressed = true;
revalidate();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
public void mouseReleased(MouseEvent mouseEvent) {
pressed = false;
revalidate();
}
});
}
@@ -62,11 +35,32 @@ public class Button extends Visual {
@Override
public void paint(BufferedImage imageBuffer)
{
Graphics g = imageBuffer.getGraphics();
//Get Graphics
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.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) {

View File

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

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) {
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.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();
}
}
}

View File

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