P2D/source

From Robowiki
< P2D
Revision as of 09:30, 1 July 2010 by RednaxelaBot (talk | contribs) (Using <syntaxhighlight>.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Source

package Resources;

import java.util.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.io.*;

/**
 * P2D Pluggable resource -  Provide basic and fast way to do debugging graphics.
 * http://robowiki.net/w/index.php?title=P2D
 * 
 * @version 1.1.1
 * @author David Alves (original)
 * @author Jacob Litewski (contributor)
 * @author Nat Pavasant (contributor)
 */
public class P2D {
	private static ArrayList<Renderable> renderables = new ArrayList<Renderable>();
	
	private static abstract class Renderable {
		public abstract void render(Graphics2D g);
		
		private static class Circle extends Renderable {
			private final Point2D center;
			private final double radius;
			private final Color color;

			public Circle(Point2D center, double radius, Color color) {
				this.center = center;
				this.radius = radius;
				this.color = color;
			}

			public void render(Graphics2D g) {
				g.setColor(color);
				g.drawOval(	(int) Math.round(center.getX() - radius),
						(int) Math.round(center.getX() - radius),
						(int) Math.round(2 * radius),
						(int) Math.round(2 * radius));
			}
		}
		
		private static class Dot extends Renderable {
			private final Point2D point;
			private final Color color;

			public Dot(Point2D point, Color color) {
				this.point = point;
				this.color = color;
			}
			public void render(Graphics2D g) {
				g.setColor(color);
				g.drawOval(	(int) Math.round(point.getX() - 2d),
						(int) Math.round(point.getY() - 2d),
						      4, 4);
			}
		}
		
		private static class Line extends Renderable {
			private final Point2D p1, p2;
			private final Color color;

			public Line(Point2D p1, Point2D p2, Color color) {
				this.p1 = p1;
				this.p2 = p2;
				this.color = color;
			}

			public void render(Graphics2D g) {
				g.setColor(color);
				g.drawLine(	(int) Math.round(p1.getX()),
						(int) Math.round(p1.getY()),
						(int) Math.round(p2.getX()),
						(int) Math.round(p2.getY()));
			}
		}
		
		private static class Text extends Renderable {
			private final String text;
			private final double x, y;
			private final Color color;
			
			public Text(String text, double x, double y, Color color) {
				this.text = text;
				this.x = x;
				this.y = y;
				this.color = color;
			}

			public void render(Graphics2D g) {
				g.setColor(color);
				g.drawString(text, (float) x, (float) y);
			}
		}
	}

	protected P2D() {}
	
	public static void onPaint(Graphics2D g) {
		for (Renderable r : renderables) {
			r.render(g);
		}

		renderables.clear();
	}
	
	/**
	 * This function need to be call before execute()
	 * in order to clear the buffered renders every tick,
	 * or when you active robot painting, your robot might
	 * throws BufferOverflowException
	 * 
	 * This has no side effect in Robocode version newer than 1.6.1.4
	 * since the onPaint is in event queue which has priority of 5
	 */
	public static void beforeExecute() {
		renderables.clear();
	}
	
	public static void drawLine(Point2D p1, Point2D p2, Color color){
		renderables.add(new Renderable.Line(p1, p2, color));
	}
	
	public static void drawLine(Point2D p1, Point2D p2){
		renderables.add(new Renderable.Line(p1, p2, Color.WHITE));
	}
	
	public static void drawCircle(Point2D center, double radius, Color color){
		renderables.add(new Renderable.Circle(center, radius, color));
	}

	public static void drawCircle(Point2D center, double radius){
		renderables.add(new Renderable.Circle(center, radius, Color.WHITE));
	}
	
	public static void drawPoint(Point2D p1, Color color){
		renderables.add(new Renderable.Dot(p1, color));
	}

	public static void drawPoint(Point2D p1){
		renderables.add(new Renderable.Dot(p1, Color.WHITE));
	}
	
	public static void drawText(String text, double x, double y, Color color){
		renderables.add(new Renderable.Text(text, x, y, color));
	}

	public static void drawText(String text, double x, double y){
		renderables.add(new Renderable.Text(text, x, y, Color.WHITE));
	}
}

Revisions

  • DrawingBot Core - David Alves - Core of this utility
  • 1.0.0 - HACKhalo2 - First version
  • 1.0.1 - HACKhalo2 - Small bugfix to fix NullPointer error
  • 1.1 - Nat
    • Add overloaded method which default color to WHITE
    • Remove some unnecessary properties
    • Use ArrayList instead of Vector
    • Make a use of Java 5 Generic
    • Switch to for-each loop style
    • Add new method to prevent BufferOverflowException
    • Make all method static and protect the class constructor so this class must statically access.
  • 1.1.1 - HACKhalo2
    • Fixed a bug in drawPoint(Point2D p1) where color was null
    • Rearranged Functions to look better
    • Fixed some errors while compiling