Code cleanup and formatting.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / RainingNumbersDemo.java
1 /*
2  * Sixth 3D engine demos. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  */
8
9 package eu.svjatoslav.sixth.e3d.examples;
10
11 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
12 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
13 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
14 import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
15 import eu.svjatoslav.sixth.e3d.math.Transform;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
20
21 import java.util.Collection;
22 import java.util.Random;
23
24 public class RainingNumbersDemo implements ViewRenderListener {
25
26     private static final int NUMBERS_COUNT = 1000;
27     private final static int AREA = 600;
28     private final static int AREA_HALF = AREA / 2;
29
30     public static void main(final String[] args) {
31         new RainingNumbersDemo().run();
32     }
33
34     @Override
35     public boolean beforeRender(final ViewPanel viewPanel,
36                                 final int millisecondsSinceLastFrame) {
37
38         final Collection<AbstractShape> shapes = viewPanel
39                 .getRootShapeCollection().getShapes();
40
41         final double translateAmount = millisecondsSinceLastFrame / 50d;
42
43         shapes.stream().filter(shape -> shape instanceof TextCanvas).forEach(shape -> {
44             final TextCanvas block = (TextCanvas) shape;
45             final Point3D location = block.getLocation();
46             location.translateY(translateAmount);
47
48             if (location.y > AREA_HALF)
49                 location.y -= AREA;
50         });
51
52         return true;
53     }
54
55     private void run() {
56         final ViewFrame viewFrame = new ViewFrame();
57
58         final ShapeCollection geometryCollection = viewFrame.getViewPanel()
59                 .getRootShapeCollection();
60
61         Random random = new Random();
62
63         for (int i = 0; i < NUMBERS_COUNT; i++) {
64             final Point3D location = new Point3D((Math.random() * AREA)
65                     - AREA_HALF, (Math.random() * AREA) - AREA_HALF,
66                     (Math.random() * AREA) - AREA_HALF);
67
68             final Color color = new Color(Math.random(), Math.random(),
69                     Math.random(), Math.random());
70
71             final TextCanvas textCanvas = new TextCanvas(
72                     new Transform(location), String.valueOf(random.nextInt(10)), color,
73                     Color.TRANSPARENT);
74
75             geometryCollection.addShape(textCanvas);
76         }
77
78         viewFrame.getViewPanel().addViewUpdateListener(this);
79     }
80 }