Difference between revisions of "P2D/source"
< P2D
Jump to navigation
Jump to search
J Litewski (talk | contribs) (→Revisions: 1.0.1) |
(create new revision, reformat the revisions) |
||
| Line 7: | Line 7: | ||
import java.io.*; | 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 | ||
| + | * @author David Alves (original) | ||
| + | * @author J. Litewski (contributor) | ||
| + | * @author Nat Pavasant (contributor) | ||
| + | */ | ||
public class P2D { | public class P2D { | ||
| − | + | 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 | + | private final Point2D center; |
| − | double radius; | + | private final double radius; |
| − | Color color; | + | private final Color color; |
| − | public Circle(Point2D | + | |
| + | 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. | + | 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 static class Dot extends Renderable { | ||
| − | Point2D.Double point; | + | private final Point2D.Double point; |
| − | + | private final Color color; | |
| − | + | ||
public Dot(Point2D.Double point, Color color) { | public Dot(Point2D.Double point, Color color) { | ||
this.point = point; | this.point = point; | ||
| − | |||
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. | + | g.drawOval( (int) Math.round(point.getX() - 2d), |
| − | + | (int) Math.round(point.getY() - 2d), | |
| − | + | 4, 4); | |
| − | |||
} | } | ||
} | } | ||
private static class Line extends Renderable { | private static class Line extends Renderable { | ||
| − | Point2D | + | private final Point2D p1, p2; |
| − | double radius; | + | private final double radius; |
| − | Color color; | + | private final Color color; |
| − | public Line(Point2D | + | |
| + | 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. | + | 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 static class Text extends Renderable { | ||
| − | String text; | + | private final String text; |
| − | double x, y; | + | private final double x, y; |
| − | + | private final 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 89: | ||
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) { |
| − | + | for (Renderable r : renderables) { | |
| − | |||
| − | |||
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 | + | 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 | + | 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 drawPoint(Point2D | + | public static void drawPoint(Point2D p1, Color color){ |
renderables.add(new Renderable.Dot(p1, color)); | renderables.add(new Renderable.Dot(p1, color)); | ||
} | } | ||
| Line 113: | Line 135: | ||
renderables.add(new Renderable.Text(text, x, y, color)); | renderables.add(new Renderable.Text(text, x, y, 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){ | ||
| + | renderables.add(new Renderable.Circle(center, radius, Color.WHITE)); | ||
| + | } | ||
| + | |||
| + | public static void drawPoint(Point2D p1){ | ||
| + | renderables.add(new Renderable.Dot(p1, color)); | ||
| + | } | ||
| + | |||
| + | public static void drawText(String text, double x, double y){ | ||
| + | renderables.add(new Renderable.Text(text, x, y, Color.WHITE)); | ||
| + | } | ||
} | } | ||
</pre> | </pre> | ||
| − | ==Revisions== | + | == Revisions == |
| − | + | * [[User:David Alves/DrawingBot|DrawingBot Core]] - [[User:David Alves|David Alves]] - Core of this utility | |
| − | + | * [{{fullurl:P2D/source|oldid=7407}} 1.0.0] - [[User:J Litewski|HACKhalo2]] - First version | |
| − | + | * [{{fullurl:P2D/source|oldid=7429}} 1.0.1] - [[User:J Litewski|HACKhalo2]] - Small bugfix to fix NullPointer error | |
| − | : | + | * [{{fullurl:P2D/source|oldid=7493}} 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. | ||
Revision as of 03:51, 24 May 2009
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
* @author David Alves (original)
* @author J. 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.Double point;
private final Color color;
public Dot(Point2D.Double 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 double radius;
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 drawCircle(Point2D center, double radius, Color color){
renderables.add(new Renderable.Circle(center, radius, color));
}
public static void drawPoint(Point2D p1, Color color){
renderables.add(new Renderable.Dot(p1, color));
}
public static void drawText(String text, double x, double y, Color color){
renderables.add(new Renderable.Text(text, x, y, 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){
renderables.add(new Renderable.Circle(center, radius, Color.WHITE));
}
public static void drawPoint(Point2D p1){
renderables.add(new Renderable.Dot(p1, 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.