diff options
Diffstat (limited to 'Занимательное программирование/2/8_turtle/core/src')
| -rw-r--r-- | Занимательное программирование/2/8_turtle/core/src/net/caraus/turtle/Turtle.java | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/Занимательное программирование/2/8_turtle/core/src/net/caraus/turtle/Turtle.java b/Занимательное программирование/2/8_turtle/core/src/net/caraus/turtle/Turtle.java new file mode 100644 index 0000000..ff7fc99 --- /dev/null +++ b/Занимательное программирование/2/8_turtle/core/src/net/caraus/turtle/Turtle.java @@ -0,0 +1,154 @@ +package net.caraus.turtle; + +import java.util.ArrayList; + +import com.badlogic.gdx.ApplicationAdapter; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup; +import com.badlogic.gdx.scenes.scene2d.ui.SelectBox; +import com.badlogic.gdx.scenes.scene2d.ui.Skin; +import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.ui.TextField; +import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; +import com.badlogic.gdx.utils.ScreenUtils; + +public class Turtle extends ApplicationAdapter { + SpriteBatch batch; + Texture img; + TextButton goButton; + TextButton moveButton; + TextButton applyButton; + HorizontalGroup container; + Stage stage; + TextField textField; + SelectBox<String> selectBox; + Sprite turtle; + int turtleX = 110; + int turtleY = 110; + boolean isDrawing = false; + ShapeRenderer shapeRenderer; + ArrayList<Vector2> beginnings = new ArrayList<Vector2>(); + ArrayList<Vector2> endings = new ArrayList<Vector2>(); + + @Override + public void create () { + batch = new SpriteBatch(); + img = new Texture("turtle.png"); + turtle = new Sprite(img); + + stage = new Stage(); + stage.getViewport().getCamera().position.y = 20; + stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false); + Gdx.input.setInputProcessor(stage); + container = new HorizontalGroup(); + container.setFillParent(true); + stage.addActor(container); + + Skin skin = new Skin(Gdx.files.internal("skin/clean-crispy-ui.json")); + + textField = new TextField(null, skin); + container.addActor(textField); + + selectBox = new SelectBox<String>(skin); + selectBox.setItems("Oben", "Unten", "Links", "Rechts"); + container.addActor(selectBox); + + goButton = new TextButton("Zeichnen", skin); + goButton.addListener(new ClickListener() { + @Override + public void clicked(InputEvent event, float x, float y) + { + isDrawing = true; + } + }); + container.addActor(goButton); + + moveButton = new TextButton("Bewegen", skin); + moveButton.addListener(new ClickListener() { + @Override + public void clicked(InputEvent event, float x, float y) + { + isDrawing = false; + } + }); + container.addActor(moveButton); + + applyButton = new TextButton("Anwenden", skin); + applyButton.addListener(new ClickListener() { + @Override + public void clicked(InputEvent event, float x, float y) + { + try { + int value = Integer.parseInt(textField.getText()); + + if (isDrawing) { + beginnings.add(new Vector2(turtleX, turtleY)); + } + switch (selectBox.getSelected()) { + case "Oben": + turtleY += value; + break; + case "Unten": + turtleY -= value; + break; + case "Links": + turtleX -= value; + break; + case "Rechts": + turtleX += value; + break; + default: + break; + } + if (isDrawing) { + endings.add(new Vector2(turtleX, turtleY)); + } + } catch (NumberFormatException e) { + } + } + }); + container.addActor(applyButton); + + shapeRenderer = new ShapeRenderer(); + shapeRenderer.setColor(0, 0, 0, 1); + } + + @Override + public void render () { + ScreenUtils.clear(1, 1, 1, 1); + + stage.act(Gdx.graphics.getDeltaTime()); + stage.draw(); + + shapeRenderer.begin(ShapeType.Filled); + + for (int i = 0; i < beginnings.size(); ++i) { + shapeRenderer.rectLine(beginnings.get(i), endings.get(i), 5); + } + + shapeRenderer.end(); + + batch.begin(); + + turtle.setPosition(turtleX, turtleY); + turtle.draw(batch); + + batch.end(); + } + + @Override + public void dispose () { + batch.dispose(); + img.dispose(); + stage.dispose(); + shapeRenderer.dispose(); + } +} |
