mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-10-12 01:45:18 +02:00
Beispielprojekte
This commit is contained in:
27
examples/zm_forces/Attractor.java
Normal file
27
examples/zm_forces/Attractor.java
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
import schule.ngb.zm.*;
|
||||
|
||||
public class Attractor extends Mover {
|
||||
|
||||
private int mass = 0;
|
||||
|
||||
public Attractor( int x, int pY, int pMass ) {
|
||||
this(x, pY, pMass, new Vector());
|
||||
}
|
||||
|
||||
public Attractor( int x, int pY, int pMass, Vector pVelocity ) {
|
||||
super(x, pY, pVelocity);
|
||||
mass = pMass;
|
||||
|
||||
setFillColor(YELLOW);
|
||||
}
|
||||
|
||||
public void attract( Mover pMover ) {
|
||||
if( pMover != this ) {
|
||||
Vector force = new Vector(this.x, this.y);
|
||||
force.sub(pMover.getX(), pMover.getY()).scale(mass*Gravity.G).limit(0, 50*Gravity.G);
|
||||
pMover.applyForce(force);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
50
examples/zm_forces/Gravity.java
Normal file
50
examples/zm_forces/Gravity.java
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import schule.ngb.zm.*;
|
||||
|
||||
public class Gravity extends Zeichenmaschine {
|
||||
|
||||
|
||||
public static final int G = 1;
|
||||
|
||||
|
||||
private LinkedList<Mover> movers = new LinkedList<>();
|
||||
|
||||
private LinkedList<Attractor> attractors = new LinkedList<>();
|
||||
|
||||
public void setup(){
|
||||
for( int i = 0; i < 10; i++ ) {
|
||||
Mover m = new Mover(random(10, width-10), random(10, height-10));
|
||||
movers.add(m);
|
||||
shapes.add(m);
|
||||
}
|
||||
|
||||
attractors.add(new Attractor(width/2, height/2, 10));
|
||||
shapes.add(attractors.get(0));
|
||||
}
|
||||
|
||||
public void update( double delta ) {
|
||||
Attractor mouseFollow = attractors.get(0);
|
||||
mouseFollow.moveTo(mouseX, mouseY);
|
||||
|
||||
for( Attractor a: attractors ) {
|
||||
for( Mover m: movers ) {
|
||||
a.attract(m);
|
||||
}
|
||||
}
|
||||
|
||||
for( Mover m: movers ) {
|
||||
m.update(delta);
|
||||
}
|
||||
shapes.clear();
|
||||
}
|
||||
|
||||
public void mouseClicked() {
|
||||
for( Mover m: movers ) {
|
||||
m.moveTo(random(10, width-10), random(10, height-10));
|
||||
m.setVelocity(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
42
examples/zm_forces/Mover.java
Normal file
42
examples/zm_forces/Mover.java
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
import schule.ngb.zm.*;
|
||||
import schule.ngb.zm.shapes.*;
|
||||
|
||||
public class Mover extends Circle implements Updatable {
|
||||
|
||||
private Vector velocity;
|
||||
private Vector acceleration = new Vector();
|
||||
|
||||
public Mover( int x, int pY ) {
|
||||
this(x, pY, new Vector());
|
||||
}
|
||||
|
||||
public Mover( int x, int pY, Vector pVelocity ) {
|
||||
super(x, pY, 10);
|
||||
this.velocity = pVelocity.copy();
|
||||
}
|
||||
|
||||
public void setVelocity(double dx, double dy) {
|
||||
velocity.set(dx, dy);
|
||||
}
|
||||
|
||||
public void applyForce( Vector force ) {
|
||||
acceleration.add(force);
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void update( double delta ) {
|
||||
acceleration.scale(delta);
|
||||
velocity.add(acceleration);
|
||||
acceleration.scale(0.0);
|
||||
|
||||
this.x += velocity.x;
|
||||
this.y += velocity.y;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
55
examples/zm_forces/package.bluej
Normal file
55
examples/zm_forces/package.bluej
Normal file
@@ -0,0 +1,55 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Attractor
|
||||
dependency1.to=Gravity
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Gravity
|
||||
dependency2.to=Mover
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Gravity
|
||||
dependency3.to=Attractor
|
||||
dependency3.type=UsesDependency
|
||||
editor.fx.0.height=728
|
||||
editor.fx.0.width=1037
|
||||
editor.fx.0.x=95
|
||||
editor.fx.0.y=53
|
||||
objectbench.height=94
|
||||
objectbench.width=776
|
||||
package.divider.horizontal=0.6
|
||||
package.divider.vertical=0.8305369127516778
|
||||
package.editor.height=488
|
||||
package.editor.width=661
|
||||
package.editor.x=374
|
||||
package.editor.y=158
|
||||
package.frame.height=660
|
||||
package.frame.width=800
|
||||
package.numDependencies=3
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.height=60
|
||||
readme.name=@README
|
||||
readme.width=48
|
||||
readme.x=10
|
||||
readme.y=10
|
||||
target1.height=70
|
||||
target1.name=Mover
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.width=120
|
||||
target1.x=380
|
||||
target1.y=220
|
||||
target2.height=70
|
||||
target2.name=Attractor
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.width=120
|
||||
target2.x=380
|
||||
target2.y=350
|
||||
target3.height=70
|
||||
target3.name=Gravity
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.width=120
|
||||
target3.x=120
|
||||
target3.y=120
|
||||
Reference in New Issue
Block a user