Difference between revisions of "P2D/source"
< P2D
Jump to navigation
Jump to search
J Litewski (talk | contribs) (added new revision to David Alves, who created DrawingBot) |
RednaxelaBot (talk | contribs) m (Using <syntaxhighlight>.) |
||
(10 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
==Source== | ==Source== | ||
− | < | + | <syntaxhighlight>package Resources; |
import java.util.*; | import java.util.*; | ||
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.1 | ||
+ | * @author David Alves (original) | ||
+ | * @author Jacob 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 | + | private final Point2D point; |
− | + | 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; | ||
} | } | ||
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; |
− | + | 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 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) { |
− | + | 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 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 | + | 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)); | ||
+ | } | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
− | ==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=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
- Fixed a bug in