mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 14:43:33 +02:00
Listeniterationen Threadsafe gemacht
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package schule.ngb.zm;
|
package schule.ngb.zm;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.Graphics2D;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class DrawableLayer extends Layer {
|
public class DrawableLayer extends Layer {
|
||||||
|
|
||||||
protected LinkedList<Drawable> drawables = new LinkedList<>();
|
protected List<Drawable> drawables = new LinkedList<>();
|
||||||
|
|
||||||
protected boolean clearBeforeDraw = true;
|
protected boolean clearBeforeDraw = true;
|
||||||
|
|
||||||
@@ -43,7 +45,8 @@ public class DrawableLayer extends Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
synchronized( drawables ) {
|
synchronized( drawables ) {
|
||||||
for( Drawable d : drawables ) {
|
List<Drawable> it = Collections.unmodifiableList(drawables);
|
||||||
|
for( Drawable d : it ) {
|
||||||
if( d.isVisible() ) {
|
if( d.isVisible() ) {
|
||||||
d.draw(drawing);
|
d.draw(drawing);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
package schule.ngb.zm;
|
package schule.ngb.zm;
|
||||||
|
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SuppressWarnings( "unused" )
|
||||||
public class Spielemaschine extends Zeichenmaschine {
|
public class Spielemaschine extends Zeichenmaschine {
|
||||||
|
|
||||||
private LinkedList<Drawable> drawables;
|
private LinkedList<Drawable> drawables;
|
||||||
@@ -78,9 +83,10 @@ public class Spielemaschine extends Zeichenmaschine {
|
|||||||
@Override
|
@Override
|
||||||
public final void update( double delta ) {
|
public final void update( double delta ) {
|
||||||
synchronized( updatables ) {
|
synchronized( updatables ) {
|
||||||
for( Updatable updatable : updatables ) {
|
List<Updatable> it = Collections.unmodifiableList(updatables);
|
||||||
if( updatable.isActive() ) {
|
for( Updatable u: it ) {
|
||||||
updatable.update(delta);
|
if( u.isActive() ) {
|
||||||
|
u.update(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,12 +98,21 @@ public class Spielemaschine extends Zeichenmaschine {
|
|||||||
public final void draw() {
|
public final void draw() {
|
||||||
mainLayer.clear();
|
mainLayer.clear();
|
||||||
synchronized( drawables ) {
|
synchronized( drawables ) {
|
||||||
for( Drawable drawable : drawables ) {
|
List<Drawable> it = Collections.unmodifiableList(drawables);
|
||||||
if( drawable.isVisible() ) {
|
for( Drawable d: it ) {
|
||||||
drawable.draw(mainLayer.getGraphics());
|
if( d.isVisible() ) {
|
||||||
|
d.draw(mainLayer.getGraphics());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class GraphicsLayer extends Layer {
|
||||||
|
|
||||||
|
public Graphics2D getGraphics() {
|
||||||
|
return drawing;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package schule.ngb.zm;
|
package schule.ngb.zm;
|
||||||
|
|
||||||
|
import schule.ngb.zm.shapes.Shape;
|
||||||
|
|
||||||
import java.awt.Canvas;
|
import java.awt.Canvas;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Toolkit;
|
import java.awt.Toolkit;
|
||||||
import java.awt.image.BufferStrategy;
|
import java.awt.image.BufferStrategy;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Eine Leinwand ist die Hauptkomponente einer Zeichenmaschine. Sie besteht aus
|
* Eine Leinwand ist die Hauptkomponente einer Zeichenmaschine. Sie besteht aus
|
||||||
@@ -195,7 +199,8 @@ public class Zeichenleinwand extends Canvas {
|
|||||||
|
|
||||||
public void updateLayers( double delta ) {
|
public void updateLayers( double delta ) {
|
||||||
synchronized( layers ) {
|
synchronized( layers ) {
|
||||||
for( Layer layer : layers ) {
|
List<Layer> it = List.copyOf(layers);
|
||||||
|
for( Layer layer : it ) {
|
||||||
layer.update(delta);
|
layer.update(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -232,7 +237,8 @@ public class Zeichenleinwand extends Canvas {
|
|||||||
public void draw( Graphics graphics ) {
|
public void draw( Graphics graphics ) {
|
||||||
Graphics2D g2d = (Graphics2D) graphics.create();
|
Graphics2D g2d = (Graphics2D) graphics.create();
|
||||||
synchronized( layers ) {
|
synchronized( layers ) {
|
||||||
for( Layer layer : layers ) {
|
List<Layer> it = List.copyOf(layers);
|
||||||
|
for( Layer layer : it ) {
|
||||||
layer.draw(g2d);
|
layer.draw(g2d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,7 +262,8 @@ public class Zeichenleinwand extends Canvas {
|
|||||||
g2d.clearRect(0, 0, getWidth(), getHeight());
|
g2d.clearRect(0, 0, getWidth(), getHeight());
|
||||||
|
|
||||||
synchronized( layers ) {
|
synchronized( layers ) {
|
||||||
for( Layer layer : layers ) {
|
List<Layer> it = List.copyOf(layers);
|
||||||
|
for( Layer layer : it ) {
|
||||||
layer.draw(g2d);
|
layer.draw(g2d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,7 @@ import schule.ngb.zm.anim.AnimationFacade;
|
|||||||
import schule.ngb.zm.anim.Easing;
|
import schule.ngb.zm.anim.Easing;
|
||||||
|
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.DoubleUnaryOperator;
|
import java.util.function.DoubleUnaryOperator;
|
||||||
|
|
||||||
public class ShapesLayer extends Layer {
|
public class ShapesLayer extends Layer {
|
||||||
@@ -45,12 +42,12 @@ public class ShapesLayer extends Layer {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.List<Shape> getShapes() {
|
public List<Shape> getShapes() {
|
||||||
return shapes;
|
return shapes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <ST extends Shape> java.util.List<ST> getShapes( Class<ST> shapeClass ) {
|
public <ST extends Shape> List<ST> getShapes( Class<ST> shapeClass ) {
|
||||||
java.util.List<ST> result = new LinkedList<>();
|
List<ST> result = new LinkedList<>();
|
||||||
for( Shape s : shapes ) {
|
for( Shape s : shapes ) {
|
||||||
if( shapeClass.isInstance(s) ) {
|
if( shapeClass.isInstance(s) ) {
|
||||||
result.add((ST) s);
|
result.add((ST) s);
|
||||||
@@ -60,7 +57,7 @@ public class ShapesLayer extends Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void add( Shape... shapes ) {
|
public void add( Shape... shapes ) {
|
||||||
synchronized( shapes ) {
|
synchronized( this.shapes ) {
|
||||||
for( Shape s : shapes ) {
|
for( Shape s : shapes ) {
|
||||||
this.shapes.add(s);
|
this.shapes.add(s);
|
||||||
}
|
}
|
||||||
@@ -68,7 +65,7 @@ public class ShapesLayer extends Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void add( Collection<Shape> shapes ) {
|
public void add( Collection<Shape> shapes ) {
|
||||||
synchronized( shapes ) {
|
synchronized( this.shapes ) {
|
||||||
for( Shape s : shapes ) {
|
for( Shape s : shapes ) {
|
||||||
this.shapes.add(s);
|
this.shapes.add(s);
|
||||||
}
|
}
|
||||||
@@ -76,7 +73,7 @@ public class ShapesLayer extends Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void remove( Shape... shapes ) {
|
public void remove( Shape... shapes ) {
|
||||||
synchronized( shapes ) {
|
synchronized( this.shapes ) {
|
||||||
for( Shape s : shapes ) {
|
for( Shape s : shapes ) {
|
||||||
this.shapes.remove(s);
|
this.shapes.remove(s);
|
||||||
}
|
}
|
||||||
@@ -84,7 +81,7 @@ public class ShapesLayer extends Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void remove( Collection<Shape> shapes ) {
|
public void remove( Collection<Shape> shapes ) {
|
||||||
synchronized( shapes ) {
|
synchronized( this.shapes ) {
|
||||||
for( Shape s : shapes ) {
|
for( Shape s : shapes ) {
|
||||||
this.shapes.remove(s);
|
this.shapes.remove(s);
|
||||||
}
|
}
|
||||||
@@ -99,16 +96,16 @@ public class ShapesLayer extends Layer {
|
|||||||
|
|
||||||
public void showAll() {
|
public void showAll() {
|
||||||
synchronized( shapes ) {
|
synchronized( shapes ) {
|
||||||
for( Shape pShape : shapes ) {
|
for( Shape s : shapes ) {
|
||||||
pShape.show();
|
s.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void hideAll() {
|
public void hideAll() {
|
||||||
synchronized( shapes ) {
|
synchronized( shapes ) {
|
||||||
for( Shape pShape : shapes ) {
|
for( Shape s : shapes ) {
|
||||||
pShape.hide();
|
s.hide();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,9 +144,10 @@ public class ShapesLayer extends Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
synchronized( shapes ) {
|
synchronized( shapes ) {
|
||||||
for( Shape pShape : shapes ) {
|
List<Shape> it = List.copyOf(shapes);
|
||||||
if( pShape.isVisible() ) {
|
for( Shape s : it ) {
|
||||||
pShape.draw(drawing);
|
if( s.isVisible() ) {
|
||||||
|
s.draw(drawing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user