Difference between revisions of "P2D/source"

From Robowiki
< P2D
Jump to navigation Jump to search
(added new revision to David Alves, who created DrawingBot)
m (Using <syntaxhighlight>.)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==Source==
 
==Source==
<pre>package Resources;
+
<syntaxhighlight>package Resources;
  
 
import java.util.*;
 
import java.util.*;
Line 7: Line 7:
 
import java.io.*;
 
import java.io.*;
  
//Point2D Pluggable Resource
+
/**
//Version 1.0.0
+
* 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 {
 
public class P2D {
public static Vector renderables;
+
private static ArrayList<Renderable> renderables = new ArrayList<Renderable>();
 
 
 
private static abstract class Renderable {
 
private static abstract class Renderable {
Line 17: Line 23:
 
 
 
private static class Circle extends Renderable {
 
private static class Circle extends Renderable {
Point2D.Double center;
+
private final Point2D center;
double radius;
+
private final double radius;
Color color;
+
private final Color color;
public Circle(Point2D.Double center, double radius, Color color) {
+
 
 +
public Circle(Point2D center, double radius, Color color) {
 
this.center = center;
 
this.center = center;
 
this.radius = radius;
 
this.radius = radius;
 
this.color = color;
 
this.color = color;
 
}
 
}
 +
 
public void render(Graphics2D g) {
 
public void render(Graphics2D g) {
 
g.setColor(color);
 
g.setColor(color);
g.drawOval( (int)Math.round(center.x - radius),
+
g.drawOval( (int) Math.round(center.getX() - radius),
(int)Math.round(center.y - radius),
+
(int) Math.round(center.getX() - radius),
(int)Math.round(2 * radius),
+
(int) Math.round(2 * radius),
(int)Math.round(2 * radius));
+
(int) Math.round(2 * radius));
 
}
 
}
 
}
 
}
 
 
 
private static class Dot extends Renderable {
 
private static class Dot extends Renderable {
Point2D.Double point;
+
private final Point2D point;
double radius;
+
private final Color color;
Color color;
+
 
public Dot(Point2D.Double point, Color color) {
+
public Dot(Point2D point, Color color) {
 
this.point = point;
 
this.point = point;
this.radius = 2;
 
 
this.color = color;
 
this.color = color;
 
}
 
}
 
public void render(Graphics2D g) {
 
public void render(Graphics2D g) {
 
g.setColor(color);
 
g.setColor(color);
g.drawOval( (int)Math.round(point.x - radius),
+
g.drawOval( (int) Math.round(point.getX() - 2d),
(int)Math.round(point.y - radius),
+
(int) Math.round(point.getY() - 2d),
(int)Math.round(2 * radius),
+
      4, 4);
(int)Math.round(2 * radius));
 
 
}
 
}
 
}
 
}
 
 
 
private static class Line extends Renderable {
 
private static class Line extends Renderable {
Point2D.Double p1, p2;
+
private final Point2D p1, p2;
double radius;
+
private final Color color;
Color color;
+
 
public Line(Point2D.Double p1, Point2D.Double p2, Color color) {
+
public Line(Point2D p1, Point2D p2, Color color) {
 
this.p1 = p1;
 
this.p1 = p1;
 
this.p2 = p2;
 
this.p2 = p2;
 
this.color = color;
 
this.color = color;
 
}
 
}
 +
 
public void render(Graphics2D g) {
 
public void render(Graphics2D g) {
 
g.setColor(color);
 
g.setColor(color);
g.drawLine( (int)Math.round(p1.x),
+
g.drawLine( (int) Math.round(p1.getX()),
(int)Math.round(p1.y),
+
(int) Math.round(p1.getY()),
(int)Math.round(p2.x),
+
(int) Math.round(p2.getX()),
(int)Math.round(p2.y));
+
(int) Math.round(p2.getY()));
 
}
 
}
 
}
 
}
 
 
 
private static class Text extends Renderable {
 
private static class Text extends Renderable {
String text;
+
private final String text;
double x, y;
+
private final double x, y;
double radius;
+
private final Color color;
Color color;
 
 
 
 
public Text(String text, double x, double y, Color color) {
 
public Text(String text, double x, double y, Color color) {
Line 82: Line 88:
 
this.color = color;
 
this.color = color;
 
}
 
}
 +
 
public void render(Graphics2D g) {
 
public void render(Graphics2D g) {
 
g.setColor(color);
 
g.setColor(color);
g.drawString(text, (float)x, (float)y);
+
g.drawString(text, (float) x, (float) y);
 
}
 
}
 
}
 
}
 
}
 
}
 +
 +
protected P2D() {}
 
 
public void onPaint(Graphics2D g) {
+
public static void onPaint(Graphics2D g) {
Iterator i = renderables.iterator();
+
for (Renderable r : renderables) {
while(i.hasNext()) {
 
Renderable r = (Renderable) i.next();
 
 
r.render(g);
 
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();
 
renderables.clear();
 
}
 
}
 
 
public static void drawLine(Point2D.Double p1, Point2D.Double p2, Color color){
+
public static void drawLine(Point2D p1, Point2D p2, Color color){
 
renderables.add(new Renderable.Line(p1, p2, color));
 
renderables.add(new Renderable.Line(p1, p2, color));
 
}
 
}
 
 
public static void drawCircle(Point2D.Double center, double radius, Color 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));
 
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.Double p1, Color color){
+
public static void drawPoint(Point2D p1, Color color){
 
renderables.add(new Renderable.Dot(p1, color));
 
renderables.add(new Renderable.Dot(p1, color));
 +
}
 +
 +
public static void drawPoint(Point2D p1){
 +
renderables.add(new Renderable.Dot(p1, Color.WHITE));
 
}
 
}
 
 
Line 114: Line 147:
 
}
 
}
  
 +
public static void drawText(String text, double x, double y){
 +
renderables.add(new Renderable.Text(text, x, y, Color.WHITE));
 +
}
 
}
 
}
</pre>
+
</syntaxhighlight>
==Revisions==
+
 
;Core --[[User:David Alves]]
+
== Revisions ==
:Created [[User:David Alves/DrawingBot|DrawingBot]]
+
* [[User:David Alves/DrawingBot|DrawingBot Core]] - [[User:David Alves|David Alves]] - Core of this utility
;1.0.0 --[[User:J Litewski|HACKhalo2]]
+
* [{{fullurl:P2D/source|oldid=7407}} 1.0.0] - [[User:J Litewski|HACKhalo2]] - First version
:First Version
+
* [{{fullurl:P2D/source|oldid=7429}} 1.0.1] - [[User:J Litewski|HACKhalo2]] - Small bugfix to fix NullPointer error
 +
* [{{fullurl:P2D/source|oldid=7444}} 1.1] - [[User:Nat|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.
 +
* [{{fullurl:P2D/source|oldid=7571}} 1.1.1] - [[User:J Litewski|HACKhalo2]]
 +
** Fixed a bug in <code>drawPoint(Point2D p1)</code> where color was null
 +
** Rearranged Functions to look better
 +
** Fixed some errors while compiling

Latest revision as of 09:30, 1 July 2010

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