[Commit] RRClient RRTkProxy.java,NONE,1.1 RRBoardPanel.java,1.4,1.5 RRImages.java,1.4,1.5

Bart Massey commit at keithp.com
Sat Jun 7 21:20:32 PDT 2003


Committed by: bart

Update of /local/src/CVS/RRClient
In directory home.keithp.com:/tmp/cvs-serv26339

Modified Files:
	RRBoardPanel.java RRImages.java 
Added Files:
	RRTkProxy.java 
Log Message:
Split out toolkit proxy.
Added mouse motion tracking and highlighting for panel.  Ready
to add robot moving.



--- NEW FILE: RRTkProxy.java ---
/*
 * Ricochet Robots Toolkit Proxy
 * Created on May 31, 2003
 *
 */

/**
 * @author Bart Massey <bart at cs.pdx.edu>
 *
 */

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class RRTkProxy {
    JApplet applet = null;
    Toolkit tk = null;
    
    RRTkProxy(JApplet applet) { this.applet = applet; }
    RRTkProxy(Toolkit tk) { this.tk = tk; }

    Image getImage(String name) {
	Image result;
        if (tk != null)
            result = tk.getImage(name);
        else if (applet != null)
            result = applet.getImage(applet.getCodeBase(), name);
	else
            throw new Error("No image access");
	if (result == null)
	    throw new Error("Can't load image " + name);
	return result;
    }

    Image createImage(ImageProducer src) {
	Image result;
        if (tk != null)
            result = tk.createImage(src);
        else if (applet != null)
            result = applet.createImage(src);
	else
            throw new Error("No image creation");
	if (result == null)
	    throw new Error("Can't create image");
	return result;
    }
}


Index: RRBoardPanel.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoardPanel.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- RRBoardPanel.java	7 Jun 2003 07:45:02 -0000	1.4
+++ RRBoardPanel.java	8 Jun 2003 03:20:30 -0000	1.5
@@ -10,33 +10,107 @@
  */
 
 import javax.swing.*;
+import javax.swing.event.*;
 import java.awt.*;
+import java.awt.event.*;
+
+class BoardCoord {
+    public int row, col;
+
+    public boolean equals(BoardCoord b) {
+	if (b.row != row)
+	    return false;
+	if (b.col != col)
+	    return false;
+	return true;
+    }
+}
+
+class RobotMover extends MouseInputAdapter {
+    static final int IDLE = 0;
+    static final int DRAGGING = 1;
+    int state = IDLE;
+    RRBoard board;
+    RRBoardPanel panel;
+    public BoardCoord start = null;
+    public BoardCoord stop = null;
+    public BoardCoord cur = null;
+
+    public RobotMover(RRBoard board, RRBoardPanel panel) {
+	super();
+	this.board = board;
+	this.panel = panel;
+    }
+
+    public void mousePressed(MouseEvent e) {
+	if (e.getButton() != e.BUTTON1)
+	    return;
+	start = panel.boardCoord(e.getPoint());
+	stop = null;
+	cur = start;
+	state = DRAGGING;
+    }
+
+    public void mouseReleased(MouseEvent e) {
+	if (e.getButton() != e.BUTTON1)
+	    return;
+	stop = panel.boardCoord(e.getPoint());
+	cur = stop;
+	state = IDLE;
+	panel.clearHilite();
+    }
+
+    public void mouseDragged(MouseEvent e) {
+	if (state != DRAGGING)
+	    return;
+	cur = panel.boardCoord(e.getPoint());
+	panel.setHilite(cur);
+    }
+}
 
 public class RRBoardPanel extends JPanel {
     static final double wallwidth = 0.1;
 
     RRBoard board;
     RRImages images;
+    RobotMover mover;
+
+    int dim;
+    double dcol, drow;
+
+    BoardCoord hilited = null;
+
+    static int floor(double d) {
+	return (int)Math.floor(d);
+    }
+
+    void update_dims() {
+	dim = board.dim;
+        Dimension d = getSize();
+	dcol = d.width / (double)dim;
+	drow = d.height / (double)dim;
+    }
 
     public RRBoardPanel(RRBoard board, RRImages images) {
         this.board = board;
         this.images = images;
+	mover = new RobotMover(board, this);
+	this.addMouseMotionListener(mover);
+	this.addMouseListener(mover);
     }
 
     public void paintComponent(Graphics g) {
-	int dim = board.dim;
-        Dimension d = getSize();
-        double dcol = d.width / (double)dim;
-        double drow = d.height / (double)dim;
-	int idcol = (int)Math.floor(dcol + 1);
-	int idrow = (int)Math.floor(drow + 1);
+	update_dims();
+
+	int idcol = floor(dcol + 1);
+	int idrow = floor(drow + 1);
 
 	/* draw floor */
 	Image blank_img = images.getBlankImage();
         for (int row = 0; row < dim; row++) {
             for (int col = 0; col < dim; col++) {
-		int xdcol = (int)Math.floor(col * dcol);
-		int ydrow = (int)Math.floor(row * drow);
+		int xdcol = floor(col * dcol);
+		int ydrow = floor(row * drow);
 		g.drawImage(blank_img, xdcol, ydrow, idcol, idrow, this);
 	    }
 	}
@@ -48,8 +122,8 @@
 		    continue;
 		boolean primary = s.isPrimaryTarget();
 		Image img = images.getTargetImage(s.getTarget(), primary);
-		int xdcol = (int)Math.floor(col * dcol);
-		int ydrow = (int)Math.floor(row * drow);
+		int xdcol = floor(col * dcol);
+		int ydrow = floor(row * drow);
 		g.drawImage(img, xdcol, ydrow, idcol, idrow, this);
             }
         }
@@ -74,10 +148,10 @@
 			wc += dcol;
 		    if (w == 2)
 			wr += drow;
-		    int iwc = (int)Math.floor(wc);
-		    int iwr = (int)Math.floor(wr);
-		    int iww = (int)Math.floor(ww);
-		    int iwh = (int)Math.floor(wh);
+		    int iwc = floor(wc);
+		    int iwr = floor(wr);
+		    int iww = floor(ww);
+		    int iwh = floor(wh);
                     g.setColor(Color.BLACK);
                     g.fillRect(iwc, iwr, iww, iwh);
                 }
@@ -91,10 +165,39 @@
                 if (r == RRSquare.NO_ROBOT)
                     continue;
                 Image img = images.getRobotImage(r);
-		int col_dcol = (int)Math.floor(col * dcol);
-		int row_drow = (int)Math.floor(row * drow);
+		int col_dcol = floor(col * dcol);
+		int row_drow = floor(row * drow);
                 g.drawImage(img, col_dcol, row_drow, idcol, idrow, this);
             }
         }
+	/* draw hilite */
+	if (hilited != null) {
+	    int x = floor(hilited.col * dcol);
+	    int y = floor(hilited.row * drow);
+	    int w = floor(dcol);
+	    int h = floor(drow);
+	    g.setColor(Color.MAGENTA);
+	    g.drawRect(x, y, w, h);
+	}
+    }
+
+    public BoardCoord boardCoord(Point p) {
+	update_dims();
+	
+	BoardCoord c = new BoardCoord();
+	c.row = floor(p.y / drow);
+	c.col = floor(p.x / dcol);
+	return c;
+    }
+
+    public void setHilite(BoardCoord c) {
+	if (hilited == null || !hilited.equals(c))
+	    repaint();
+	hilited = c;
+    }
+
+    public void clearHilite() {
+	hilited = null;
+	repaint();
     }
 }

Index: RRImages.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRImages.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- RRImages.java	7 Jun 2003 07:45:02 -0000	1.4
+++ RRImages.java	8 Jun 2003 03:20:30 -0000	1.5
@@ -13,40 +13,6 @@
 import java.awt.*;
 import java.awt.image.*;
 
-class ToolkitProxy {
-    JApplet applet = null;
-    Toolkit tk = null;
-    
-    ToolkitProxy(JApplet applet) { this.applet = applet; }
-    ToolkitProxy(Toolkit tk) { this.tk = tk; }
-
-    Image getImage(String name) {
-	Image result;
-        if (tk != null)
-            result = tk.getImage(name);
-        else if (applet != null)
-            result = applet.getImage(applet.getCodeBase(), name);
-	else
-            throw new Error("No image access");
-	if (result == null)
-	    throw new Error("Can't load image " + name);
-	return result;
-    }
-
-    Image createImage(ImageProducer src) {
-	Image result;
-        if (tk != null)
-            result = tk.createImage(src);
-        else if (applet != null)
-            result = applet.createImage(src);
-	else
-            throw new Error("No image creation");
-	if (result == null)
-	    throw new Error("Can't create image");
-	return result;
-    }
-}
-
 class DimFilter extends RGBImageFilter {
     public DimFilter() {
 	// The filter's operation does not depend on the
@@ -74,7 +40,7 @@
     Image[] dim_target_images = new Image[17];
     Image[] robot_images = new Image[4];
 
-    ToolkitProxy tkproxy;
+    RRTkProxy tkproxy;
 
     int[] colors = {
       RRSquare.RED,
@@ -85,7 +51,7 @@
     String[] colornames = {"red", "yellow", "green", "blue"};
     String[] shapenames = {"circle", "square", "triangle", "star"};
 
-    private RRImages(ToolkitProxy tkproxy) {
+    private RRImages(RRTkProxy tkproxy) {
         this.tkproxy = tkproxy;
 	/* get the floor tile */
         blank_image = tkproxy.getImage("blank.png");
@@ -111,11 +77,11 @@
     }
 
     public RRImages(Toolkit tk) {
-        this(new ToolkitProxy(tk));
+        this(new RRTkProxy(tk));
     }
 
     public RRImages(JApplet applet) {
-        this(new ToolkitProxy(applet));
+        this(new RRTkProxy(applet));
     }
 
     public Image getBlankImage() {




More information about the Commit mailing list