Feel free to join the next Helmholtz Hacky Hour #26 on Wednesday, April 21, 2021 from 2PM to 3PM!

Commit 4b26987c authored by Martin Lange's avatar Martin Lange

reworked Graphics to use anonymous canvas class and annotations, removed oracle download links

parent befd08a9
Pipeline #12167 passed with stage
in 33 seconds
......@@ -17,10 +17,10 @@ The graphics system draws the amount of grass per cell by shades of green. Furth
/// file:src/main/java/grassing/sys/Graphics.java
package grassing.sys;
import com.artemis.Aspect;
import com.artemis.BaseSystem;
import com.artemis.BaseEntitySystem;
import com.artemis.ComponentMapper;
import com.artemis.EntitySubscription;
import com.artemis.annotations.All;
import com.artemis.annotations.One;
import com.artemis.annotations.Wire;
import com.artemis.utils.IntBag;
import grassing.comp.*;
......@@ -31,9 +31,9 @@ import grassing.util.MathUtil;
import javax.swing.*;
import java.awt.*;
public class Graphics extends BaseSystem {
private EntitySubscription grasserSubs;
@All(Position.class)
@One({IsGrassing.class, IsSearching.class})
public class Graphics extends BaseEntitySystem {
protected ComponentMapper<Position> mPosition;
protected ComponentMapper<Heading> mHeading;
......@@ -43,7 +43,7 @@ public class Graphics extends BaseSystem {
@Wire Grass grass;
final private int cellSize;
private Canvas canvas;
private JPanel canvas;
public Graphics(int cellSize) {
super();
......@@ -52,13 +52,16 @@ public class Graphics extends BaseSystem {
@Override
protected void initialize() {
grasserSubs = world.getAspectSubscriptionManager()
.get(Aspect.all(Position.class).one(IsGrassing.class, IsSearching.class));
var frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
canvas = new Canvas();
canvas = new JPanel() {
@Override
public void paint(java.awt.Graphics g) {
Graphics.this.paint((Graphics2D)g);
}
};
var dim = new Dimension(grass.grass.width * cellSize, grass.grass.height * cellSize);
canvas.setPreferredSize( dim );
......@@ -72,49 +75,43 @@ public class Graphics extends BaseSystem {
protected void processSystem() {
canvas.paintImmediately(0, 0, grass.grass.width * cellSize, grass.grass.height * cellSize);
}
void paint(Graphics2D g2d) {
Grid.Float grass = this.grass.grass;
int s = cellSize;
class Canvas extends JPanel {
g2d.setBackground(Color.BLACK);
@Override
public void paint(java.awt.Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Grid.Float grass = Graphics.this.grass.grass;
int s = Graphics.this.cellSize;
setBackground(Color.BLACK);
for(int y=0; y<grass.height; y++) {
for(int x=0; x<grass.width; x++) {
g2d.setPaint(new Color(0f, 0.7f * grass.get(x, y), 0f));
g2d.fillRect(x*s, y*s, s, s);
}
for(int y=0; y<grass.height; y++) {
for(int x=0; x<grass.width; x++) {
g2d.setPaint(new Color(0f, 0.7f * grass.get(x, y), 0f));
g2d.fillRect(x*s, y*s, s, s);
}
}
int[] x = new int[3];
int[] y = new int[3];
IntBag grassers = Graphics.this.grasserSubs.getEntities();
for (int i = 0; i < grassers.size(); i++) {
int entity = grassers.get(i);
var pos = mPosition.get(entity);
var head = mHeading.get(entity);
if(mGrassing.has(entity)) {
g2d.setPaint(Color.WHITE);
} else if(mSearching.has(entity)) {
g2d.setPaint(Color.ORANGE);
} else {
g2d.setPaint(Color.MAGENTA);
}
var f = MathUtil.heading(head.angle);
var r = MathUtil.heading(head.angle + 0.5f * (float) Math.PI);
x[0] = (int) ((pos.x + f.x) * s);
y[0] = (int) ((pos.y + f.y) * s);
x[1] = (int) ((pos.x + 0.4f * r.x) * s);
y[1] = (int) ((pos.y + 0.4f * r.y) * s);
x[2] = (int) ((pos.x - 0.4f * r.x) * s);
y[2] = (int) ((pos.y - 0.4f * r.y) * s);
g2d.fillPolygon(x, y, 3);
int[] x = new int[3];
int[] y = new int[3];
IntBag grassers = subscription.getEntities();
for (int i = 0; i < grassers.size(); i++) {
int entity = grassers.get(i);
var pos = mPosition.get(entity);
var head = mHeading.get(entity);
if(mGrassing.has(entity)) {
g2d.setPaint(Color.WHITE);
} else if(mSearching.has(entity)) {
g2d.setPaint(Color.ORANGE);
} else {
g2d.setPaint(Color.MAGENTA);
}
var f = MathUtil.heading(head.angle);
var r = MathUtil.heading(head.angle + 0.5f * (float) Math.PI);
x[0] = (int) ((pos.x + f.x) * s);
y[0] = (int) ((pos.y + f.y) * s);
x[1] = (int) ((pos.x + 0.4f * r.x) * s);
y[1] = (int) ((pos.y + 0.4f * r.y) * s);
x[2] = (int) ((pos.x - 0.4f * r.x) * s);
y[2] = (int) ((pos.y - 0.4f * r.y) * s);
g2d.fillPolygon(x, y, 3);
}
}
}
......
......@@ -17,7 +17,7 @@ After following any of the following sections, you should see a window with gras
The [downloads](https://git.ufz.de/oesa/ecs-tutorial/-/jobs/artifacts/master/download?job=build) contain a file `Grassing.zip`. Extract it and navigate into folder `Grassing/bin`. Double-click or run file `Grassing` (or `Grassing.bat` if you are on Windows).
> This required [Java](https://www.java.com) 11 or newer to be installed on your machine. Simply try running the model, and install Java in case you get a respective error. Download Java [here](https://www.oracle.com/de/java/technologies/javase-downloads.html) or [here](https://adoptopenjdk.net/) (OpenJDK).
> This required [Java](https://www.java.com) 11 or newer to be installed on your machine. Simply try running the model, and install Java in case you get a respective error. Download Java [here](https://adoptopenjdk.net/).
## Browse the plain Java code
......@@ -35,7 +35,7 @@ You can build and run the project with the following command executed inside fol
gradle run
```
> This requires Gradle and Java JDK 11 or newer to be installed on your machine. Download Java [here](https://www.oracle.com/de/java/technologies/javase-downloads.html) or [here](https://adoptopenjdk.net/) (OpenJDK), and Gradle [here](https://gradle.org/releases/).
> This requires Gradle and Java JDK 11 or newer to be installed on your machine. Download Java [here](https://adoptopenjdk.net/), and Gradle [here](https://gradle.org/releases/).
## Clone, extract, build
......@@ -67,4 +67,4 @@ cd code
gradle run
```
> This requires Gradle and the Java JDK 11 or newer to be installed on your machine. Download Java [here](https://www.oracle.com/de/java/technologies/javase-downloads.html) or [here](https://adoptopenjdk.net/) (OpenJDK), and Gradle [here](https://gradle.org/releases/).
> This requires Gradle and the Java JDK 11 or newer to be installed on your machine. Download Java [here](https://adoptopenjdk.net/), and Gradle [here](https://gradle.org/releases/).
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment