All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.plantuml.logo.TurtleGraphicsPane Maven / Gradle / Ivy

There is a newer version: 1.2024.8
Show newest version
// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.
/* +=======================================================================
 * |
 * |      PlantUML : a free UML diagram generator
 * |
 * +=======================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 *
 * If you like this project or if you find it useful, you can support us at:
 *
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://plantuml.com/liberapay (only 1€ per month!)
 * https://plantuml.com/paypal
 *
 *
 * PlantUML is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License V2.
 *
 * THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
 * LICENSE ("AGREEMENT"). [GNU General Public License V2]
 *
 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
 * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
 *
 * You may obtain a copy of the License at
 *
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * PlantUML can occasionally display sponsored or advertising messages. Those
 * messages are usually generated on welcome or error images and never on
 * functional diagrams.
 * See https://plantuml.com/professional if you want to remove them
 *
 * Images (whatever their format : PNG, SVG, EPS...) generated by running PlantUML
 * are owned by the author of their corresponding sources code (that is, their
 * textual description in PlantUML language). Those images are not covered by
 * this GPL v2 license.
 *
 * The generated images can then be used without any reference to the GPL v2 license.
 * It is not even necessary to stipulate that they have been generated with PlantUML,
 * although this will be appreciated by the PlantUML team.
 *
 * There is an exception : if the textual description in PlantUML language is also covered
 * by any license, then the generated images are logically covered
 * by the very same license.
 *
 * This is the IGY distribution (Install GraphViz by Yourself).
 * You have to install GraphViz and to setup the GRAPHVIZ_DOT environment variable
 * (see https://plantuml.com/graphviz-dot )
 *
 * Icons provided by OpenIconic :  https://useiconic.com/open
 * Archimate sprites provided by Archi :  http://www.archimatetool.com
 * Stdlib AWS provided by https://github.com/milo-minderbinder/AWS-PlantUML
 * Stdlib Icons provided https://github.com/tupadr3/plantuml-icon-font-sprites
 * ASCIIMathML (c) Peter Jipsen http://www.chapman.edu/~jipsen
 * ASCIIMathML (c) David Lippman http://www.pierce.ctc.edu/dlippman
 * CafeUndZopfli ported by Eugene Klyuchnikov https://github.com/eustas/CafeUndZopfli
 * Brotli (c) by the Brotli Authors https://github.com/google/brotli
 * Themes (c) by Brett Schwarz https://github.com/bschwarz/puml-themes
 * Twemoji (c) by Twitter at https://twemoji.twitter.com/
 *
 */
package net.sourceforge.plantuml.logo;

import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.HColor;
import net.sourceforge.plantuml.klimt.color.HColorSet;
import net.sourceforge.plantuml.klimt.color.HColors;
import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
import net.sourceforge.plantuml.klimt.font.UFont;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.klimt.shape.ULine;
import net.sourceforge.plantuml.klimt.shape.UPolygon;
import net.sourceforge.plantuml.klimt.sprite.SpriteContainerEmpty;

class TurtleGraphicsPane {
	final private double width;
	final private double height;
	private double x;
	private double y;
	private double turtleDirection = 90;
	private boolean penIsDown = true;
	private boolean showTurtle = true;
	private HColor penColor = HColors.BLACK;
	private List lines = new ArrayList();
	private List colors = new ArrayList<>();

	private String message;

	public TurtleGraphicsPane(int width, int height) {
		this.width = width;
		this.height = height;
		clearScreen();
	}

	public void clearScreen() {
		x = width / 2;
		y = -height / 2;
		turtleDirection = 90;
		lines.clear();
		colors.clear();
	}

	private double dtor(double degrees) {
		return degrees * Math.PI / 180.0;
	}

	private void drawTurtle(UGraphic ug) {
		if (showTurtle == false) {
			return;
		}
		final UPolygon poly = new UPolygon();
		double size = 2;
		double deltax = 4.5 * size;
		poly.addPoint(0 * size - deltax, 0);
		poly.addPoint(0 * size - deltax, -2 * size);
		poly.addPoint(1 * size - deltax, -2 * size);
		poly.addPoint(1 * size - deltax, -4 * size);
		poly.addPoint(2 * size - deltax, -4 * size);
		poly.addPoint(2 * size - deltax, -6 * size);
		poly.addPoint(3 * size - deltax, -6 * size);
		poly.addPoint(3 * size - deltax, -8 * size);
		poly.addPoint(4 * size - deltax, -8 * size);
		poly.addPoint(4 * size - deltax, -9 * size);
		poly.addPoint(5 * size - deltax, -9 * size);
		poly.addPoint(5 * size - deltax, -8 * size);
		poly.addPoint(6 * size - deltax, -8 * size);
		poly.addPoint(6 * size - deltax, -6 * size);
		poly.addPoint(7 * size - deltax, -6 * size);
		poly.addPoint(7 * size - deltax, -4 * size);
		poly.addPoint(8 * size - deltax, -4 * size);
		poly.addPoint(8 * size - deltax, -2 * size);
		poly.addPoint(9 * size - deltax, -2 * size);
		poly.addPoint(9 * size - deltax, 0);
		poly.addPoint(0 * size - deltax, 0);
		final double angle = -dtor(turtleDirection - 90);
		poly.rotate(angle);
		// ug.setAntiAliasing(false);
		final HColorSet htmlColorSet = HColorSet.instance();
		final HColor turtleColor1 = htmlColorSet.getColorOrWhite("OliveDrab");
		final HColor turtleColor2 = htmlColorSet.getColorOrWhite("MediumSpringGreen");

		ug.apply(turtleColor1).apply(turtleColor2.bg()).apply(new UTranslate(x, -y)).draw(poly);
		// ug.setAntiAliasing(true);
	}

	public void showTurtle() {
		showTurtle = true;
	}

	public void hideTurtle() {
		showTurtle = false;
	}

	public void setPenColor(HColor newPenColor) {
		penColor = newPenColor;
	}

	void addLine(double x1, double y1, double x2, double y2) {
		lines.add(new Rectangle2D.Double(x1, y1, x2, y2));
		colors.add(penColor);
	}

	public void forward(double distance) {
		double angle = dtor(turtleDirection);
		double newX = x + distance * Math.cos(angle);
		double newY = y + distance * Math.sin(angle);
		if (penIsDown) {
			addLine(x, y, newX, newY);
			x = newX;
			y = newY;
		} else {
			x = newX;
			y = newY;
		}
	}

	public void back(double distance) {
		forward(-distance);
	}

	public void left(double turnAngle) {
		turtleDirection += turnAngle;
		while (turtleDirection > 360) {
			turtleDirection -= 360;
		}
		while (turtleDirection < 0) {
			turtleDirection += 360;
		}
	}

	public void right(double turnAngle) {
		left(-turnAngle);
	}

	public void penUp() {
		penIsDown = false;
	}

	public void penDown() {
		penIsDown = true;
	}

	public void paint(UGraphic ug) {
		int n = lines.size();

		for (int i = 0; i < n; i++) {
			final HColor color = colors.get(i);
			final Rectangle2D.Double r = lines.get(i);
			final ULine line = new ULine(r.width - r.x, -r.height + r.y);
			ug.apply(color).apply(new UTranslate(r.x, -r.y)).draw(line);

		}
		drawTurtle(ug);
		if (message != null) {
			final FontConfiguration font = FontConfiguration.blackBlueTrue(UFont.build("", Font.PLAIN, 14));
			final TextBlock text = Display.create(message).create(font, HorizontalAlignment.LEFT,
					new SpriteContainerEmpty());
			final XDimension2D dim = text.calculateDimension(ug.getStringBounder());
			final double textHeight = dim.getHeight();
			text.drawU(ug.apply(UTranslate.dy((height - textHeight))));
		}
	}

	public void message(String messageText) {
		this.message = messageText;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy