You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.7 KiB
78 lines
1.7 KiB
package com.jotuntech.sketcher.common;
|
|
|
|
import java.awt.Image;
|
|
import java.awt.image.ImageObserver;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.Set;
|
|
|
|
import javax.swing.event.ChangeEvent;
|
|
import javax.swing.event.ChangeListener;
|
|
|
|
public class Canvas implements ImageObserver {
|
|
private int width, height;
|
|
private TwoWayHashMap<Integer, Layer> layerMap;
|
|
private Set<ImageObserver> observers;
|
|
private volatile boolean drawing;
|
|
|
|
public Canvas(int width, int height) {
|
|
this.drawing = false;
|
|
|
|
this.width = width;
|
|
this.height = height;
|
|
|
|
this.layerMap = new TwoWayHashMap<Integer, Layer>();
|
|
|
|
this.observers = new LinkedHashSet<ImageObserver>();
|
|
|
|
getLayerMap().addChangeListener(new ChangeListener() {
|
|
public void stateChanged(ChangeEvent e) {
|
|
addImageObserverToLayers();
|
|
}
|
|
});
|
|
}
|
|
|
|
public int getWidth() {
|
|
return width;
|
|
}
|
|
|
|
public int getHeight() {
|
|
return height;
|
|
}
|
|
|
|
public TwoWayHashMap<Integer, Layer> getLayerMap() {
|
|
return layerMap;
|
|
}
|
|
|
|
public void addImageObserver(ImageObserver observer) {
|
|
observers.add(observer);
|
|
}
|
|
|
|
public Set<ImageObserver> getImageObservers() {
|
|
return observers;
|
|
}
|
|
|
|
public ImageObserver getImageObserver() {
|
|
return this;
|
|
}
|
|
|
|
private void addImageObserverToLayers() {
|
|
for(Layer l : getLayerMap().values()) {
|
|
l.addImageObserver(this);
|
|
}
|
|
}
|
|
|
|
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
|
|
for(ImageObserver o : observers) {
|
|
o.imageUpdate(img, infoflags, x, y, width, height);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void setDrawing(boolean drawing) {
|
|
this.drawing = drawing;
|
|
}
|
|
|
|
public boolean isDrawing() {
|
|
return drawing;
|
|
}
|
|
}
|
|
|