Difference between revisions of "P2D/source"
< P2D
Jump to navigation
Jump to search
J Litewski (talk | contribs) m (accually fixed the code) |
RednaxelaBot (talk | contribs) m (Using <syntaxhighlight>.) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
==Source== | ==Source== | ||
− | < | + | <syntaxhighlight>package Resources; |
import java.util.*; | import java.util.*; | ||
Line 43: | Line 43: | ||
private static class Dot extends Renderable { | private static class Dot extends Renderable { | ||
− | private final Point2D | + | private final Point2D point; |
private final Color color; | private final Color color; | ||
− | public Dot(Point2D | + | public Dot(Point2D point, Color color) { |
this.point = point; | this.point = point; | ||
this.color = color; | this.color = color; | ||
Line 135: | Line 135: | ||
} | } | ||
− | public static void drawPoint(Point2D | + | 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 | + | public static void drawPoint(Point2D p1){ |
renderables.add(new Renderable.Dot(p1, Color.WHITE)); | renderables.add(new Renderable.Dot(p1, Color.WHITE)); | ||
} | } | ||
Line 151: | Line 151: | ||
} | } | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
== Revisions == | == Revisions == | ||
Line 165: | Line 165: | ||
** Add new method to prevent BufferOverflowException | ** Add new method to prevent BufferOverflowException | ||
** Make all method static and protect the class constructor so this class must statically access. | ** Make all method static and protect the class constructor so this class must statically access. | ||
− | * [{{fullurl:P2D/source|oldid= | + | * [{{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 | ** Fixed a bug in <code>drawPoint(Point2D p1)</code> where color was null | ||
** Rearranged Functions to look better | ** Rearranged Functions to look better | ||
** Fixed some errors while compiling | ** 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
- Fixed a bug in