[TeamTalk 126]: [663] TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder: 1) Changed the names of some classes to improve consistency and readability.
tk@edam.speech.cs.cmu.edu
tk at edam.speech.cs.cmu.edu
Thu Jul 19 22:49:23 EDT 2007
An HTML attachment was scrubbed...
URL: http://mailman.srv.cs.cmu.edu/pipermail/teamtalk-developers/attachments/20070719/cb32ad94/attachment-0001.html
-------------- next part --------------
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/AbstractDecoderTool.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/AbstractDecoderTool.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/AbstractDecoderTool.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -2,10 +2,10 @@
public abstract class AbstractDecoderTool extends AbstractTool {
- protected DrawingCanvas canvas;
+ protected MapCanvas canvas;
protected String tooltip;
- protected AbstractDecoderTool(DrawingCanvas canvas, String name, String tooltip) {
+ protected AbstractDecoderTool(MapCanvas canvas, String name, String tooltip) {
super(canvas, name);
this.canvas = canvas;
this.tooltip = tooltip;
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/AbstractTool.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/AbstractTool.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/AbstractTool.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -4,14 +4,14 @@
public abstract class AbstractTool implements Tool {
- protected DrawingCanvas canvas;
+ protected MapCanvas canvas;
protected String name;
public String getName() {
return name;
}
- protected AbstractTool(DrawingCanvas canvas, String name) {
+ protected AbstractTool(MapCanvas canvas, String name) {
this.canvas = canvas;
this.name = name;
}
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/BotTracker.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/BotTracker.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/BotTracker.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -4,13 +4,13 @@
import java.awt.Graphics;
public class BotTracker {
- protected DrawingCanvas canvas;
+ protected MapCanvas canvas;
protected final static int size = 3;
protected HashMap<String, Shape> bots = new HashMap<String, Shape>();
protected HashMap<Shape, String> names = new HashMap<Shape, String>();
- BotTracker(DrawingCanvas c) {
+ BotTracker(MapCanvas c) {
canvas = c;
}
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/ButtonTool.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/ButtonTool.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/ButtonTool.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -20,7 +20,7 @@
public class ButtonTool extends AbstractDecoderTool {
/** Creates a new instance of ButtonTool */
- protected ButtonTool(DrawingCanvas canvas, String name, String tooltip) {
+ protected ButtonTool(MapCanvas canvas, String name, String tooltip) {
super(canvas, name, tooltip);
this.canvas = canvas;
}
@@ -35,5 +35,5 @@
public void mouseMovedTo(Point p) {}
public void cancel() {}
- protected DrawingCanvas canvas;
+ protected MapCanvas canvas;
}
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DecoderInterpreter.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DecoderInterpreter.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DecoderInterpreter.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -24,10 +24,10 @@
protected Reader reader;
protected PrintStream outStream;
protected PrintStream errStream;
- protected DrawingPad pad;
+ protected PenDecoderDisplay pad;
/** Creates a new instance of DecoderInterpreter */
- public DecoderInterpreter(JConsole jconsole, DrawingPad pad) {
+ public DecoderInterpreter(JConsole jconsole, PenDecoderDisplay pad) {
this.jconsole = jconsole;
this.pad = pad;
reader = jconsole.getIn();
Deleted: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvas.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvas.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvas.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -1,505 +0,0 @@
-package edu.cmu.ravenclaw.pendecoder;
-
-import java.awt.Color;
-import java.awt.Dimension;
-import java.awt.Graphics;
-import java.awt.Graphics2D;
-import java.awt.Point;
-import java.awt.Image;
-import java.awt.Rectangle;
-import java.awt.image.BufferedImage;
-import java.awt.image.DataBuffer;
-import java.awt.image.IndexColorModel;
-import java.awt.image.WritableRaster;
-import java.util.*;
-import java.io.*;
-import java.awt.event.*;
-import java.awt.geom.AffineTransform;
-import java.awt.geom.NoninvertibleTransformException;
-import java.util.EventListener;
-import javax.swing.*;
-import javax.imageio.ImageIO;
-
-public class DrawingCanvas extends JComponent implements Scrollable {
-
- protected DrawingPad drawingPad = null;
- protected DrawingCanvasListener drawingCanvasListener;
- static public String opTraderFrameHolder; //[2005-09-30] (tk): hack
-
- /**
- * The width and height of the area that the map represents (in centimeters)
- */
- protected float native_width, native_height;
-
- /**
- * A vector from the upper left corner of the map to the world origin (in centimeters)
- */
- protected float x_origin, y_origin;
-
- /**
- * The width and height of the actual map image (in pixels)
- */
- protected float image_width_in_pixels, image_height_in_pixels;
-
- public DrawingCanvas() {
- colorGenerator = new ColorGenerator(new Color[] {
- Color.BLACK, //clear space
- Color.DARK_GRAY, //hash marks
- Color.GRAY, //unknown space
- Color.GREEN, //obstructed space
- Color.YELLOW //highlights
- });
-
- String mapfile = null;
- try {
- Properties properties = new Properties();
- properties.load(new FileInputStream("map.properties"));
- mapfile = properties.getProperty("edu.cmu.ravenclaw.pendecoder.mapfile");
- if(mapfile == null) throw new Exception("mapfile is missing");
- String widthstr = properties.getProperty("edu.cmu.ravenclaw.pendecoder.mapwidth");
- if(widthstr == null) throw new Exception("mapwidth is missing");
- String heightstr = properties.getProperty("edu.cmu.ravenclaw.pendecoder.mapheight");
- native_width = Float.parseFloat(widthstr);
- native_height = Float.parseFloat(heightstr);
- } catch (NumberFormatException e) {
- System.err.println("Can't parse double:" + e.toString());
- System.exit(1);
- } catch (Exception e) {
- System.err.println("Problem with properties" + e.toString());
- System.exit(1);
- }
- try {
- backImage = new MapBufferedImage(ImageIO.read(new File(mapfile)));
- image_width_in_pixels = backImage.getWidth(this);
- image_height_in_pixels = backImage.getHeight(this);
- setPreferredSize(new Dimension((int)image_width_in_pixels,
- (int)image_height_in_pixels));
- } catch(Exception e) {
- System.err.println("Problem reading or converting PNG: " + e.toString());
- System.exit(1);
- }
- drawingCanvasListener = makeCanvasListener();
- addMouseListener((MouseListener) listener);
- addMouseMotionListener((MouseMotionListener) listener);
- }
-
- public DrawingCanvas(Image backImage, DrawingPad drawingPad) {
- //calling factory method
- System.err.println("scribble w/backimage");
- drawingCanvasListener = makeCanvasListener();
- addMouseListener((MouseListener) listener);
- addMouseMotionListener((MouseMotionListener) listener);
- backImage = new MapBufferedImage(backImage);
- this.drawingPad = drawingPad;
- }
-
- public DrawingCanvas(DrawingPad drawingPad) {
- this.drawingPad = drawingPad;
- }
-
- public void setTool(Tool tool) {
- drawingCanvasListener.setTool(tool);
- }
-
- public Tool getTool() {
- return drawingCanvasListener.getTool();
- }
-
- /**
- * factory method
- */
- protected DrawingCanvasListener makeCanvasListener() {
- return new DrawingCanvasListener(this);
- }
-
- public void saySelectBot(String name) {
- drawingPad.saySelectBot(name);
- }
-
- public void zoomOut() {
- int width = getPreferredSize().width;
- int height = getPreferredSize().height;
- JViewport viewport = (JViewport) getParent();
- Dimension d = viewport.getSize();
- Point center = new Point(
- viewport.getViewPosition().x+
- viewport.getExtentSize().width/2,
- viewport.getViewPosition().y+
- viewport.getExtentSize().height/2);
- setPreferredSize(new Dimension(width/2,height/2));
- setSize(getPreferredSize());
- revalidate();
- viewport.setViewPosition(new Point((center.x-d.width)/2, (center.y-d.height)/2));
- }
-
- public void zoomIn(Point p) {
- int width = getPreferredSize().width;
- int height = getPreferredSize().height;
- JViewport viewport = (JViewport) getParent();
- Dimension d = viewport.getSize();
- setPreferredSize(new Dimension(width*2,height*2));
- setSize(getPreferredSize());
- revalidate();
- viewport.setViewPosition(new Point(2*(p.x-d.width/4), 2*(p.y-d.height/4)));
- }
-
- public void selectBot(String name) {
- if (shapes != null) {
- Iterator<Shape> i = shapes.iterator();
- while(i.hasNext()) {
- Shape shape = i.next();
- if(!(shape instanceof BotShape)) continue;
- if(name.equalsIgnoreCase("EVERYONE") || name.equalsIgnoreCase(shape.getName())) {
- shape.highlight();
- } else {
- shape.unhighlight();
- }
- }
- repaint();
- } else {
- System.err.println("no shapes!");
- }
- }
-
- /**
- * this informs the bot that it should be at the screen position and orientation
- * indicated by the point
- */
- public void placeSelectedBot(Point p, float theta) {
- screen_to_map.transform(p, p);
- if(shapes != null) {
- Iterator<Shape> i = shapes.iterator();
- while(i.hasNext()) {
- Shape shape = i.next();
- if(shape.isSelected()) {
- BotShape selectedBot = (BotShape) shape;
- drawingPad.placeBot(shape.getName(), p, theta);
- return;
- }
- }
- }
- }
-
- public void search(RectangleShape shape) {
- //find translator bot and do translation here
- BotShape bot = BotOf(opTraderFrameHolder); //[2005-10-02] (tk): hack
- AffineTransform placement = bot.getPlacement();
- try {
- shape.transform(placement.createInverse());
- } catch (NoninvertibleTransformException e) {
- System.err.println("Can't invert transform: " + e.toString());
- System.exit(1);
- }
- drawingPad.search(shape);
- }
-
- public void explore(PolyLineShape shape) {
- //find translator bot and do translation here
- System.err.println("exploring here");
- BotShape bot = BotOf(opTraderFrameHolder); //[2005-10-02] (tk): hack
- AffineTransform placement = bot.getPlacement();
- PolyLineShape searchShape = new PolyLineShape(shape);
- try {
- searchShape.transform(placement.createInverse());
- } catch (NoninvertibleTransformException e) {
- System.err.println("Can't invert transform: " + e.toString());
- System.exit(1);
- }
- drawingPad.explore(searchShape);
- }
-
- public void search(PolyLineShape shape) {
- //find translator bot and do translation here
- System.err.println("searching here");
- BotShape bot = BotOf(opTraderFrameHolder); //[2005-10-02] (tk): hack
- AffineTransform placement = bot.getPlacement();
- PolyLineShape searchShape = new PolyLineShape(shape);
- try {
- searchShape.transform(placement.createInverse());
- } catch (NoninvertibleTransformException e) {
- System.err.println("Can't invert transform: " + e.toString());
- System.exit(1);
- }
- drawingPad.search(searchShape);
- }
-
- public BotShape BotOf(String name) {
- ListIterator<Shape> i = shapes.listIterator();
- while(i.hasNext()) {
- Shape shape = i.next();
- if(shape.getName().equalsIgnoreCase(name)) return (BotShape)shape;
- }
- return null;
- }
-
- public void debugPoint(Point p) {
- drawingPad.interpreter.print_human(null, null, "clicked on (pixels): " + p.toString()
- + " map coord (cm): " + screen_to_map.transform(p, null).toString()
- + " map dimentions (pixels): (" + image_width_in_pixels + ", " + image_height_in_pixels + ")"
- + " map dimentions (cm): (" + native_width + ", " + native_height + ")"
- + " origin (cm): (" + x_origin + ", " + y_origin + ")");
- }
-
- private static final long serialVersionUID = 2665310629710366124L;
-
- /**
- *
- * The list of shapes of the drawing
- */
- protected List<Shape> shapes = new ArrayList<Shape>();
-
- protected Color curColor = Color.black;
-
- protected EventListener listener;
-
- protected boolean mouseButtonDown = false;
-
- protected int x;
-
- protected int y;
-
- /**
- * The image to display as the background (the map)
- */
- protected MapBufferedImage backImage = null;
-
- /**
- * An image of hashmarks for the transparent portions of the backImage
- */
- protected BufferedImage hashImage = null;
-
- /**
- * The width and height of the rendered map image (in pixels).
- * The map is streched when the application window grows or during zooming, etc.
- */
- protected Dimension view_size;
-
- /**
- * A transform from the map image to the rendered map image
- * The map is streched when the application window grows or during zooming, etc.
- */
- protected AffineTransform mapScale = new AffineTransform();
-
- /**
- * A transform from screen coordinates (pixels, left-handed)
- * to map coordinates (centimeters, right-handed)
- */
- protected AffineTransform screen_to_map = new AffineTransform();
-
- /**
- * A transform from map coordinates (centimeters, right-handed)
- * to screen coordinates (pixels, left-handed)
- */
- protected AffineTransform map_to_screen = new AffineTransform();
-
- /**
- * A class to generate new colors, maximally distinct from an existing set.
- */
- public ColorGenerator colorGenerator = null;
-
- public void mapUpdate(int type, int x_dim, int y_dim, float x_origin, float y_origin, int resolution, int[] encoded_map) {
- byte[] decoded_map = new byte[x_dim * y_dim];
- decodeMap(decoded_map, x_dim, y_dim, encoded_map);
- if (type == MapBufferedImage.FULL) {
- if (image_width_in_pixels != x_dim || image_height_in_pixels != y_dim) {
- int colors[] = new int[3];
- colors[1] = Color.BLACK.getRGB();
- colors[2] = Color.DARK_GRAY.getRGB();
- IndexColorModel iColor = new IndexColorModel(2, 3, colors, 0, false, 0, DataBuffer.TYPE_BYTE);
- hashImage = new BufferedImage(x_dim, y_dim, BufferedImage.TYPE_BYTE_INDEXED, iColor);
- int hashing = 100 / resolution;
- WritableRaster wr = hashImage.getRaster();
- for (int j = 0; j < y_dim; j++) {
- for (int i = 0; i < x_dim; i++) {
- if (j % hashing == 0 || i % hashing == 0)
- wr.setSample(i, j, 0, 2); else
- wr.setSample(i, j, 0, 1);
- }
- }
- setPreferredSize(new Dimension(x_dim, y_dim));
- }
- backImage = MapBufferedImage.createMapBufferedImage(x_dim, y_dim);
- native_width = x_dim * resolution;
- native_height = y_dim * resolution;
- this.x_origin = x_origin * 100;
- this.y_origin = y_origin * 100;
- image_width_in_pixels = x_dim;
- image_height_in_pixels = y_dim;
- backImage.setFullMap(decoded_map);
- repaint();
- } else
- if (type == MapBufferedImage.DIFF) {
- if (x_dim != image_width_in_pixels || y_dim != image_height_in_pixels) {
- System.err.println("diff map for different full map");
- } else {
- backImage.setDiffMap(decoded_map);
- repaint();
- }
- } else {
- System.err.println("unknown map type: " + type);
- }
- }
-
- protected void decodeMap(byte[] decoded_map, int x, int y, int[] encoded_map) {
- System.err.println("x: " + x + " y: " + y + " encoded_len: " + encoded_map.length);
- int j = 0, k = 0;
- for (int i = 0; i < encoded_map.length; i++) {
- int run_length = (encoded_map[i] & -256) >> 8;
- byte run_value = (byte) (encoded_map[i] & 255);
- if (i == 0)
- System.err.println("raw: " + encoded_map[i] + " rl: " + run_length + " rv: " + run_value + " j: " + j);
- for (; j < k + run_length; j++) {
- decoded_map[j] = run_value;
- }
- k = j;
- }
- }
-
- public void setCurColor(Color curColor) {
- this.curColor = curColor;
- }
-
- public Color getCurColor() {
- return curColor;
- }
-
- public void addShape(Shape shape) {
- if (shape != null) {
- shapes.add(shape);
- }
- }
-
- public void removeShape(Shape shape) {
- shapes.remove(shape);
- }
-
- public void paintComponent(Graphics g) {
- Dimension viewportDim = ((JViewport) getParent()).getExtentSize();
- if (view_size.width < viewportDim.width || view_size.height < viewportDim.height) {
- setPreferredSize(new Dimension(Math.max(view_size.width, viewportDim.width), Math.max(view_size.height, viewportDim.height)));
- revalidate();
- }
- if (backImage == null) {
- System.err.println("Got no backImage");
- return;
- }
- Graphics2D mapG = (Graphics2D) g.create();
- mapG.transform(mapScale);
- if (hashImage != null) {
- mapG.drawImage(hashImage, 0, 0, this);
- }
- mapG.drawImage(backImage, 0, 0, this);
- Graphics2D shapeG = (Graphics2D) g.create();
- shapeG.transform(map_to_screen);
- shapeG.setFont(BotShape.labelFont);
- if (shapes != null) {
- Iterator iter = shapes.iterator();
- while (iter.hasNext()) {
- Shape shape = (Shape) iter.next();
- if (shape != null) {
- shape.draw(shapeG);
- if (shape instanceof BotShape) {
- ((BotShape) shape).label(screen_to_map, shapeG);
- }
- }
- }
- }
- }
-
- public void newFile() {
- shapes.clear();
- repaint();
- }
-
- public void openFile(String filename) {
- try {
- ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
- try {
- List l = (List) in.readObject();
- Iterator li = l.iterator();
- while (li.hasNext()) {
- shapes.add((Shape) li.next());
- }
- } catch (Exception e) {
- System.err.println("Some problem with reading: " + e);
- }
- in.close();
- repaint();
- } catch (IOException e1) {
- System.out.println("Unable to open file: " + filename);
- }
- }
-
- public void saveFile(String filename) {
- try {
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
- out.writeObject(shapes);
- out.close();
- System.out.println("Save drawing to " + filename);
- } catch (IOException e) {
- System.out.println("Unable to write file: " + filename);
- }
- }
-
- public AffineTransform getScreenToMap() {
- return screen_to_map;
- }
-
- public String selectObj(Point p) {
- String retval = null;
- screen_to_map.transform(p, p);
- if (shapes != null) {
- Iterator<Shape> i = shapes.iterator();
- while (i.hasNext()) {
- Shape shape = i.next();
- if (shape.contains(p)) {
- shape.highlight();
- retval = shape.getName();
- } else {
- shape.unhighlight();
- }
- }
- repaint();
- }
- return retval;
- }
-
- public void setPreferredSize(Dimension d) {
- view_size = d;
- map_to_screen.setToScale(view_size.width / native_width, view_size.height / native_height);
- map_to_screen.translate(x_origin, y_origin);
- try {
- screen_to_map = map_to_screen.createInverse();
- } catch (NoninvertibleTransformException e) {
- System.err.println("couldn't build map_to_screen");
- }
- mapScale.setToScale(view_size.width / image_width_in_pixels, view_size.height / image_height_in_pixels);
- mapScale.translate(0, image_height_in_pixels);
- }
-
- public Dimension getPreferredSize() {
- return view_size;
- }
-
- public Dimension getPreferredScrollableViewportSize() {
- return getPreferredSize();
- }
-
- public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
- return 1;
- }
-
- public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
- if (orientation == SwingConstants.VERTICAL)
- return visibleRect.height / 2; else
- return visibleRect.width / 2;
- }
-
- public boolean getScrollableTracksViewportWidth() {
- return false;
- }
-
- public boolean getScrollableTracksViewportHeight() {
- return false;
- }
-}
Deleted: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvasListener.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvasListener.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvasListener.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -1,66 +0,0 @@
-package edu.cmu.ravenclaw.pendecoder;
-
-import java.awt.*;
-import java.awt.event.*;
-
-public class DrawingCanvasListener implements MouseListener, MouseMotionListener {
-
- protected DrawingCanvas canvas;
- protected Tool tool;
-
- public DrawingCanvasListener(DrawingCanvas canvas) {
- this.canvas = canvas;
- tool = new DrawingTool(canvas, "Scribble");
- }
-
- public void mousePressed(MouseEvent e) {
- Point p = e.getPoint();
- tool.mouseDownAt(p);
- canvas.mouseButtonDown = true;
- canvas.x = p.x;
- canvas.y = p.y;
- }
-
- public void mouseDragged(MouseEvent e) {
- Point p = e.getPoint();
- if (canvas.mouseButtonDown) {
- tool.mouseDraggedTo(p);
- canvas.x = p.x;
- canvas.y = p.y;
- }
- }
-
- public void mouseReleased(MouseEvent e) {
- Point p = e.getPoint();
- tool.mouseReleasedAt(p);
- canvas.mouseButtonDown = false;
- }
-
- public void mouseClicked(MouseEvent e) {
- Point p = e.getPoint();
- if(e.getClickCount() == 1) {
- if(e.getButton() == e.BUTTON1)
- tool.mouseClickedAt(p);
- else
- tool.mouseRightClickedAt(p);
- } else {
- tool.mouseDoubleClickedAt(p);
- }
- }
-
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- public void mouseMoved(MouseEvent e) {
- Point p = e.getPoint();
- tool.mouseMovedTo(p);
- }
-
- public Tool getTool() {
- return tool;
- }
-
- public void setTool(Tool tool) {
- this.tool = tool;
- }
-
-}
Deleted: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingPad.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingPad.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingPad.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -1,349 +0,0 @@
-package edu.cmu.ravenclaw.pendecoder;
-
-import java.util.*;
-import java.awt.*;
-import java.awt.event.*;
-import javax.swing.*;
-
-import bsh.util.JConsole;
-import java.awt.image.BufferedImage;
-
-public class DrawingPad extends JFrame {
-
- protected ToolKit toolkit;
- protected JComponent toolbar;
- protected DrawingCanvas drawingCanvas;
- protected DecoderInterpreter interpreter;
- protected java.util.List<PenDecoderServer> servers = new java.util.Vector<PenDecoderServer>();
- protected Imager imager;
-
- public TaskKeeper taskKeeper = new TaskKeeper();
-
- public DrawingPad(String title) {
- super(title);
- // calling factory method
- drawingCanvas = makeCanvas();
- getContentPane().setLayout(new BorderLayout());
- menuBar = createMenuBar();
- getContentPane().add(menuBar, BorderLayout.NORTH);
- pane = new JScrollPane(drawingCanvas);
- JRootPane rootPane = new JRootPane();
- rootPane.setContentPane(pane);
- LogoViewport logopane = new LogoViewport();
- rootPane.setGlassPane(logopane);
- logopane.setVisible(true);
- pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
- pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
- getContentPane().add(rootPane, BorderLayout.CENTER);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- if (exitAction != null) {
- exitAction.actionPerformed(new ActionEvent(DrawingPad.this, 0, null));
- }
- }
- });
-
- //add toolbar
- toolkit = createToolkit();
- drawingCanvas.setTool(toolkit.getTool(0));
- ActionListener toolListener = new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- Object source = event.getSource();
- if (source instanceof AbstractButton) {
- AbstractButton button = (AbstractButton) source;
- Tool tool = toolkit.setSelectedTool(button.getText());
- drawingCanvas.setTool(tool);
- try {
- if(tool.getName() == "Zoom Out") {
- drawingCanvas.zoomOut();
- } else if(tool.getName() == "Cancel Task") {
- Integer lastAdded = taskKeeper.getLastAdded();
- if(lastAdded == null) System.err.println("There are no tasks.");
- else cancelTask(lastAdded);
- } else if(tool.getName() == "Start Session") {
- startSession();
- ToggleButtonTool bt = (ToggleButtonTool)tool;
- bt.toggleState();
- ((JToggleButton)((JPanel)toolbar).getComponent(0)).setText(bt.getName());
- } else if(tool.getName() == "Stop Session") {
- endSession();
- ToggleButtonTool bt = (ToggleButtonTool)tool;
- bt.toggleState();
- ((JToggleButton)((JPanel)toolbar).getComponent(0)).setText(bt.getName());
- }
- } catch (NullPointerException e) {}
- }
- repaint();
- }
- };
- toolbar = createToolBar(toolListener);
- getContentPane().add(toolbar, BorderLayout.WEST);
-
- //add menu
- //JMenu menu = createToolMenu(toolListener);
- //menuBar.add(menu, 1); // insert at index position 1
-
- JPanel lowerContainer = new JPanel(new BorderLayout());
- //lowerContainer.setPreferredSize(new Dimension(1024, 260));
-
- //add console to lowerContainer
- JConsole console = new JConsole();
- console.setPreferredSize(new Dimension(1000, 100));
- console.getViewport().getView().setBackground(Color.black);
- console.getViewport().getView().setForeground(Color.white);
- lowerContainer.add(console, BorderLayout.CENTER);
- interpreter = new DecoderInterpreter(console, this);
- interpreter.start();
-
- //add imager
- imager = new Imager();
- //imager.setPreferredSize(new Dimension(240, 195));
- lowerContainer.add(imager, BorderLayout.EAST);
-
- //add lowerContainer to this
- getContentPane().add(lowerContainer, BorderLayout.SOUTH);
- }
-
- /**
- *
- * @param i image to paint in imager panel
- */
- public void image(BufferedImage i) {
- imager.setImage(i);
- getRootPane().revalidate();
- imager.repaint();
- }
-
- public void mapUpdate(int type, int x_dim, int y_dim, float x_origin, float y_origin, int resolution, int[] encoded_map) {
- drawingCanvas.mapUpdate(type, x_dim, y_dim, x_origin, y_origin, resolution, encoded_map);
- }
-
- public void addServer(PenDecoderServer server) {
- servers.add(server);
- System.err.println("added server #" + servers.size());
- }
-
- public Tool getSelectedTool() {
- return toolkit.getSelectedTool();
- }
-
- protected ToolKit createToolkit() {
- ToolKit t = new ToolKit();
- t.addTool(new ToggleButtonTool(drawingCanvas,
- "Start Session",
- "Click to start speaking",
- "Stop Session",
- "Click to mute"));
- t.addTool(new PointTool(drawingCanvas,
- "Select",
- "Click on a robot to select it.",
- PointTool.POINT));
- t.addTool(new PlacementTool(drawingCanvas,
- "Re-position",
- "Click to reposition the selected robot. Drag to orient it at the new position."));
- t.addTool(new PointTool(drawingCanvas,
- "Zoom In",
- "Click to zoom into a point on the map.",
- PointTool.ZOOMIN));
- t.addTool(new ButtonTool(drawingCanvas, "Zoom Out", "Zoom out to see the entire map."));
- t.addTool(new ButtonTool(drawingCanvas, "Cancel Task", "Cancel the last task."));
- t.addTool(new NPointTool(drawingCanvas,
- "Search",
- "Select waypoints along which to search.",
- NPointTool.POLYLINE));
- t.addTool(new NPointTool(drawingCanvas,
- "Explore",
- "Select waypoints along which to explore.",
- NPointTool.POLYLINE_EXPLORE));
- return t;
- }
-
- // factory method
- protected DrawingCanvas makeCanvas() {
- return (drawingCanvas = new DrawingCanvas(this));
- }
-
- public void say(String utt) {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().say(utt);
- }
- }
-
- public void saySelectBot(String name) {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().saySelectBot(name);
- }
- }
-
- public void placeBot(String name, Point p, float theta) {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().placeBot(name, p.x, p.y, theta);
- }
- }
-
- public void selectBot(String name) {
- drawingCanvas.selectBot(name);
- }
-
- public void search(RectangleShape shape) {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().search(shape);
- }
- }
-
- public void explore(PolyLineShape shape) {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().explore(shape);
- }
- }
-
- public void cancelTask(Integer taskid) {
- for(PenDecoderServer server : servers) server.cancelTask(taskid);
- }
-
- public void startSession() {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().startSession();
- }
- i = servers.listIterator();
- while(i.hasNext()) {
- i.next().saySelectBot("EVERYONE");
- }
- }
-
- public void endSession() {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().endSession();
- }
- }
-
- public void search(PolyLineShape shape) {
- ListIterator<PenDecoderServer> i = servers.listIterator();
- while(i.hasNext()) {
- i.next().search(shape);
- }
- }
-
- public void cancel() { //clears context of explore or search
-
- }
-
- public DrawingCanvas getCanvas() {
- return drawingCanvas;
- }
-
- public DecoderInterpreter getInterpreter() {
- return interpreter;
- }
-
- protected JComponent createToolBar(ActionListener toolListener) {
- JPanel toolbar = new JPanel(new GridLayout(0, 1, 8, 0));
- ButtonGroup group = new ButtonGroup();
- Font buttonFont = new Font("Arial Narrow", Font.PLAIN, 28);
- int n = toolkit.getToolCount();
- for (int i = 0; i < n; i++) {
- AbstractDecoderTool tool = toolkit.getTool(i);
- if (tool != null) {
- JToggleButton button;
- if (tool.getIcon() == null) {
- button = new JToggleButton(tool.getName()) {
- public JToolTip createToolTip() {
- JToolTip t = super.createToolTip();
- t.setFont(new Font("Tahoma", Font.PLAIN, 18));
- return t;
- }
- private static final long serialVersionUID = -8249242044579783954L;
- };
- button.setFont(buttonFont);
- } else {
- button = new JToggleButton(tool.getIcon());
- }
- if(tool.getTooltip() != null) button.setToolTipText(tool.getTooltip());
- button.addActionListener(toolListener);
- toolbar.add(button);
- group.add(button);
- }
- }
- return toolbar;
- }
-
- protected JMenu createToolMenu(ActionListener toolListener) {
- JMenu menu = new JMenu("Tools");
- int n = toolkit.getToolCount();
- for (int i = 0; i < n; i++) {
- Tool tool = toolkit.getTool(i);
- if (tool != null) {
- JMenuItem menuitem = new JMenuItem(tool.getName());
- menuitem.addActionListener(toolListener);
- menu.add(menuitem);
- }
- }
- return menu;
- }
-
- public static void main(String[] args) {
- DrawingPad frame = new DrawingPad("Treasure Hunt GUI");
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- frame.setSize(screenSize.width/2, screenSize.height/2);
- frame.setLocation(screenSize.width/4, screenSize.height/4);
- frame.setExtendedState(MAXIMIZED_BOTH);
- frame.setVisible(true);
- }
-
- private static final long serialVersionUID = -3941481106291332264L;
-
- protected JMenuBar createMenuBar() {
- JMenuBar menuBar = new JMenuBar();
- JMenu menu;
- JMenuItem mi;
- menu = new JMenu("File");
- menuBar.add(menu);
- exitAction = new ExitListener();
- mi = new JMenuItem("Exit");
- menu.add(mi);
- mi.addActionListener(exitAction);
- menu = new JMenu("Help");
- menuBar.add(menu);
- mi = new JMenuItem("About");
- menu.add(mi);
- mi.addActionListener(new AboutListener());
- return menuBar;
- }
-
- class ExitListener implements ActionListener {
-
- public void actionPerformed(ActionEvent e) {
- int result = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Exit Treasure Hunt GUI?", JOptionPane.YES_NO_OPTION);
- if (result == JOptionPane.YES_OPTION) {
- System.exit(0);
- }
- }
- }
-
-
- class AboutListener implements ActionListener {
-
- public void actionPerformed(ActionEvent e) {
- JOptionPane.showMessageDialog(null, "Teamtalk version 1.0\nCarnegie Mellon\nBoeing Phantom Works\n",
- "About", JOptionPane.INFORMATION_MESSAGE);
- }
- }
-
- protected JScrollPane pane;
-
- protected JMenuBar menuBar;
-
- protected ActionListener exitAction;
-
- protected static int width = 1200;
-
- protected static int height = 800;
-
-}
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingTool.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingTool.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingTool.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -5,7 +5,7 @@
public class DrawingTool extends AbstractTool {
- public DrawingTool(DrawingCanvas canvas, String name) {
+ public DrawingTool(MapCanvas canvas, String name) {
super(canvas, name);
}
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/IconTool.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/IconTool.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/IconTool.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -22,7 +22,7 @@
public class IconTool extends AbstractDecoderTool {
/** Creates a new instance of IconTool */
- public IconTool(DrawingCanvas canvas, String name, String tooltip, String imageFile) {
+ public IconTool(MapCanvas canvas, String name, String tooltip, String imageFile) {
super(canvas, name, tooltip);
try {
Image image = ImageIO.read(new File(imageFile));
Copied: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/MapCanvas.java (from rev 662, TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvas.java)
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/MapCanvas.java (rev 0)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/MapCanvas.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -0,0 +1,505 @@
+package edu.cmu.ravenclaw.pendecoder;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.Point;
+import java.awt.Image;
+import java.awt.Rectangle;
+import java.awt.image.BufferedImage;
+import java.awt.image.DataBuffer;
+import java.awt.image.IndexColorModel;
+import java.awt.image.WritableRaster;
+import java.util.*;
+import java.io.*;
+import java.awt.event.*;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.NoninvertibleTransformException;
+import java.util.EventListener;
+import javax.swing.*;
+import javax.imageio.ImageIO;
+
+public class MapCanvas extends JComponent implements Scrollable {
+
+ protected PenDecoderDisplay drawingPad = null;
+ protected MapCanvasListener drawingCanvasListener;
+ static public String opTraderFrameHolder; //[2005-09-30] (tk): hack
+
+ /**
+ * The width and height of the area that the map represents (in centimeters)
+ */
+ protected float native_width, native_height;
+
+ /**
+ * A vector from the upper left corner of the map to the world origin (in centimeters)
+ */
+ protected float x_origin, y_origin;
+
+ /**
+ * The width and height of the actual map image (in pixels)
+ */
+ protected float image_width_in_pixels, image_height_in_pixels;
+
+ public MapCanvas() {
+ colorGenerator = new ColorGenerator(new Color[] {
+ Color.BLACK, //clear space
+ Color.DARK_GRAY, //hash marks
+ Color.GRAY, //unknown space
+ Color.GREEN, //obstructed space
+ Color.YELLOW //highlights
+ });
+
+ String mapfile = null;
+ try {
+ Properties properties = new Properties();
+ properties.load(new FileInputStream("map.properties"));
+ mapfile = properties.getProperty("edu.cmu.ravenclaw.pendecoder.mapfile");
+ if(mapfile == null) throw new Exception("mapfile is missing");
+ String widthstr = properties.getProperty("edu.cmu.ravenclaw.pendecoder.mapwidth");
+ if(widthstr == null) throw new Exception("mapwidth is missing");
+ String heightstr = properties.getProperty("edu.cmu.ravenclaw.pendecoder.mapheight");
+ native_width = Float.parseFloat(widthstr);
+ native_height = Float.parseFloat(heightstr);
+ } catch (NumberFormatException e) {
+ System.err.println("Can't parse double:" + e.toString());
+ System.exit(1);
+ } catch (Exception e) {
+ System.err.println("Problem with properties" + e.toString());
+ System.exit(1);
+ }
+ try {
+ backImage = new MapBufferedImage(ImageIO.read(new File(mapfile)));
+ image_width_in_pixels = backImage.getWidth(this);
+ image_height_in_pixels = backImage.getHeight(this);
+ setPreferredSize(new Dimension((int)image_width_in_pixels,
+ (int)image_height_in_pixels));
+ } catch(Exception e) {
+ System.err.println("Problem reading or converting PNG: " + e.toString());
+ System.exit(1);
+ }
+ drawingCanvasListener = makeCanvasListener();
+ addMouseListener((MouseListener) listener);
+ addMouseMotionListener((MouseMotionListener) listener);
+ }
+
+ public MapCanvas(Image backImage, PenDecoderDisplay drawingPad) {
+ //calling factory method
+ System.err.println("scribble w/backimage");
+ drawingCanvasListener = makeCanvasListener();
+ addMouseListener((MouseListener) listener);
+ addMouseMotionListener((MouseMotionListener) listener);
+ backImage = new MapBufferedImage(backImage);
+ this.drawingPad = drawingPad;
+ }
+
+ public MapCanvas(PenDecoderDisplay drawingPad) {
+ this.drawingPad = drawingPad;
+ }
+
+ public void setTool(Tool tool) {
+ drawingCanvasListener.setTool(tool);
+ }
+
+ public Tool getTool() {
+ return drawingCanvasListener.getTool();
+ }
+
+ /**
+ * factory method
+ */
+ protected MapCanvasListener makeCanvasListener() {
+ return new MapCanvasListener(this);
+ }
+
+ public void saySelectBot(String name) {
+ drawingPad.saySelectBot(name);
+ }
+
+ public void zoomOut() {
+ int width = getPreferredSize().width;
+ int height = getPreferredSize().height;
+ JViewport viewport = (JViewport) getParent();
+ Dimension d = viewport.getSize();
+ Point center = new Point(
+ viewport.getViewPosition().x+
+ viewport.getExtentSize().width/2,
+ viewport.getViewPosition().y+
+ viewport.getExtentSize().height/2);
+ setPreferredSize(new Dimension(width/2,height/2));
+ setSize(getPreferredSize());
+ revalidate();
+ viewport.setViewPosition(new Point((center.x-d.width)/2, (center.y-d.height)/2));
+ }
+
+ public void zoomIn(Point p) {
+ int width = getPreferredSize().width;
+ int height = getPreferredSize().height;
+ JViewport viewport = (JViewport) getParent();
+ Dimension d = viewport.getSize();
+ setPreferredSize(new Dimension(width*2,height*2));
+ setSize(getPreferredSize());
+ revalidate();
+ viewport.setViewPosition(new Point(2*(p.x-d.width/4), 2*(p.y-d.height/4)));
+ }
+
+ public void selectBot(String name) {
+ if (shapes != null) {
+ Iterator<Shape> i = shapes.iterator();
+ while(i.hasNext()) {
+ Shape shape = i.next();
+ if(!(shape instanceof BotShape)) continue;
+ if(name.equalsIgnoreCase("EVERYONE") || name.equalsIgnoreCase(shape.getName())) {
+ shape.highlight();
+ } else {
+ shape.unhighlight();
+ }
+ }
+ repaint();
+ } else {
+ System.err.println("no shapes!");
+ }
+ }
+
+ /**
+ * this informs the bot that it should be at the screen position and orientation
+ * indicated by the point
+ */
+ public void placeSelectedBot(Point p, float theta) {
+ screen_to_map.transform(p, p);
+ if(shapes != null) {
+ Iterator<Shape> i = shapes.iterator();
+ while(i.hasNext()) {
+ Shape shape = i.next();
+ if(shape.isSelected()) {
+ BotShape selectedBot = (BotShape) shape;
+ drawingPad.placeBot(shape.getName(), p, theta);
+ return;
+ }
+ }
+ }
+ }
+
+ public void search(RectangleShape shape) {
+ //find translator bot and do translation here
+ BotShape bot = BotOf(opTraderFrameHolder); //[2005-10-02] (tk): hack
+ AffineTransform placement = bot.getPlacement();
+ try {
+ shape.transform(placement.createInverse());
+ } catch (NoninvertibleTransformException e) {
+ System.err.println("Can't invert transform: " + e.toString());
+ System.exit(1);
+ }
+ drawingPad.search(shape);
+ }
+
+ public void explore(PolyLineShape shape) {
+ //find translator bot and do translation here
+ System.err.println("exploring here");
+ BotShape bot = BotOf(opTraderFrameHolder); //[2005-10-02] (tk): hack
+ AffineTransform placement = bot.getPlacement();
+ PolyLineShape searchShape = new PolyLineShape(shape);
+ try {
+ searchShape.transform(placement.createInverse());
+ } catch (NoninvertibleTransformException e) {
+ System.err.println("Can't invert transform: " + e.toString());
+ System.exit(1);
+ }
+ drawingPad.explore(searchShape);
+ }
+
+ public void search(PolyLineShape shape) {
+ //find translator bot and do translation here
+ System.err.println("searching here");
+ BotShape bot = BotOf(opTraderFrameHolder); //[2005-10-02] (tk): hack
+ AffineTransform placement = bot.getPlacement();
+ PolyLineShape searchShape = new PolyLineShape(shape);
+ try {
+ searchShape.transform(placement.createInverse());
+ } catch (NoninvertibleTransformException e) {
+ System.err.println("Can't invert transform: " + e.toString());
+ System.exit(1);
+ }
+ drawingPad.search(searchShape);
+ }
+
+ public BotShape BotOf(String name) {
+ ListIterator<Shape> i = shapes.listIterator();
+ while(i.hasNext()) {
+ Shape shape = i.next();
+ if(shape.getName().equalsIgnoreCase(name)) return (BotShape)shape;
+ }
+ return null;
+ }
+
+ public void debugPoint(Point p) {
+ drawingPad.interpreter.print_human(null, null, "clicked on (pixels): " + p.toString()
+ + " map coord (cm): " + screen_to_map.transform(p, null).toString()
+ + " map dimentions (pixels): (" + image_width_in_pixels + ", " + image_height_in_pixels + ")"
+ + " map dimentions (cm): (" + native_width + ", " + native_height + ")"
+ + " origin (cm): (" + x_origin + ", " + y_origin + ")");
+ }
+
+ private static final long serialVersionUID = 2665310629710366124L;
+
+ /**
+ *
+ * The list of shapes of the drawing
+ */
+ protected List<Shape> shapes = new ArrayList<Shape>();
+
+ protected Color curColor = Color.black;
+
+ protected EventListener listener;
+
+ protected boolean mouseButtonDown = false;
+
+ protected int x;
+
+ protected int y;
+
+ /**
+ * The image to display as the background (the map)
+ */
+ protected MapBufferedImage backImage = null;
+
+ /**
+ * An image of hashmarks for the transparent portions of the backImage
+ */
+ protected BufferedImage hashImage = null;
+
+ /**
+ * The width and height of the rendered map image (in pixels).
+ * The map is streched when the application window grows or during zooming, etc.
+ */
+ protected Dimension view_size;
+
+ /**
+ * A transform from the map image to the rendered map image
+ * The map is streched when the application window grows or during zooming, etc.
+ */
+ protected AffineTransform mapScale = new AffineTransform();
+
+ /**
+ * A transform from screen coordinates (pixels, left-handed)
+ * to map coordinates (centimeters, right-handed)
+ */
+ protected AffineTransform screen_to_map = new AffineTransform();
+
+ /**
+ * A transform from map coordinates (centimeters, right-handed)
+ * to screen coordinates (pixels, left-handed)
+ */
+ protected AffineTransform map_to_screen = new AffineTransform();
+
+ /**
+ * A class to generate new colors, maximally distinct from an existing set.
+ */
+ public ColorGenerator colorGenerator = null;
+
+ public void mapUpdate(int type, int x_dim, int y_dim, float x_origin, float y_origin, int resolution, int[] encoded_map) {
+ byte[] decoded_map = new byte[x_dim * y_dim];
+ decodeMap(decoded_map, x_dim, y_dim, encoded_map);
+ if (type == MapBufferedImage.FULL) {
+ if (image_width_in_pixels != x_dim || image_height_in_pixels != y_dim) {
+ int colors[] = new int[3];
+ colors[1] = Color.BLACK.getRGB();
+ colors[2] = Color.DARK_GRAY.getRGB();
+ IndexColorModel iColor = new IndexColorModel(2, 3, colors, 0, false, 0, DataBuffer.TYPE_BYTE);
+ hashImage = new BufferedImage(x_dim, y_dim, BufferedImage.TYPE_BYTE_INDEXED, iColor);
+ int hashing = 100 / resolution;
+ WritableRaster wr = hashImage.getRaster();
+ for (int j = 0; j < y_dim; j++) {
+ for (int i = 0; i < x_dim; i++) {
+ if (j % hashing == 0 || i % hashing == 0)
+ wr.setSample(i, j, 0, 2); else
+ wr.setSample(i, j, 0, 1);
+ }
+ }
+ setPreferredSize(new Dimension(x_dim, y_dim));
+ }
+ backImage = MapBufferedImage.createMapBufferedImage(x_dim, y_dim);
+ native_width = x_dim * resolution;
+ native_height = y_dim * resolution;
+ this.x_origin = x_origin * 100;
+ this.y_origin = y_origin * 100;
+ image_width_in_pixels = x_dim;
+ image_height_in_pixels = y_dim;
+ backImage.setFullMap(decoded_map);
+ repaint();
+ } else
+ if (type == MapBufferedImage.DIFF) {
+ if (x_dim != image_width_in_pixels || y_dim != image_height_in_pixels) {
+ System.err.println("diff map for different full map");
+ } else {
+ backImage.setDiffMap(decoded_map);
+ repaint();
+ }
+ } else {
+ System.err.println("unknown map type: " + type);
+ }
+ }
+
+ protected void decodeMap(byte[] decoded_map, int x, int y, int[] encoded_map) {
+ System.err.println("x: " + x + " y: " + y + " encoded_len: " + encoded_map.length);
+ int j = 0, k = 0;
+ for (int i = 0; i < encoded_map.length; i++) {
+ int run_length = (encoded_map[i] & -256) >> 8;
+ byte run_value = (byte) (encoded_map[i] & 255);
+ if (i == 0)
+ System.err.println("raw: " + encoded_map[i] + " rl: " + run_length + " rv: " + run_value + " j: " + j);
+ for (; j < k + run_length; j++) {
+ decoded_map[j] = run_value;
+ }
+ k = j;
+ }
+ }
+
+ public void setCurColor(Color curColor) {
+ this.curColor = curColor;
+ }
+
+ public Color getCurColor() {
+ return curColor;
+ }
+
+ public void addShape(Shape shape) {
+ if (shape != null) {
+ shapes.add(shape);
+ }
+ }
+
+ public void removeShape(Shape shape) {
+ shapes.remove(shape);
+ }
+
+ public void paintComponent(Graphics g) {
+ Dimension viewportDim = ((JViewport) getParent()).getExtentSize();
+ if (view_size.width < viewportDim.width || view_size.height < viewportDim.height) {
+ setPreferredSize(new Dimension(Math.max(view_size.width, viewportDim.width), Math.max(view_size.height, viewportDim.height)));
+ revalidate();
+ }
+ if (backImage == null) {
+ System.err.println("Got no backImage");
+ return;
+ }
+ Graphics2D mapG = (Graphics2D) g.create();
+ mapG.transform(mapScale);
+ if (hashImage != null) {
+ mapG.drawImage(hashImage, 0, 0, this);
+ }
+ mapG.drawImage(backImage, 0, 0, this);
+ Graphics2D shapeG = (Graphics2D) g.create();
+ shapeG.transform(map_to_screen);
+ shapeG.setFont(BotShape.labelFont);
+ if (shapes != null) {
+ Iterator iter = shapes.iterator();
+ while (iter.hasNext()) {
+ Shape shape = (Shape) iter.next();
+ if (shape != null) {
+ shape.draw(shapeG);
+ if (shape instanceof BotShape) {
+ ((BotShape) shape).label(screen_to_map, shapeG);
+ }
+ }
+ }
+ }
+ }
+
+ public void newFile() {
+ shapes.clear();
+ repaint();
+ }
+
+ public void openFile(String filename) {
+ try {
+ ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
+ try {
+ List l = (List) in.readObject();
+ Iterator li = l.iterator();
+ while (li.hasNext()) {
+ shapes.add((Shape) li.next());
+ }
+ } catch (Exception e) {
+ System.err.println("Some problem with reading: " + e);
+ }
+ in.close();
+ repaint();
+ } catch (IOException e1) {
+ System.out.println("Unable to open file: " + filename);
+ }
+ }
+
+ public void saveFile(String filename) {
+ try {
+ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
+ out.writeObject(shapes);
+ out.close();
+ System.out.println("Save drawing to " + filename);
+ } catch (IOException e) {
+ System.out.println("Unable to write file: " + filename);
+ }
+ }
+
+ public AffineTransform getScreenToMap() {
+ return screen_to_map;
+ }
+
+ public String selectObj(Point p) {
+ String retval = null;
+ screen_to_map.transform(p, p);
+ if (shapes != null) {
+ Iterator<Shape> i = shapes.iterator();
+ while (i.hasNext()) {
+ Shape shape = i.next();
+ if (shape.contains(p)) {
+ shape.highlight();
+ retval = shape.getName();
+ } else {
+ shape.unhighlight();
+ }
+ }
+ repaint();
+ }
+ return retval;
+ }
+
+ public void setPreferredSize(Dimension d) {
+ view_size = d;
+ map_to_screen.setToScale(view_size.width / native_width, view_size.height / native_height);
+ map_to_screen.translate(x_origin, y_origin);
+ try {
+ screen_to_map = map_to_screen.createInverse();
+ } catch (NoninvertibleTransformException e) {
+ System.err.println("couldn't build map_to_screen");
+ }
+ mapScale.setToScale(view_size.width / image_width_in_pixels, view_size.height / image_height_in_pixels);
+ mapScale.translate(0, image_height_in_pixels);
+ }
+
+ public Dimension getPreferredSize() {
+ return view_size;
+ }
+
+ public Dimension getPreferredScrollableViewportSize() {
+ return getPreferredSize();
+ }
+
+ public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
+ return 1;
+ }
+
+ public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
+ if (orientation == SwingConstants.VERTICAL)
+ return visibleRect.height / 2; else
+ return visibleRect.width / 2;
+ }
+
+ public boolean getScrollableTracksViewportWidth() {
+ return false;
+ }
+
+ public boolean getScrollableTracksViewportHeight() {
+ return false;
+ }
+}
Copied: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/MapCanvasListener.java (from rev 661, TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingCanvasListener.java)
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/MapCanvasListener.java (rev 0)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/MapCanvasListener.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -0,0 +1,66 @@
+package edu.cmu.ravenclaw.pendecoder;
+
+import java.awt.*;
+import java.awt.event.*;
+
+public class MapCanvasListener implements MouseListener, MouseMotionListener {
+
+ protected MapCanvas canvas;
+ protected Tool tool;
+
+ public MapCanvasListener(MapCanvas canvas) {
+ this.canvas = canvas;
+ tool = new DrawingTool(canvas, "Scribble");
+ }
+
+ public void mousePressed(MouseEvent e) {
+ Point p = e.getPoint();
+ tool.mouseDownAt(p);
+ canvas.mouseButtonDown = true;
+ canvas.x = p.x;
+ canvas.y = p.y;
+ }
+
+ public void mouseDragged(MouseEvent e) {
+ Point p = e.getPoint();
+ if (canvas.mouseButtonDown) {
+ tool.mouseDraggedTo(p);
+ canvas.x = p.x;
+ canvas.y = p.y;
+ }
+ }
+
+ public void mouseReleased(MouseEvent e) {
+ Point p = e.getPoint();
+ tool.mouseReleasedAt(p);
+ canvas.mouseButtonDown = false;
+ }
+
+ public void mouseClicked(MouseEvent e) {
+ Point p = e.getPoint();
+ if(e.getClickCount() == 1) {
+ if(e.getButton() == e.BUTTON1)
+ tool.mouseClickedAt(p);
+ else
+ tool.mouseRightClickedAt(p);
+ } else {
+ tool.mouseDoubleClickedAt(p);
+ }
+ }
+
+ public void mouseEntered(MouseEvent e) {}
+ public void mouseExited(MouseEvent e) {}
+ public void mouseMoved(MouseEvent e) {
+ Point p = e.getPoint();
+ tool.mouseMovedTo(p);
+ }
+
+ public Tool getTool() {
+ return tool;
+ }
+
+ public void setTool(Tool tool) {
+ this.tool = tool;
+ }
+
+}
Modified: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/NPointTool.java
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/NPointTool.java 2007-07-19 21:33:55 UTC (rev 662)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/NPointTool.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -14,7 +14,7 @@
protected NPointShape lastShape = null;
protected Point lastPoint = null;
- public NPointTool(DrawingCanvas canvas, String name, String tooltip, int shapeType) {
+ public NPointTool(MapCanvas canvas, String name, String tooltip, int shapeType) {
super(canvas, name, tooltip);
this.shapeType = shapeType;
makeNewShape();
Copied: TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/PenDecoderDisplay.java (from rev 661, TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/DrawingPad.java)
===================================================================
--- TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/PenDecoderDisplay.java (rev 0)
+++ TeamTalk/Agents/PenDecoder/src/edu/cmu/ravenclaw/pendecoder/PenDecoderDisplay.java 2007-07-20 02:49:22 UTC (rev 663)
@@ -0,0 +1,473 @@
+package edu.cmu.ravenclaw.pendecoder;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.awt.image.BufferedImage;
+import java.util.*;
+import javax.swing.*;
+
+import bsh.util.JConsole;
+
+/**
+ * PenDecoderDisplay is the top-level JFrame (docked mode) or collection of
+ * top-level JFrames (undocked) for the PenDecoder. It contains the map
+ * (MapCanvas), the console, the tool buttons, and the camera images
+ * (imager).
+ */
+public class PenDecoderDisplay extends JFrame {
+
+ /**
+ * Constrcuct the components of the drawing pad: console, map,
+ * camera image, buttons
+ * @param title The title for the JFrame (when docked)
+ */
+ public PenDecoderDisplay(String title) {
+ super(title);
+ // calling factory method
+ drawingCanvas = makeCanvas();
+ getContentPane().setLayout(new BorderLayout());
+ menuBar = createMenuBar();
+ getContentPane().add(menuBar, BorderLayout.NORTH);
+ pane = new JScrollPane(drawingCanvas);
+ JRootPane rootPane = new JRootPane();
+ rootPane.setContentPane(pane);
+ LogoViewport logopane = new LogoViewport();
+ rootPane.setGlassPane(logopane);
+ logopane.setVisible(true);
+ pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
+ pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
+ getContentPane().add(rootPane, BorderLayout.CENTER);
+ addWindowListener(new WindowAdapter() {
+ public void windowClosing(WindowEvent e) {
+ if (exitAction != null) {
+ exitAction.actionPerformed(new ActionEvent(PenDecoderDisplay.this, 0, null));
+ }
+ }
+ });
+
+ //add toolbar
@@ Diff output truncated at 60000 characters. @@
More information about the TeamTalk-developers
mailing list