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

straightedge.test.demo.WorldLetters Maven / Gradle / Ivy

Go to download

Includes 2 main parts: - Path finding through 2D polygons using the A star algorithm and navigation-mesh generation Field of vision / shadows / line of sight / lighting. The basic polygon and point classes are the KPolygon and KPoint. KPolygon contains a list of KPoints for vertices as well as a center (centroid), area, and radius (circular bound or distance from center to furthest point). KPolygon was born out of the need for a more game-oriented and flexible polygon class than the Path2D class in the standard Java library. KPolygon implements java.awt.geom.Shape so it can be easily drawn and filled by Java2D's Graphics2D object. - This API provides path-finding and field-of-vision. For other complex geometric operations such as buffering (fattening and shrinking) and constructive area geometry (intersections and unions) it is recommended to use the excellent Java Topology Suite (JTS). The standard Java2D library also provides the Area class which can be used for some constructive area geometry operations. Note that there is a utility class PolygonConverter that can quickly convert KPolygons to JTS polygons and vice versa.

The newest version!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package straightedge.test.demo;

import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.util.ArrayList;
import straightedge.geom.AABB;
import straightedge.geom.KMultiPolygon;
import straightedge.geom.KPolygon;
import straightedge.geom.PolygonConverter;
import straightedge.geom.vision.OccluderImpl;

/**
 *
 * @author Keith
 */
public class WorldLetters extends World{
	public WorldLetters(Main main){
		super(main);
	}

	public void fillMultiPolygonsList(){
		Container cont = main.getParentFrameOrApplet();
		double contW = cont.getWidth() - (cont.getInsets().right + cont.getInsets().left);
		double contH = cont.getHeight() - (cont.getInsets().top + cont.getInsets().bottom);

		// Add some words as shapes
		Graphics2D g2d = (Graphics2D)AcceleratedImage.createTransparentBufferedImage(1, 1).getGraphics();
		FontRenderContext frc = g2d.getFontRenderContext();
		Font font = g2d.getFont().deriveFont(130.0f);
//			KPolygon border = KPolygon.createRectOblique(0, 0, frameW, 0, 1);
//			KPolygon border2 = KPolygon.createRectOblique(frameW, 0, frameW, frameH, 1);
//			KPolygon border3 = KPolygon.createRectOblique(frameW, frameH, 0, frameH, 1);
//			KPolygon border4 = KPolygon.createRectOblique(0, frameH, 0, 0, 1);
//			allOccluders.add(new OccluderImpl(border));
//			allOccluders.add(new OccluderImpl(border2));
//			allOccluders.add(new OccluderImpl(border3));
//			allOccluders.add(new OccluderImpl(border4));
//			allMultiPolygons.add(new KMultiPolygon(border));
//			allMultiPolygons.add(new KMultiPolygon(border2));
//			allMultiPolygons.add(new KMultiPolygon(border3));
//			allMultiPolygons.add(new KMultiPolygon(border4));

		{
			String text = "Straight";
			TextLayout tl = new TextLayout(text, font, frc);
			FontMetrics fm = cont.getFontMetrics(font);
			double height = fm.getHeight();
			double textW = fm.stringWidth(text);
			Shape textShape = tl.getOutline(null);
			PolygonConverter pc = new PolygonConverter();
			ArrayList polygons = pc.makeKPolygonListFrom(textShape, 1);
			ArrayList multiPolygons = pc.makeKMultiPolygonListFrom(polygons);
			AABB bigAABB = new AABB();
			for (int i = 0; i < polygons.size(); i++){
				bigAABB = bigAABB.union(polygons.get(i).getAABB());
			}
			double translateX = -bigAABB.w()/2f + contW/2f;
			double translateY = -bigAABB.h()/2f + contH/2f;
			for (int i = 0; i < multiPolygons.size(); i++){
				KMultiPolygon polygon = multiPolygons.get(i);
				polygon.translate(translateX, translateY);
			}
			allMultiPolygons.addAll(multiPolygons);
		}
		{
			String text = "Edge";
			TextLayout tl = new TextLayout(text, font, frc);
			FontMetrics fm = cont.getFontMetrics(font);
			double height = fm.getHeight();
			double textW = fm.stringWidth(text);
			Shape textShape = tl.getOutline(null);
			PolygonConverter pc = new PolygonConverter();
			ArrayList polygons = pc.makeKPolygonListFrom(textShape, 1);
			ArrayList multiPolygons = pc.makeKMultiPolygonListFrom(polygons);
			AABB bigAABB = new AABB();
			for (int i = 0; i < polygons.size(); i++){
				bigAABB = bigAABB.union(polygons.get(i).getAABB());
			}
			double translateX = -bigAABB.w()/2f + contW/2f;
			double translateY = +bigAABB.h()/2f + bigAABB.h()/2f + contH/2f;
			for (int i = 0; i < multiPolygons.size(); i++){
				KMultiPolygon polygon = multiPolygons.get(i);
				polygon.translate(translateX, translateY);
			}
			allMultiPolygons.addAll(multiPolygons);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy