[Home]Nfwu/Painter

Robo Home | Nfwu | Changes | Preferences | AllPages

Difference (from prior minor revision) (no other diffs)

Removed: 6,7d5



Removed: 9d6


Removed: 11d7


Removed: 13d8


Removed: 15d9


Removed: 17d10


Removed: 20,21d12



Removed: 23d13


Removed: 25d14


Removed: 33d21


Removed: 36d23


Removed: 38d24


Removed: 40d25


Removed: 44d28


Removed: 46d29


Removed: 48d30


Removed: 52d33


Removed: 54d34


Removed: 56d35


Removed: 64d42


Removed: 66d43


Removed: 68d44


Removed: 71,72d46



Removed: 74d47


Removed: 76d48


Removed: 78d49


Removed: 80d50


Removed: 82d51


Removed: 85,86d53



Removed: 89d55


Removed: 91d56


Removed: 93d57


Removed: 95d58


Removed: 97d59


Removed: 99d60


Removed: 103d63


Removed: 105d64


Removed: 109d67


Removed: 111d68


Removed: 114,115d70



Removed: 117d71


Removed: 119d72


Removed: 121d73


Removed: 124,125d75



Removed: 127d76


Removed: 129d77


Removed: 132,133d79



Removed: 135d80


Removed: 137d81


Removed: 139d82


Changed: 141,144c84

}


}

Removed: 147d86


Removed: 149d87


Removed: 152,153d89



Removed: 155d90


Removed: 157d91


Removed: 160,161d93



Removed: 163d94


Removed: 165d95


Removed: 171d100


Removed: 173d101


Removed: 175d102


Removed: 177d103


Removed: 179d104


Removed: 181d105


Removed: 183d106


Removed: 185d107


Removed: 187d108


Removed: 191d111


Removed: 193d112


Removed: 195d113


Removed: 197d114


Removed: 199d115


Removed: 201d116


Removed: 205d119


Removed: 211d124


Removed: 213d125


Removed: 215d126


Removed: 217d127


Removed: 219d128


Removed: 221d129


Removed: 223d130


Removed: 225d131


Removed: 227d132


Removed: 231d135


Removed: 233d136


Removed: 235d137


Removed: 237d138


Removed: 239d139


Removed: 241d140


Removed: 245d143


Removed: 251d148


Removed: 253d149


Removed: 255d150


Removed: 259d153


Removed: 261d154


Removed: 263d155


Removed: 265d156


Removed: 269d159


Removed: 271d160


Removed: 273d161


Removed: 275d162


Removed: 277d163


Removed: 279d164


Removed: 281d165


Removed: 285d168


Removed: 287d169


Removed: 289d170


Removed: 291d171


Removed: 293d172


Removed: 301,303d179




Removed: 305d180


Removed: 307d181


Removed: 309d182


Removed: 311d183


Removed: 313d184


Removed: 315d185


Removed: 317d186


Removed: 319d187


Removed: 321d188


Removed: 323d189


Removed: 329d194


Removed: 331d195


Removed: 337d200


Removed: 339d201


Removed: 341d202


Removed: 343d203


Removed: 345d204


Removed: 347d205


Removed: 349d206


Removed: 351d207


Removed: 353d208


Removed: 355d209


Removed: 357d210


Removed: 359d211


Removed: 365d216


Removed: 367d217


Removed: 369d218


Removed: 371d219


Removed: 377d224


Removed: 379d225


Removed: 381d226


Removed: 383d227


Removed: 385d228


Removed: 387d229


Removed: 389d230


Removed: 391d231


Removed: 393d232


Removed: 395d233


Removed: 397d234


Removed: 403d239


Removed: 405d240


Removed: 407d241


Removed: 409d242


Removed: 411d243


Removed: 413d244


Removed: 415d245


Removed: 417d246


Removed: 421d249


Removed: 423d250


Removed: 427d253


Removed: 429d254


Changed: 431c256
</pre>
</pre>

Something new I wrote to help with my debugging. Feel free to use, with or without giving credit.

package nfwu;

import robocode.*;
import java.util.Vector;
import java.util.Iterator;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

// Credit goes to David Alves for basic structure from DrawingBot.
// Added multiple layers for drawing on.
//Event Handlers to call from bot...

/*
	public void run(){
		//I want 2 layers
		Painter.init(2);
		//Register first layer to key '1'
		Painter.setupLayer(0, "Enemy Position", java.awt.event.KeyEvent.VK_1); 
		//Register second layer to key 'w'
		Painter.setupLayer(1, "Waves", java.awt.event.KeyEvent.VK_W);
	//other stuff
	}

	public void onKeyPressed(java.awt.event.KeyEvent e){
		if (Painter.onKeyPressed(e)) return;
		// other stuff
	}

	public void onKeyReleased(java.awt.event.KeyEvent e){
		if (Painter.onKeyReleased(e)) return;
		// other stuff
	}

	public void onPaint(java.awt.Graphics2D g){ Painter.onPaint(g); }

*/

// Interactive Display Controls:
//   Holding "key" displays the relevant layer until the key is released.
//   Typing Shift+"key" toggles permanent display of a layer.
//   Holding "H" shows a list of layer names and their keycodes.

public class Painter {
	public final static int HELP_KEY = KeyEvent.VK_H; //The 'h' key.
	public final static float MAGIC_FONT_HEIGHT_VALUE = 12.0f;
	static boolean isDisplayingHelpMessage = true;
	static boolean isDisplayingLayersList = false;
	static boolean isShiftPressed = false;

//Setup stuff
	public static void init(int numberOfLayers){//Call before every round
		System.out.println("This bot has debugging graphics.");
		System.out.println("Hold the '"+KeyEvent.getKeyText(HELP_KEY)+"' key with Paint enabled for help.");
		overlays = new DebugOverlay[numberOfLayers];
		isDisplayingLayersList = false;
		isShiftPressed = false;
	}

