[Home]PluggableRobot/Hud

Robo Home | PluggableRobot | Changes | Preferences | AllPages

Showing revision 2
/*
 * PluggableRobot, by Robert J. Walker
 * Home page: http://robowiki.net/cgi-bin/robowiki?PluggableRobot
 * This software is made available under the RoboWiki Public Code License (RWPCL). The full text of
 * the license may be found at http://robowiki.net/cgi-bin/robowiki?RWPCL.
 */
package rjw.pluggablerobot;

import java.awt.Color;
import java.awt.Graphics2D;

import robocode.Robot;

/**
 * Facilitates drawing graphics on the arena by automatically converting between values used in
 * calculations to values used for drawing. The graphics context (Graphics2D) is injected into the
 * Hud just before it is handed to Painters, then removed afterward.
 * @author Robert J. Walker
 */
public class Hud {
	/**
	 * Objects which implement this interface can be passed to PluggableRobot.registerPainter(), and
	 * will get their paint() methods called when it's time to draw the HUD.
	 */
	public interface Painter {
		/**
		 * This method will be called by PluggableRobot when it's time for this object to draw.
		 */
		public void paint(Hud hud, long tick);
	}

	private Graphics2D _g;

	public double _w;
	public double _h;

	/**
	 * Creates a new Hud object.
	 */
	public Hud(Robot bot) {
		_w = bot.getBattleFieldWidth();
		_h = bot.getBattleFieldHeight();
	}

	/**
	 * Draws a point at the given coordinates.
	 */
	public void drawPoint(double x, double y) {
		int x0 = i(x);
		int y0 = i(y);
		_g.drawLine(x0, y0, x0, y0);
	}

	/**
	 * Draws a line between the two indicated points.
	 */
	public void drawLine(double x1, double y1, double x2, double y2) {
		_g.drawLine(i(x1), i(y1), i(x2), i(y2));
	}

	/**
	 * Draws the largest oval that can be contained in a rectangle defined with the given
	 * coordinates and dimensions.
	 */
	public void drawOval(double x, double y, double w, double h) {
		_g.drawOval(i(x), i(y), i(w), i(h));
	}

	/**
	 * Draws a circle with the given center point and radius.
	 */
	public void drawCircle(double x, double y, double r) {
		drawOval(i(x - r), i(y - r), r * 2, r * 2);
	}

	/**
	 * Draws a rectangle with the given position and dimensions.
	 */
	public void drawRect(double x, double y, double w, double h) {
		_g.drawRect(i(x), i(y), i(w), i(h));
	}

	/**
	 * Draws a filled rectangle with the given position and dimensions.
	 */
	public void drawFilledRect(double x, double y, double w, double h) {
		_g.fillRect(i(x), i(y), i(w), i(h));
	}

	/**
	 * Draws a left-aligned String at the given position.
	 */
	public void drawString(String s, double x, double y) {
		_g.drawString(s, i(x), i(y));
	}

	/**
	 * Draws a right-aligned String at the given position.
	 */
	public void drawStringRight(String s, double x, double y) {
		_g.drawString(s, i(x) - _g.getFontMetrics().stringWidth(s), i(y));
	}

	/**
	 * Sets the Hud's current drawing color.
	 */
	public void setColor(Color c) {
		_g.setColor(c);
	}

	/**
	 * Returns the width of the battlefield. This is useful in situations where you don't have
	 * access to the robot object.
	 */
	public double getWidth() {
		return _w;
	}

	/**
	 * Returns the height of the battlefield. This is useful in situations where you don't have
	 * access to the robot object.
	 */
	public double getHeight() {
		return _h;
	}

	/**
	 * Allows PluggableRobot to inject the graphics context into the Hud.
	 */
	public void setContext(Graphics2D g) {
		_g = g;
	}

	/**
	 * Rounds the given double value to the nearest int.
	 */
	private int i(double val) {
		return (int) Math.round(val);
	}
}

Robo Home | PluggableRobot | Changes | Preferences | AllPages
Edit revision 2 of this page | View other revisions | View current revision
Edited September 22, 2007 5:56 EST by RobertWalker (diff)
Search: