2 * Sixth 3D engine demos. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
7 package eu.svjatoslav.sixth.e3d.examples;
9 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
10 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
11 import eu.svjatoslav.sixth.e3d.geometry.Rectangle;
12 import eu.svjatoslav.sixth.e3d.gui.Avatar;
13 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
14 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
15 import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.LookAndFeel;
16 import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.TextEditComponent;
17 import eu.svjatoslav.sixth.e3d.math.Transform;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
20 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
21 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid2D;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.URISyntaxException;
28 import java.util.stream.Collectors;
30 public class TextEditorDemo2 {
32 public static void main(final String[] args) {
34 new TextEditorDemo2().build();
35 } catch (URISyntaxException e) {
37 } catch (IOException e) {
42 public void build() throws URISyntaxException, IOException {
43 final ViewFrame viewFrame = new ViewFrame();
44 final ViewPanel viewPanel = viewFrame.getViewPanel();
46 final ShapeCollection shapeCollection = viewFrame.getViewPanel()
47 .getRootShapeCollection();
49 setAvatarLocation(viewPanel);
51 addGrid(shapeCollection);
53 addCity(viewPanel, shapeCollection);
56 private void addCity(ViewPanel viewPanel, ShapeCollection shapeCollection) throws URISyntaxException, IOException {
58 for (int z = -citySize; z< citySize; z+= 1000 ){
59 for (int x = -citySize; x< citySize; x+= 1000 ){
60 addBuilding(viewPanel, shapeCollection, x, z);
66 private static void addGrid(ShapeCollection shapeCollection) {
67 final Transform transform = new Transform(new Point3D(0, 100, 0), 0,
70 final Rectangle rectangle = new Rectangle(10000);
71 final LineAppearance appearance = new LineAppearance(10, new Color(
74 shapeCollection.addShape(new Grid2D(transform, rectangle, 50, 50, appearance));
78 private void addBuilding(ViewPanel viewPanel, ShapeCollection shapeCollection, double x, double z) throws URISyntaxException, IOException {
79 Transform transform = new Transform(new Point3D(x, -390, z-200));
80 addTextEditor(viewPanel, shapeCollection, transform);
82 transform = new Transform(new Point3D(x, -390, z+200),Math.PI, 0);
83 addTextEditor(viewPanel, shapeCollection, transform);
85 transform = new Transform(new Point3D(x-200, -390, z),Math.PI/2, 0);
86 addTextEditor(viewPanel, shapeCollection, transform);
88 transform = new Transform(new Point3D(x+200, -390, z),Math.PI/2*3f, 0);
89 addTextEditor(viewPanel, shapeCollection, transform);
92 private void addTextEditor(ViewPanel viewPanel, ShapeCollection shapeCollection, Transform transform) throws IOException {
93 LookAndFeel lookAndFeel = getLookAndFeel();
95 final TextEditComponent textEditor = new TextEditComponent(
98 new Point2D(400, 1000),
102 String text = getResourceFileAsString("demo.txt");
104 textEditor.setText(text);
105 textEditor.goToLine((int)(Math.random()*200f));
106 shapeCollection.addShape(textEditor);
109 private LookAndFeel getLookAndFeel() {
110 LookAndFeel lookAndFeel = new LookAndFeel();
111 lookAndFeel.background = new Color(20, 20, 50, 150);
112 lookAndFeel.tabStopBackground = lookAndFeel.background;
113 lookAndFeel.foreground = new Color(150, 150, 255,250);
117 static String getResourceFileAsString(String fileName) throws IOException {
118 ClassLoader classLoader = ClassLoader.getSystemClassLoader();
119 try (InputStream is = classLoader.getResourceAsStream(fileName)) {
120 if (is == null) return null;
121 try (InputStreamReader isr = new InputStreamReader(is);
122 BufferedReader reader = new BufferedReader(isr)) {
123 return reader.lines().collect(Collectors.joining(System.lineSeparator()));
128 private static void setAvatarLocation(ViewPanel viewPanel) {
129 Avatar avatar = viewPanel.getAvatar();
130 avatar.setLocation(new Point3D(500, -300, -800));
131 avatar.setAngleXZ(0.6);
132 avatar.setAngleYZ(-0.5);