	public static void setupLayer(int oid, String name, int key, boolean defaultDisp){ //key must be a valid VK_ code!!!
		overlays[oid] = new DebugOverlay(name, key, defaultDisp);
	}

	public static void setupLayer(int oid, String name, int key){
		setupLayer(oid, name, key, false);
	}

//Draw Stuff (oid = the layer id)
	public static void drawObject(int oid, Renderable r){
		overlays[oid].renderables.add(r);
	}

	public static void drawLineRect(int oid, Color color, double x1, double y1, double x2, double y2){ //Rectangular Coords as input
		drawObject(oid, new Line(x1, y1, x2, y2, color)); 
	}

	public static void drawLinePol(int oid, Color color, double x1, double y1, double len, double ang){ //Relative polar Coords as input
		double x2 = x1 + Math.sin(ang) * len; //NOTE: Robocode Angles... www
		double y2 = y1 + Math.cos(ang) * len; 
		drawObject(oid, new Line(x1, y1, x2, y2, color));
	}	

	public static void drawCircle(int oid, Color color, double x, double y, double radius){
		drawObject(oid, new Circle(x, y, radius, color));
	}

	public static void drawPoint(int oid, Color color, double x, double y){
		drawObject(oid, new Dot(x, y, color));
	}

	public static void drawText(int oid, Color color, double x, double y, String text){
		drawObject(oid, new Text(text, x, y, color));
	}


//Handler Stuff
	public static boolean onKeyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_SHIFT:
			isShiftPressed = true;
			return true;
		case HELP_KEY:
			if (isShiftPressed) isDisplayingHelpMessage = false;
			else isDisplayingLayersList = true;
			return true;
		}

		if (isShiftPressed) return false;
		for (int i=0;i<overlays.length;++i){
			if (overlays[i].key == e.getKeyCode()){
				overlays[i].displayed = true;
				return true;
			}
		}

		return false;
	}



	public static boolean onKeyReleased(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_SHIFT:
			isShiftPressed = false;
			return true;
		case HELP_KEY:
			if (isShiftPressed) isDisplayingHelpMessage = false;
			else isDisplayingLayersList = false;
			return true;
		}

		//Toggle whether or not shift is pressed
		for (int i=0;i<overlays.length;++i){
			if (overlays[i].key == e.getKeyCode()){
				overlays[i].displayed = !overlays[i].displayed;
				return true;
			}
		}

		return false;
	}



	public static void onPaint(Graphics2D g){
		g.setColor(Color.WHITE);
		if (isDisplayingHelpMessage) g.drawString("Hold the '"+KeyEvent.getKeyText(HELP_KEY)+"' key for help.", 0.0f, 0.0f);
		else if (isDisplayingLayersList) g.drawString("List of avaliable debug graphics layers:", 0.0f, 0.0f);

		for (int j=0;j<overlays.length;++j){
			if (isDisplayingLayersList){
				g.setColor(Color.WHITE);
				g.drawString((overlays[j].displayed?"*":"  ")+"["+KeyEvent.getKeyText(overlays[j].key)+"]: "+overlays[j].name, 0.0f, (j+1)*MAGIC_FONT_HEIGHT_VALUE);
			}

			if (!overlays[j].displayed) continue;
			Iterator i = overlays[j].renderables.iterator();
			while(i.hasNext()){
				Renderable r = (Renderable) i.next();
				r.render(g);
			}
			overlays[j].renderables.clear();
		}

		if (isDisplayingLayersList){
			g.setColor(Color.WHITE);
			g.drawString("Holding the key in brackets displays the layer", 0.0f, (overlays.length+1)*MAGIC_FONT_HEIGHT_VALUE);
			g.drawString("Typing Shift+key toggles permanent display of a layer", 0.0f, (overlays.length+2)*MAGIC_FONT_HEIGHT_VALUE);
		}
	}



//Implementation

	static DebugOverlay[] overlays;
	private static class DebugOverlay {
		DebugOverlay(String name, int key, boolean defaultDisp){
			this.name = name;
			this.key = key;
			this.displayed = defaultDisp;
		}
		Vector renderables = new Vector();
		boolean displayed;
		int key; //Keycode to toggle display mode
		String name;
	}



	public static abstract class Renderable {
		public abstract void render(Graphics2D g);
	}

	

	private static class Circle extends Renderable {
		double x, y, radius;
		Color color;
		public Circle(double x, double y, double radius, Color color){
			this.x = x; this.y = y; this.radius = radius;
			this.color = color;
		}
		public void render(Graphics2D g) {
			g.setColor(color);
			g.drawOval((int)Math.round(x - radius), (int)Math.round(y - radius),
				   (int)Math.round(2 * radius), (int)Math.round(2 * radius));
		}
	}

	

	private static class Dot extends Circle {
		public Dot(double x, double y, Color color){
			super(x, y, 2, color); //super(x, y, 2, colour);
		}
	}

	

	private static class Line extends Renderable {
		double x1, y1, x2, y2;
		Color color;
		public Line(double x1, double y1, double x2, double y2, Color color){
			this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2;
			this.color = color;
		}
		public void render(Graphics2D g) {
			g.setColor(color);
			g.drawLine((int)Math.round(x1), (int)Math.round(y1), (int)Math.round(x2), (int)Math.round(y2));
		}
	}

	

	private static class Text extends Renderable {
		String text;
		double x, y;
		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);

		}
	}
}

Robo Home | Nfwu | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited September 6, 2008 1:37 EST by Nfwu (diff)
Search: