//package Dasearch; import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.*; import java.net.Socket; public class pathfinder extends JFrame { public static boolean showtrace = true; // does not erase image protected BufferedImage diamondgif, chargif; // animated gif images protected Graphics display; protected Image dbuf; // double buffer protected static int gap = 20; // 24; // side/radius of hexagon protected int yoff = 40; // 40; protected int delaytime = 250; // animation delay time protected astar PG; int rows, cols; int XDIM, YDIM; // window dimensions int gemx, gemy, profx, profy; protected BufferedImage[] imageof; // image vector for terrain. protected BufferedImage[] imagechar; //image vector for character based on terrain. Color[] colorof ={Color.green,Color.yellow,Color.white,Color.blue}; // color corresponding to each terrain type. public static String fpath = ""; // file path prefix public static String savefile = "myastar.run"; public static boolean downloadconfig = false; public static boolean sendpath = false; // graphical representation Hexagon[][] HX; int hpdist = Hexagon.calchpdist(gap); // access center graphical coordinates at cell i,j int getx(int i, int j) { return HX[i][j].x; } int gety(int i, int j) { return HX[i][j].y; } Graphics Gg; public void paint(Graphics g) {} // override automatic repaint public pathfinder(int r, int c, String[] argv) // constructor { rows = r; cols = c; if (argv.length>0 && !isint(argv[0])) { astar.genmap=false; // load map from file or socket if (argv.length==2&&argv[0].equals("download")) {downloadconfig=sendpath=true;} } PG = new myastar(rows,cols); // note it's myastar, not astar if (downloadconfig) downloadrun(defaultserver,defaultsrvport,argv[1]); else if (!astar.genmap&&!argv[0].equals("relay")) loadrun(argv[0],argv.length==1); else if (!astar.genmap) loadrun(true); // relayconfig set externally if (astar.genmap) PG = new myastar(r,c); // if (down)loadrun failed //((myastar)PG).PF=this; rows = PG.ROWS; cols = PG.COLS; System.out.print("Costs: "); for(int tc:PG.costof) System.out.print(tc+" "); System.out.println(); hpdist = Hexagon.calchpdist(gap); HX = new Hexagon[rows][cols]; // graphical map for(int i=0;i=cols || y<0 || y>=rows) return; display.setColor(Color.black); display.drawString(""+t,getx(y,x)-gap,gety(y,x)-gap-4); } public void animate() { // invert path. coord path = null; try { // student code path = PG.search(gemy,gemx, profy,profx); // call SEARCH here } catch (Exception e) {System.out.println("error in call to search: "+e);} if (sendpath && csk!=null) try { // ObjectOutputStream bout=new ObjectOutputStream(csk.getOutputStream()); cskout.writeObject(path); } catch (Exception e) {System.out.println(e);} if (path==null) { display.setColor(Color.red); display.drawString("NO PATH TO TARGET!",50,100); System.out.println("no path"); if (sendpath && csk!=null) try { String response = (String)cskin.readObject(); System.out.println("Response from server: "+response); csk.close(); }catch(Exception ie){} return; } int px=0, py=0; // for calculating graphical coords while (path!=null) { px = getx(path.y,path.x); py = gety(path.y,path.x); display.drawImage(imagechar[PG.M[path.y][path.x]], (px-gap/2),(py-gap/2),gap,gap,null); // display.drawImage(imagechar[PG.M[path.y][path.x]], // (path.x*gap),(path.y*gap)+yoff,gap,gap,null); //System.out.printf("%d,%d: %d\n",path.y,path.x,PG.M[path.y][path.x]); try{Thread.sleep(delaytime);} catch(Exception se) {} // display.drawImage(imageof[PG.M[path.y][path.x]], // (path.x*gap),(path.y*gap)+yoff,gap,gap,null); // display.setColor(Color.red); // display.fillOval((path.x*gap)+8,(path.y*gap)+yoff+8,4,4); // for animation: // display.drawImage(diamondgif,gemx*gap,gemy*gap+yoff,gap,gap,null); if (!showtrace) // erase trail - redraw static map display.drawImage(dbuf,0,0,this); path = path.prev; }//with path!=null px = getx(gemy,gemx); py = gety(gemy,gemx); display.drawImage(diamondgif,px-gap/2,py-gap/2,gap,gap,null); display.drawImage(imagechar[PG.M[gemy][gemx]],px-gap/2,py-gap/2,gap,gap,null); if (sendpath && csk!=null) try { String response = (String)cskin.readObject(); System.out.println("Response from server: "+response); csk.close(); }catch(Exception ie){} }//animate public void drawmap() { int i, j; for(i=0;i=2) try { r = Integer.parseInt(args[args.length-2]); c = Integer.parseInt(args[args.length-1]); pathfinder.gap = (pathfinder.gap*defaultrows)/r; } catch (NumberFormatException nfe) {r=defaultrows; c=defaultcols;} pathfinder pf = new pathfinder(r,c,args); }//main } // class pathfinder