import com.ms.wfc.io.*; import com.ms.wfc.core.*; import com.ms.wfc.app.*; import com.ms.wfc.ui.*; import com.ms.wfc.util.*; import java.util.Vector; import com.ms.lang.*; import com.ms.*; public class Strat extends Form { Point navalDragPoint = new Point(-1,-1); Point navalStartPoint = new Point(-1,-1); Bitmap usDebrief,bothDebrief,geDebrief; Rectangle[] arrowBounds; int maxMaps = 44; int maxArrows = 84; int[] navalConns = new int[12]; Vector navals = new Vector(); Point[] navalIcons = new Point[12]; Rectangle[] stratAreaBounds; int[][] stratConnections; Point[] stratMapHotSpots; ConnectionData connData = new ConnectionData(); Rectangle[] stratArrowBounds; boolean delete = false; Bitmap[] stratAreaBitmaps; Bitmap[][] stratArrowBitmaps; Color yellColor = new Color(255,208,0); DrawArea drawA; DebriefArea debriefA; EditorArea editorA; Vector areas,dareas; Point start = new Point(-1,-1); Point end = new Point(-1,-1); int mouseDownAtRegion=-1; int maxX,maxY; int runs; Region tempR; Color transparent; Bitmap tempBitmap; int onRegion = -1; Vector points; int grabbedEditDiamond=-1; int grabbedEditNaval=-1; boolean showMain = true; boolean showDebrief = false; boolean showEditor = false; short[][] maskData; boolean maskOn = false; MenuItem ViewMenuViewStrat; MenuItem ViewMenuViewDebrief; MenuItem ViewMenuViewEditor; public static void main (String[] args) throws Exception { Strat mainW = new Strat(); mainW.setVisible(true); mainW.setWindowState(FormWindowState.MAXIMIZED); try {Application.run(mainW);} catch (Exception e) { //System.out.println(e); } } public Strat() { areas = new Vector(); dareas = new Vector(); formInit(); this.setWindowState(FormWindowState.MAXIMIZED); } public void formInit() { MenuItem FileMenuOpen = new MenuItem("Open");FileMenuOpen.addOnClick(new EventHandler(this.FileMenuOpen_click)); MenuItem FileMenuSave = new MenuItem("Save");FileMenuSave.addOnClick(new EventHandler(this.FileMenuSave_click)); MenuItem FileMenuSaveData = new MenuItem("Save data");FileMenuSaveData.addOnClick(new EventHandler(this.FileMenuSaveData_click)); MenuItem FileMenuOpenData = new MenuItem("Open data");FileMenuOpenData.addOnClick(new EventHandler(this.FileMenuOpenData_click)); MenuItem FileMenuExit = new MenuItem("Exit");FileMenuExit.addOnClick(new EventHandler(this.exitApp)); MenuItem FileMenu = new MenuItem("File",null,new MenuItem[]{FileMenuOpen,FileMenuOpenData,FileMenuSave,FileMenuSaveData,FileMenuExit}); MenuItem EditMenuMaskToggle = new MenuItem("Show mask");EditMenuMaskToggle.setChecked(maskOn);EditMenuMaskToggle.addOnClick(new EventHandler(this.EditMenuMaskToggle_click)); MenuItem EditMenuDetect = new MenuItem("Autodetect areas");EditMenuDetect.addOnClick(new EventHandler(this.doAutoDetect)); MenuItem EditMenuDetectConnections = new MenuItem("Autodetect connections");EditMenuDetectConnections.addOnClick(new EventHandler(this.doConnectionDetect)); MenuItem EditMenuText = new MenuItem("Export text");EditMenuText.addOnClick(new EventHandler(this.exportText)); MenuItem EditMenu = new MenuItem("Edit",null,new MenuItem[]{EditMenuMaskToggle,EditMenuDetect,EditMenuDetectConnections,EditMenuText}); ViewMenuViewStrat = new MenuItem("Main stratmap");ViewMenuViewStrat.setChecked(true);ViewMenuViewStrat.addOnClick(new EventHandler(this.ViewMenuChange)); ViewMenuViewDebrief = new MenuItem("Debrief map");ViewMenuViewDebrief.setChecked(false);ViewMenuViewDebrief.addOnClick(new EventHandler(this.ViewMenuChange)); ViewMenuViewEditor = new MenuItem("Scenarioeditor map");ViewMenuViewEditor.setChecked(false);ViewMenuViewEditor.addOnClick(new EventHandler(this.ViewMenuChange)); MenuItem ViewMenu = new MenuItem("View",null,new MenuItem[]{ViewMenuViewStrat,ViewMenuViewDebrief,ViewMenuViewEditor}); int i; MainMenu myMenu = new MainMenu(new MenuItem[]{FileMenu,EditMenu,ViewMenu}); drawA = new DrawArea(); debriefA = new DebriefArea(); editorA = new EditorArea(); this.setMenu(myMenu); this.setAutoScroll(true); this.add(drawA); } public void FileMenuOpenData_click(Object sender,Event e) { this.setCursor(Cursor.WAIT); areas = new Vector(); drawA.repaint(); loadData(); this.setCursor(Cursor.DEFAULT); drawA.repaint(); } public void FileMenuSaveData_click(Object sender,Event e) { this.setCursor(Cursor.WAIT); saveFile(); this.setCursor(Cursor.DEFAULT); drawA.repaint(); } public void FileMenuSave_click(Object sender,Event e) { this.setCursor(Cursor.WAIT); short[][][] arrowTgaData = connData.getArrowTgaData(); short[][] areaTgaData = getMapAreaTgaData(); getDAreaTgaData(); int erase = maxMaps - areas.size(); File exeFile = File.open("cc5.exe"); exeFile.setPosition(0x12A728); for ( int i = 0 ; i < areas.size() ; i++ ) { // maparea data StratArea currentArea = (StratArea)(areas.elementAt(i)); exeFile.writeInt(i); exeFile.writeInt(870+i); exeFile.writeInt(16); exeFile.writeInt(currentArea.area.getBounds().x); exeFile.writeInt(currentArea.area.getBounds().y); exeFile.writeInt(currentArea.area.getBounds().width); exeFile.writeInt(currentArea.area.getBounds().height); } exeFile.setPosition(0x12AD60); for ( int i = 0 ; i < connData.connections.size() ; i++ ) { // arrow data exeFile.writeInt(i); exeFile.writeInt(782+i); exeFile.writeInt(17); exeFile.writeInt(arrowBounds[i].x); exeFile.writeInt(arrowBounds[i].y); exeFile.writeInt(arrowBounds[i].width); exeFile.writeInt(arrowBounds[i].height); } exeFile.setPosition(0x12ABF8); for ( int i = 0 ; i < areas.size() ; i++ ) { // area hotspots, ie. centers StratArea currentArea = (StratArea)(areas.elementAt(i)); exeFile.writeInt(currentArea.getCenter().x); exeFile.writeInt(currentArea.getCenter().y); } exeFile.setPosition(0x12D650); for ( int i = 0 ; i < areas.size() ; i++ ) { // connection data StratArea currentArea = (StratArea)(areas.elementAt(i)); exeFile.writeInts(currentArea.getConnectionArray()); } exeFile.setPosition(0x126AA8); for ( int i = 0 ; i < dareas.size() ; i++ ) { //debrief areas DArea currentArea = (DArea)(dareas.elementAt(i)); exeFile.writeInt(i); exeFile.writeInt(264+i); exeFile.writeInt(0); exeFile.writeInt(currentArea.area.getBounds().x+218); exeFile.writeInt(currentArea.area.getBounds().y+72); exeFile.writeInt(currentArea.area.getBounds().width+218); exeFile.writeInt(currentArea.area.getBounds().height+72); } exeFile.setPosition(0x12618C); for ( int i = 0 ; i < 12 ; i++ ) { // naval icons Point aNaval = (Point)(navals.elementAt(i)); exeFile.writeInt(133+i); exeFile.writeInt(715); exeFile.writeInt(0); exeFile.writeInt(aNaval.x); exeFile.writeInt(aNaval.y); exeFile.writeInt(32); exeFile.writeInt(15); } exeFile.setPosition(0x138C90); for ( int i = 0 ; i < 12 ; i++ ) { // maps getting naval bombardment exeFile.writeInt(navalConns[i]); } exeFile.setPosition(0x1257EC); for ( int i = 0 ; i < areas.size() ; i++ ) { // editor map places Point aNaval = (Point)(( (StratArea)(areas.elementAt(i))).editCentroid); aNaval = new Point(aNaval.x-15,aNaval.y-8); exeFile.writeInt(45+i); exeFile.writeInt(659); exeFile.writeInt(0); exeFile.writeInt(aNaval.x); exeFile.writeInt(aNaval.y); exeFile.writeInt(30); exeFile.writeInt(17); } exeFile.close(); this.setCursor(Cursor.DEFAULT); } public void FileMenuOpen_click(Object sender,Event e) { drawA.graphBitmap = loadTga("graph.tga"); drawA.maskBitmap = loadTga("mask.tga"); maskData = getTgaArray("mask.tga"); for ( int o = 0 ; o < 12 ; o++ ) { navals.addElement(new Point(140+o*40,40)); navalConns[o] = -1; } usDebrief = loadTga("usDebrief.tga"); bothDebrief = loadTga("bothDebrief.tga"); geDebrief = loadTga("geDebrief.tga"); editorA.graphBitmap = loadTga("egraph.tga"); debriefA.graphBitmap = loadTga("dgraph.tga"); debriefA.maskBitmap = loadTga("dmask.tga"); Graphics tempG = drawA.maskBitmap.getGraphics(); transparent = tempG.getPixel(0,0); //System.out.println("Transparent color is " + transparent); drawA.graphBitmap.setTransparent(true); drawA.graphBitmap.setTransparentColor(transparent); //drawA.maskBitmap = new Bitmap("mask.bmp"); drawA.addOnMouseDown(new MouseEventHandler(DrawArea_down)); drawA.addOnMouseMove(new MouseEventHandler(DrawArea_move)); drawA.addOnMouseUp(new MouseEventHandler(DrawArea_up)); maxX = drawA.maskBitmap.getSize().x; maxY = drawA.maskBitmap.getSize().y; //System.out.println(maxX + " " + maxY); drawA.repaint(); } public String getFileToSave(String title) { SaveFileDialog ofd = new SaveFileDialog(); ofd.setInitialDir("."); //ofd.setDefaultExt("tga"); //ofd.setFilter("Tga files (*.tga)|*.tga"); //ofd.setFilterIndex(1); String inFileName; ofd.setTitle(title); int dlgResult = ofd.showDialog(); if (dlgResult == DialogResult.OK) { inFileName = ofd.getFileName(); return(inFileName); } else return(null); } public String getFileToLoad(String title) { OpenFileDialog ofd = new OpenFileDialog(); ofd.setInitialDir("."); //ofd.setDefaultExt("tga"); //ofd.setFilter("Tga files (*.tga)|*.tga"); //ofd.setFilterIndex(1); String inFileName; ofd.setTitle(title); int dlgResult = ofd.showDialog(); if (dlgResult == DialogResult.OK) { inFileName = ofd.getFileName(); return(inFileName); } else return(null); } public short[][] getTgaArray(String filename) { File tgaFile = File.open(filename); int i,j; tgaFile.setPosition(12); int width = tgaFile.readShort(); int height =tgaFile.readShort(); short[] dimArray = new short[width*height]; short[][] tgaArray = new short[width][height]; tgaFile.setPosition(18); int k=0; tgaFile.readShorts(dimArray,0,width*height); for ( j = height-1 ; j > -1 ; j-- ) { for ( i = 0 ; i < width ; i++ ) { tgaArray[i][j] = dimArray[k]; k++; } } tgaFile.close(); return(tgaArray); } public Bitmap loadTga(String fileName) { Bitmap toReturn; File tgaFile = File.open(fileName); tgaFile.setPosition(12); int width = tgaFile.readShort(); int height = tgaFile.readShort(); tgaFile.setPosition(18); int plus = 0; while ( (plus+width)%4 != 0 ) plus++; short[] data = new short[(width+plus)*height]; for ( int j = 0 ; j < height ; j++ ) { tgaFile.readShorts(data,j*(width+plus),width); } //System.out.println(data[0]); int i=0; MemoryStream bmp = new MemoryStream(); bmp.write(0x42); bmp.write(0x4d); int dataSize = width*height*2; while ( dataSize % 4 != 0 ) dataSize++; bmp.writeInt(0x36+dataSize); bmp.writeInt(0); bmp.writeInt(0x36); bmp.writeInt(0x28); bmp.writeInt(width); bmp.writeInt(height); bmp.writeShort((short)1); bmp.writeShort((short)16); bmp.writeInt(0); bmp.writeInt(dataSize); bmp.writeInt(0);bmp.writeInt(0); bmp.writeInt(0);bmp.writeInt(0); bmp.writeShorts(data); bmp.setPosition(0); toReturn = new Bitmap(bmp); tgaFile.close(); bmp.close(); return(toReturn); } public void DrawArea_up(Object sender, MouseEvent e) { start = new Point(-1,-1); end = start; Point upAt = new Point(e.x,e.y); int result = isRegion(upAt); if ( mouseDownAtRegion != -1 && result != -1 && result != mouseDownAtRegion && (e.button == MouseButton.LEFT )) { ((StratArea)(areas.elementAt(mouseDownAtRegion))).addConnection(result); ((StratArea)(areas.elementAt(result))).addConnection(mouseDownAtRegion); mouseDownAtRegion = -1; } if ( mouseDownAtRegion != -1 && result != -1 && result != mouseDownAtRegion && (e.button == MouseButton.RIGHT )) { delete = false; ((StratArea)(areas.elementAt(mouseDownAtRegion))).removeConnection(result); ((StratArea)(areas.elementAt(result))).removeConnection(mouseDownAtRegion); mouseDownAtRegion = -1; } } public void DrawArea_move(Object sender, MouseEvent e) { Point mouseAt = new Point(e.x,e.y); int result = isRegion(mouseAt); if ( mouseDownAtRegion != -1 && result != -1 && result != mouseDownAtRegion && (e.button == MouseButton.LEFT ) ) { end = mouseAt; drawA.repaint(); } onRegion = -1; if ( (e.button == MouseButton.NONE ) ) { onRegion = result; } if ( mouseDownAtRegion != -1 && result != -1 && result != mouseDownAtRegion && (e.button == MouseButton.RIGHT ) ) { delete = true; end = mouseAt; drawA.repaint(); } drawA.repaint(); } public void DrawArea_down(Object sender, MouseEvent e) { Point downAt = new Point(e.x,e.y); int reg; if ( ( reg = isRegion(downAt)) == -1 ) { this.setCursor(Cursor.WAIT); Region newArea = getArea(downAt); if ( newArea == null ) return; if ( areas.size() < maxMaps ) { areas.addElement(new StratArea(newArea)); drawA.repaint(); } this.setCursor(Cursor.DEFAULT); } else { mouseDownAtRegion = reg; start = ((StratArea)(areas.elementAt(reg))).getCenter(); } } public Point isAlmostNaval(Point aPoint) { for ( int i = 0 ; i < navals.size() ; i ++ ) { Point aNaval = (Point)navals.elementAt(i); if ( aNaval.equals(aPoint) == false ) { if ( Math.abs(aPoint.x-aNaval.x) < 5 && Math.abs(aPoint.y-aNaval.y) < 5 ) { return(aNaval); } } } return(null); } public void EditorArea_up(Object sender, MouseEvent e) { Point aPoint = new Point(e.x,e.y); if ( (e.button == MouseButton.LEFT && grabbedEditNaval != -1 ) ) { Point newPoint = isAlmostNaval(new Point(aPoint.x,aPoint.y)); if ( newPoint != null && new Point(aPoint.x,aPoint.y).equals(newPoint) == false ) { navals.setElementAt(new Point(newPoint.x,newPoint.y),grabbedEditNaval); } } if ( navalDragPoint.x != -1 && navalDragPoint.y != -1 && navalStartPoint.x != -1 && navalStartPoint.y != -1) { int diamond; int naval; if ( (diamond = isDiamond(aPoint)) != -1 ) { naval = isNaval(navalStartPoint); boolean setN = true; for ( int i = 0 ; i < navalConns.length ; i++ ) { if ( navalConns[i] == diamond ) setN = false; } if ( setN ) navalConns[naval] = diamond; else { if ( navalConns[naval] == diamond ) navalConns[naval] = -1; } } } grabbedEditDiamond=-1; grabbedEditNaval=-1; navalDragPoint = new Point(-1,-1); navalStartPoint = new Point(-1,-1); } public void EditorArea_move(Object sender, MouseEvent e) { Point aPoint = new Point(e.x,e.y); System.out.println(aPoint); if ( (e.button == MouseButton.LEFT && grabbedEditDiamond != -1 ) ) { System.out.println("Moving diamond #"+grabbedEditDiamond); StratArea currentArea = (StratArea)(areas.elementAt(grabbedEditDiamond)); currentArea.setEditCentroid(aPoint); } if ( (e.button == MouseButton.LEFT && grabbedEditNaval != -1 ) ) { System.out.println("Moving Naval #"+grabbedEditNaval); navals.setElementAt(new Point(aPoint.x,aPoint.y),grabbedEditNaval); } if ( (e.button == MouseButton.RIGHT && grabbedEditNaval != -1 ) ) { System.out.println("Moving Naval #"+grabbedEditNaval); navalDragPoint = aPoint; } editorA.repaint(); } public void EditorArea_down(Object sender, MouseEvent e) { Point aPoint = new Point(e.x,e.y); int temp; grabbedEditDiamond=isDiamond(aPoint); if ( grabbedEditDiamond == -1 ) grabbedEditNaval = isNaval(aPoint); if ( (e.button == MouseButton.RIGHT && grabbedEditNaval != -1 ) ) navalStartPoint = aPoint; } public int isNaval(Point aPoint) { for ( int i = 0 ; i < navals.size() ; i ++ ) { Point aNaval = (Point)navals.elementAt(i); if ( Math.abs(aPoint.x-aNaval.x) < 32 && Math.abs(aPoint.y-aNaval.y) < 15) { return(i); } } return(-1); } public int isDiamond(Point aPoint) { for ( int i = 0 ; i < areas.size() ; i ++ ) { StratArea anArea = (StratArea)(areas.elementAt(i)); if ( Math.abs(aPoint.x-anArea.editCentroid.x) < 15 && Math.abs(aPoint.y-anArea.editCentroid.y) < 8) { return(i); } } return(-1); } public void DebriefArea_up(Object sender, MouseEvent e) { } public void DebriefArea_move(Object sender, MouseEvent e) { } public void DebriefArea_down(Object sender, MouseEvent e) { Point downAt = new Point(e.x,e.y); int reg; if ( ( reg = isDRegion(downAt)) == -1 ) { this.setCursor(Cursor.WAIT); Region newArea = getArea(downAt); if ( newArea == null ) return; if ( dareas.size() < maxMaps ) { dareas.addElement(new DArea(newArea)); debriefA.repaint(); } this.setCursor(Cursor.DEFAULT); } } public Region getArea(Point origP) { if ( showMain ) maskData = getTgaArray("mask.tga"); else maskData = getTgaArray("dmask.tga"); maxX = maskData.length; maxY = maskData[0].length; if ( showMain ) Graphics tempG = drawA.maskBitmap.getGraphics(); else Graphics tempG = debriefA.maskBitmap.getGraphics(); tempR = new Region(); int oldColor = maskData[origP.x][origP.y]; //System.out.println("Color at mouseDown " + new Color(oldColor)); if ( oldColor == maskData[0][0] ){ this.setCursor(Cursor.DEFAULT); return(null); } runs = 0; seedFill(oldColor,origP.x,origP.y); //System.out.println("Runs " + runs); return(tempR); } public void seedFill ( int old,int X,int Y ) { if ( tempR.contains(X,Y) ) return; try { int start=X,end=X,i,j; if ( maskData[X][Y] == old ) { while ( start > 0 && maskData[start][Y] == old ) {start--;} while ( end < maxX && maskData[end][Y] == old ) {end++; } } else return; tempR = tempR.orWith(Region.createRectangular(start,Y,end-start,1)); int xr; if ( Y +1 < maxY ) for ( xr = start+1 ; xr < end ; xr++ ) seedFill(old,xr,Y+1); if ( Y -1 > -1 ) for ( xr = start+1 ; xr < end ; xr++ ) seedFill(old,xr,Y-1); runs++; } catch ( Exception sErr ) { //System.out.println(sErr); } } public int isDRegion(Point testP) { for ( int i = 0 ; i < dareas.size() ; i++ ) { if ( ( (DArea)(dareas.elementAt(i))).contains(testP) ) return(i); } return(-1); } public int isRegion(Point testP) { for ( int i = 0 ; i < areas.size() ; i++ ) { if ( ( (StratArea)(areas.elementAt(i))).contains(testP) ) return(i); } return(-1); } public void EditMenuMaskToggle_click(Object sender,Event e) { ((MenuItem)sender).setChecked(!maskOn); maskOn = !maskOn; drawA.repaint(); debriefA.repaint(); } public void ViewMenuChange(Object sender,Event e) { MenuItem source = ((MenuItem)sender); if ( source.equals(ViewMenuViewEditor ) ) { showEditor = !showEditor; ViewMenuViewEditor.setChecked(true); ViewMenuViewDebrief.setChecked(false);showDebrief = ViewMenuViewDebrief.getChecked(); ViewMenuViewStrat.setChecked(false);showMain = ViewMenuViewStrat.getChecked(); debriefA.setLeft(0);debriefA.setTop(0); this.remove(debriefA); debriefA.removeOnMouseDown(new MouseEventHandler(DebriefArea_down)); debriefA.removeOnMouseMove(new MouseEventHandler(DebriefArea_move)); debriefA.removeOnMouseUp(new MouseEventHandler(DebriefArea_up)); drawA.setLeft(0);drawA.setTop(0); this.remove(drawA); drawA.removeOnMouseDown(new MouseEventHandler(DrawArea_down)); drawA.removeOnMouseMove(new MouseEventHandler(DrawArea_move)); drawA.removeOnMouseUp(new MouseEventHandler(DrawArea_up)); this.update(); this.add(editorA); editorA.addOnMouseDown(new MouseEventHandler(EditorArea_down)); editorA.addOnMouseMove(new MouseEventHandler(EditorArea_move)); editorA.addOnMouseUp(new MouseEventHandler(EditorArea_up)); editorA.repaint(); } if ( source.equals(ViewMenuViewStrat) ) { if ( !showMain ) { showMain = !showMain; ViewMenuViewStrat.setChecked(true); ViewMenuViewDebrief.setChecked(false);showDebrief = ViewMenuViewDebrief.getChecked(); ViewMenuViewEditor.setChecked(false);showEditor = ViewMenuViewEditor.getChecked(); debriefA.setLeft(0);debriefA.setTop(0); this.remove(debriefA); debriefA.removeOnMouseDown(new MouseEventHandler(DebriefArea_down)); debriefA.removeOnMouseMove(new MouseEventHandler(DebriefArea_move)); debriefA.removeOnMouseUp(new MouseEventHandler(DebriefArea_up)); editorA.setLeft(0);editorA.setTop(0); this.remove(editorA); editorA.removeOnMouseDown(new MouseEventHandler(EditorArea_down)); editorA.removeOnMouseMove(new MouseEventHandler(EditorArea_move)); editorA.removeOnMouseUp(new MouseEventHandler(EditorArea_up)); this.update(); this.add(drawA); drawA.addOnMouseDown(new MouseEventHandler(DrawArea_down)); drawA.addOnMouseMove(new MouseEventHandler(DrawArea_move)); drawA.addOnMouseUp(new MouseEventHandler(DrawArea_up)); drawA.repaint(); } } if ( source.equals(ViewMenuViewDebrief) ) { if ( !showDebrief ) { showDebrief = !showDebrief; ViewMenuViewStrat.setChecked(false);showMain = ViewMenuViewStrat.getChecked(); ViewMenuViewDebrief.setChecked(true); ViewMenuViewEditor.setChecked(false);showEditor = ViewMenuViewEditor.getChecked(); drawA.setLeft(0);drawA.setTop(0); this.remove(drawA); drawA.removeOnMouseDown(new MouseEventHandler(DrawArea_down)); drawA.removeOnMouseMove(new MouseEventHandler(DrawArea_move)); drawA.removeOnMouseUp(new MouseEventHandler(DrawArea_up)); editorA.setLeft(0);editorA.setTop(0); this.remove(editorA); editorA.removeOnMouseDown(new MouseEventHandler(EditorArea_down)); editorA.removeOnMouseMove(new MouseEventHandler(EditorArea_move)); editorA.removeOnMouseUp(new MouseEventHandler(EditorArea_up)); this.update(); this.add(debriefA); debriefA.addOnMouseDown(new MouseEventHandler(DebriefArea_down)); debriefA.addOnMouseMove(new MouseEventHandler(DebriefArea_move)); debriefA.addOnMouseUp(new MouseEventHandler(DebriefArea_up)); debriefA.repaint(); } } } public void exitApp(Object sender,Event e) { Application.exit(); } public double distance(Point a,Point b) { double aDist = Math.sqrt( (double) ((Math.abs(a.x-b.x))*(Math.abs(a.x-b.x)) + (Math.abs(a.y-b.y))*(Math.abs(a.y-b.y)) )); return(aDist); } public Region getEdge(Region area) { int pixelCount=0; Region edgePixels = new Region(); Region edgeArea = new Region(); double maxDist = 2.5; Rectangle bounds = area.getBounds(); //System.out.println("Bounds " + bounds); for ( int i = bounds.x ; i < bounds.x+bounds.width ; i++ ) { for ( int j = bounds.y ; j < bounds.y+bounds.height ; j++ ) { if ( area.contains(i,j) && ( area.contains(i-1,j) == false || area.contains(i+1,j) == false || area.contains(i,j-1) == false || area.contains(i,j+1) == false) ) { edgePixels = edgePixels.orWith(Region.createRectangular(i,j,1,1)); } } } //System.out.println("EDGEPIXELS " + edgePixels.getBounds() + " Containing " + pixelCount + " pixels"); edgePixels = edgePixels.orWith(edgePixels.offset(0,-1)).orWith(edgePixels.offset(0,1)).orWith(edgePixels.offset(-1,0)).orWith(edgePixels.offset(1,0)); //edgePixels = edgePixels.orWith(edgePixels.offset(0,-2)).orWith(edgePixels.offset(0,2)).orWith(edgePixels.offset(-2,0)).orWith(edgePixels.offset(2,0)); //edgePixels = edgePixels.orWith(edgePixels.offset(-3,-3)).orWith(edgePixels.offset(3,3)).orWith(edgePixels.offset(-3,3)).orWith(edgePixels.offset(3,-3)); edgePixels = edgePixels.andWith(area); return(edgePixels); } public short[] bitmapToTgaData(Bitmap bitmap) { int width = bitmap.getSize().x; int height = bitmap.getSize().y; short[] data = new short[width*height]; int count=0; Graphics tempG = bitmap.getGraphics(); for ( int j = height-1 ; j > -1 ; j-- ) { for ( int i = 0 ; i < width -1 ; i++ ) { Color tempColor = tempG.getPixel(i,j); if ( tempColor.equals(Color.BLACK) ) { data[count] = 0x0000; count++; } if ( tempColor.equals(Color.WHITE) ) { data[count] = 0x7FFF; count++; } if ( tempColor.equals(yellColor) ) { data[count] = 0x7F40; count++; } } } return(data); } public Point translate(Point aPoint, Point origin) { Point toReturn = new Point(aPoint.x-origin.x,aPoint.y-origin.y); return(toReturn); } public void getDAreaTgaData() { short[][][] data = new short[dareas.size()][3][]; for ( int i = 0 ; i < dareas.size() ; i++ ) { Region mapArea = ((DArea)(dareas.elementAt(i))).area; Rectangle origRect = mapArea.getBounds(); Bitmap mapBitmap = new Bitmap(origRect.width,origRect.height); Rectangle targetRect = new Rectangle(0,0,mapBitmap.getSize().x,mapBitmap.getSize().y); mapArea = mapArea.offset(-1*origRect.x,-1*origRect.y); Graphics tempG = mapBitmap.getGraphics(); tempG.setBackColor(Color.WHITE); tempG.clearRect(0,0,mapArea.getBounds().width,mapArea.getBounds().height); tempG.drawImage(usDebrief,origRect,targetRect,false); tempG.fill(mapArea.xorWith(Region.createRectangular(targetRect)),new Brush(Color.WHITE,BrushStyle.SOLID)); data[i][0] = connData.bitmapToTgaData2(mapBitmap); tempG.clearRect(0,0,mapArea.getBounds().width,mapArea.getBounds().height); tempG.drawImage(geDebrief,origRect,targetRect,false); tempG.fill(mapArea.xorWith(Region.createRectangular(targetRect)),new Brush(Color.WHITE,BrushStyle.SOLID)); data[i][1] = connData.bitmapToTgaData2(mapBitmap); tempG.clearRect(0,0,mapArea.getBounds().width,mapArea.getBounds().height); tempG.drawImage(bothDebrief,origRect,targetRect,false); tempG.fill(mapArea.xorWith(Region.createRectangular(targetRect)),new Brush(Color.WHITE,BrushStyle.SOLID)); data[i][2] = connData.bitmapToTgaData2(mapBitmap); for ( int kk = 0 ; kk < 3 ; kk++ ) { String indexS = new String(Integer.toString(i)); while ( indexS.length() < 2 ) indexS = "0" + indexS; indexS = "DESTRATMAP" + indexS + "_00" + kk + ".tga"; File tgaFile = File.create(indexS); tgaFile.writeInt(0x00020000); tgaFile.writeInt(0); tgaFile.writeInt(0); tgaFile.writeShort((short)(mapBitmap.getSize().x)); tgaFile.writeShort((short)(mapBitmap.getSize().y)); tgaFile.writeShort((short)0x0110); tgaFile.writeShorts(data[i][kk]); tgaFile.close(); } } } public short[][] getMapAreaTgaData() { short[][] data = new short[areas.size()][]; for ( int i = 0 ; i < areas.size() ; i++ ) { Region mapArea = ((StratArea)(areas.elementAt(i))).area; Region edgeArea = ((StratArea)(areas.elementAt(i))).edgeArea; Bitmap mapBitmap = new Bitmap(mapArea.getBounds().width,mapArea.getBounds().height); mapArea = mapArea.offset(-1*mapArea.getBounds().x,-1*mapArea.getBounds().y); edgeArea = edgeArea.offset(-1*edgeArea.getBounds().x,-1*edgeArea.getBounds().y); Graphics tempG = mapBitmap.getGraphics(); tempG.setBackColor(Color.WHITE); tempG.clearRect(0,0,mapArea.getBounds().width,mapArea.getBounds().height); tempG.fill(mapArea,new Brush(Color.BLACK,BrushStyle.SOLID)); tempG.fill(edgeArea,new Brush(yellColor,BrushStyle.SOLID)); data[i] = connData.bitmapToTgaData2(mapBitmap); String indexS = new String(Integer.toString(i)); while ( indexS.length() < 2 ) indexS = "0" + indexS; indexS = "STRAMAPAREA" + indexS + "_000.tga"; File tgaFile = File.create(indexS); tgaFile.writeInt(0x00020000); tgaFile.writeInt(0); tgaFile.writeInt(0); tgaFile.writeShort((short)(mapBitmap.getSize().x)); tgaFile.writeShort((short)(mapBitmap.getSize().y)); tgaFile.writeShort((short)0x0110); tgaFile.writeShorts(data[i]); tgaFile.close(); } return(data); } public void exportText(Object sender, Event e) { File outFile = File.create(getFileToSave("Save text...")); TextWriter out = new TextWriter(outFile); out.write("Total number of maps is " + areas.size()); for ( int i = 0 ; i < areas.size() ; i++ ) { StratArea currentArea = (StratArea)(areas.elementAt(i)); out.write("\n\n[Map # " + i + "]\n"); out.write("\nCenter at : " + currentArea.getCenter()); out.write("\nBounding rectangle : " + currentArea.area.getBounds() ); int[] con = currentArea.getConnections(); out.write("\nConnected to :"); for ( int j = 0 ; j < con.length ; j++ ) { out.write("\nMap # " + con[j] + ", arrow file is # " + connData.findIndex(i,con[j])); } } out.close(); outFile.close(); } public void doAutoDetect(Object sender,Event e) { this.setCursor(Cursor.WAIT); int width = 0 ; int height = 0; Graphics tempG; int jump = 10; if ( showMain ) { areas = new Vector(); width = drawA.maskBitmap.getSize().x; height = drawA.maskBitmap.getSize().y; tempG = drawA.maskBitmap.getGraphics(); int count = 0; for ( int j = 0 ; j < height ; j = j + jump ) { //System.out.println(j); for ( int i = 0 ; i < width ; i = i + jump ) { if ( tempG.getPixel(i,j).equals(Color.WHITE) == false ) if ( showMain ) { this.DrawArea_down(drawA,new MouseEvent(MouseButton.LEFT,1,i,j,0)); if ( areas.size() > maxMaps-1 ) { j = height;i = width;} } else { this.DebriefArea_down(drawA,new MouseEvent(MouseButton.LEFT,1,i,j,0)); if ( dareas.size() > maxMaps-1 ) { j = height;i = width;} } } } } else { dareas = new Vector(); width = debriefA.maskBitmap.getSize().x; height = debriefA.maskBitmap.getSize().y; tempG = debriefA.maskBitmap.getGraphics(); jump = 1; int count = 0; for ( int j = 0 ; j < areas.size() ; j++ ) { Point center = ((StratArea)(areas.elementAt(j))).getCenter(); //System.out.println(center); this.DebriefArea_down(drawA,new MouseEvent(MouseButton.LEFT,1,(int)(center.x*((double)559/(double)1708)),(int)(center.y*((double)559/(double)1708)),0)); } } //System.out.println("Autodetect done"); this.setCursor(Cursor.DEFAULT); } public void doConnectionDetect(Object sender, Event e) { this.setCursor(Cursor.WAIT); for ( int i = 0 ; i < areas.size() ; i++ ) { //System.out.println(i); Region tempArea = ((StratArea)(areas.elementAt(i))).edgeArea; StratArea currentArea = ((StratArea)(areas.elementAt(i))); int startX = tempArea.getBounds().x; int startY = tempArea.getBounds().y; int endX = startX + tempArea.getBounds().width; int endY = startY + tempArea.getBounds().height; int[] neighbours = new int[maxMaps]; int x,y; int temp; for ( y = startY ; y < endY ; y++ ) { for ( x = startX ; x < endX ; x++ ) { if ( tempArea.contains(x,y)) { if ( (temp = isRegion(new Point(x-1,y)) )!= -1 ) { neighbours[temp]++; } if ( (temp = isRegion(new Point(x+1,y)) )!= -1 ) { neighbours[temp]++; } if ( (temp = isRegion(new Point(x,y-1)) )!= -1 ) { neighbours[temp]++; } if ( (temp = isRegion(new Point(x,y+1)) )!= -1 ) { neighbours[temp]++; } } } } for ( int index = 0 ; index < maxMaps ; index++ ) { if ( neighbours[index] > 15 ) currentArea.addConnection(index); } } this.setCursor(Cursor.DEFAULT); } public void loadData() { File in = (File.open(getFileToLoad("Select data file to load..."))); int areaCount = in.readInt(); for ( int i = 0 ; i < areaCount ; i++ ) { Point editCentroid = new Point(in.readInt(),in.readInt()); byte[] connects = new byte[8]; connects = in.readBytes(8); Region tempArea = new Region(); int originX = in.readInt(); int originY = in.readInt(); boolean breaker = false; int width = in.readInt()-originX; int height =in.readInt()-originY; //System.out.println(originX + "," + originY); //System.out.println(width + "," + height); int count = 1; Region temp2 = Region.createRectangular(0,0,1,1); for ( int j = 0 ; j < height ; j++ ) { byte[] line = new byte[width]; line = in.readBytes(width); for ( int k = 0 ; k < width ; k++ ) { if ( line[k] == 1 ) { count =1; int start = k; while ( k < width && line[k] == 1 ) { count++; k++; } tempArea = tempArea.orWith(Region.createRectangular(originX+start,originY+j,count,1)); } } } System.out.println(tempArea.getBounds()); StratArea toAdd = new StratArea(tempArea); toAdd.editCentroid = editCentroid; areas.addElement(toAdd); for ( int l = 0 ; l < 8 ; l++ ) { if ( connects[l] != -1 ) toAdd.addConnection(connects[l]); } } int dareaCount = in.readInt(); for ( int i = 0 ; i < dareaCount ; i++ ) { Region tempArea = new Region(); int originX = in.readInt(); int originY = in.readInt(); boolean breaker = false; int width = in.readInt()-originX; int height =in.readInt()-originY; //System.out.println(originX + "," + originY); //System.out.println(width + "," + height); int count = 1; Region temp2 = Region.createRectangular(0,0,1,1); for ( int j = 0 ; j < height ; j++ ) { byte[] line = new byte[width]; line = in.readBytes(width); for ( int k = 0 ; k < width ; k++ ) { if ( line[k] == 1 ) { count =1; int start = k; while ( k < width && line[k] == 1 ) { count++; k++; } tempArea = tempArea.orWith(Region.createRectangular(originX+start,originY+j,count,1)); } } } DArea toAdd = new DArea(tempArea); dareas.addElement(toAdd); } for ( int i = 0 ; i < 12 ; i++ ) { Point aPoint; navalConns[i] = in.readInt(); aPoint = new Point(in.readInt(),in.readInt()); navals.setElementAt(aPoint,i); } in.close(); } public void saveFile() { Rectangle bounds; File out = File.create("map001.sav"); out.writeInt(areas.size()); for ( int i = 0 ; i < areas.size() ; i++ ) { StratArea currentArea = (StratArea)(areas.elementAt(i)); out.writeInt(currentArea.editCentroid.x); out.writeInt(currentArea.editCentroid.y); int[] connects = currentArea.getConnections(); for ( int j = 0 ; j < 8 ; j++ ) { if ( j < connects.length ) { out.write(connects[j]); } else { out.write(-1); } } bounds = currentArea.area.getBounds(); int x1 = bounds.x; int x2 = bounds.x+bounds.width; int y1 = bounds.y; int y2 = bounds.y+bounds.height; out.writeInt(x1);out.writeInt(y1); out.writeInt(x2);out.writeInt(y2); for ( int y = y1 ; y < y2 ; y++ ) { for ( int x = x1 ; x < x2 ; x++ ) { if ( currentArea.area.contains(x,y) ) { out.write(1); } else { out.write(0); } } } } out.writeInt(dareas.size()); for ( int i = 0 ; i < dareas.size() ; i++ ) { DArea currentArea = (DArea)(dareas.elementAt(i)); bounds = currentArea.area.getBounds(); int x1 = bounds.x; int x2 = bounds.x+bounds.width; int y1 = bounds.y; int y2 = bounds.y+bounds.height; out.writeInt(x1);out.writeInt(y1); out.writeInt(x2);out.writeInt(y2); for ( int y = y1 ; y < y2 ; y++ ) { for ( int x = x1 ; x < x2 ; x++ ) { if ( currentArea.area.contains(x,y) ) { out.write(1); } else { out.write(0); } } } } for ( int i = 0 ; i < 12 ; i++ ) { Point aPoint = (Point)(navals.elementAt(i)); out.writeInt(navalConns[i]); out.writeInt(aPoint.x); out.writeInt(aPoint.y); } out.close(); } public class ConnectionData { Vector connections; public ConnectionData() { connections = new Vector(); } public int findIndex(int index1,int index2) { for ( int i = 0 ; i < connections.size() ; i++ ) { Point test = (Point)(connections.elementAt(i)); if ( ( test.x == index1 && test.y == index2 ) || (test.x == index2 && test.y == index1) ) { return(i); } } return(-1); } public Point getConnection(int index) { if ( index < connections.size() ) return((Point)(connections.elementAt(index))); else return(new Point(-1,-1)); } public boolean areConnected(int index1,int index2) { for ( int i = 0 ; i < connections.size() ; i++ ) { Point test = (Point)connections.elementAt(i); if ( ( test.x == index1 && test.y == index2 ) || (test.x == index2 && test.y == index1) ) { return(true); } } return(false); } public short[] bitmapToTgaData2(Bitmap bitmap) { int width = bitmap.getSize().x; int height = bitmap.getSize().y; short[] data = new short[width*height]; int count=0; Graphics tempG = bitmap.getGraphics(); for ( int j = 0 ; j < height ; j++ ) { for ( int i = 0 ; i < width ; i++ ) { Color tempColor = tempG.getPixel(i,j); int color32bit = tempColor.getRGB(); int color16bit = ((((color32bit >> 16)&0xff) >> 3 )&0x1f ) | ((((color32bit >> 8)&0xff) >> 3 )&0x1f ) << 5| ((((color32bit)& 0xff) >> 3 )&0x1f) << 10; data[count] = (short)color16bit; count++; } } return(data); } public short[][][] getArrowTgaData ( ) { short[][][] data = new short[connections.size()][2][]; arrowBounds = new Rectangle[connections.size()]; for ( int k = 0 ; k < connections.size() ; k++ ) { Point conn = (Point)(this.connections.elementAt(k)); StratArea area1 = (StratArea)(areas.elementAt(conn.x)); StratArea area2 = (StratArea)(areas.elementAt(conn.y)); int width = (int)(Math.abs(area2.getCenter().x-area1.getCenter().x)); int height =(int)(Math.abs(area2.getCenter().y-area1.getCenter().y)); Point origin = new Point(0,0); Point center1 = area1.getCenter(); Point center2 = area2.getCenter(); if ( center2.x >= center1.x ) { origin.x = center1.x-20; } else origin.x = center2.x-20; if ( center2.y >= center1.y ) { origin.y = center1.y-20; } else origin.y = center2.y-20; arrowBounds[k] = new Rectangle(origin.x,origin.y,width+40,height+40); Bitmap arrow1 = new Bitmap(width+40,height+40); Bitmap arrow2 = new Bitmap(width+40,height+40); Rectangle rect = new Rectangle(0,0,arrow1.getSize().x,arrow1.getSize().y); center1 = translate(center1,origin); center2 = translate(center2,origin); Graphics tempG = arrow1.getGraphics(); tempG.setBackColor(Color.WHITE); tempG.clearRect(rect); drawA.drawArrow(tempG,center1,center2); tempG = arrow2.getGraphics(); tempG.setBackColor(Color.WHITE); tempG.clearRect(rect); drawA.drawArrow(tempG,center2,center1); if ( conn.x < conn.y ) { data[k][0] = this.bitmapToTgaData2(arrow1); data[k][1] = this.bitmapToTgaData2(arrow2); } else { data[k][1] = this.bitmapToTgaData2(arrow1); data[k][0] = this.bitmapToTgaData2(arrow2); } String fileName = new String(Integer.toString(k)); while ( fileName.length() < 2 ) fileName = "0" + fileName; fileName = "STRAARROW" + fileName; File tgaFile = File.create(fileName + "_000.tga"); tgaFile.writeInt(0x00020000); tgaFile.writeInt(0); tgaFile.writeInt(0); tgaFile.writeShort((short)(arrow1.getSize().x)); tgaFile.writeShort((short)(arrow1.getSize().y)); tgaFile.writeShort((short)0x0110); tgaFile.writeShorts(data[k][0]); tgaFile.close(); tgaFile = File.create(fileName + "_001.tga"); tgaFile.writeInt(0x00020000); tgaFile.writeInt(0); tgaFile.writeInt(0); tgaFile.writeShort((short)(arrow1.getSize().x)); tgaFile.writeShort((short)(arrow1.getSize().y)); tgaFile.writeShort((short)0x0110); tgaFile.writeShorts(data[k][1]); tgaFile.close(); } return(data); } public boolean addConnection(int start,int end) { if ( connections.size() < maxArrows && areConnected(start,end)==false ) { connections.addElement(new Point(start,end)); return(true); } return(false); } public boolean removeConnection(int start,int end) { for ( int i = 0 ; i < connections.size() ; i++ ) { Point test = (Point)connections.elementAt(i); if ( ( test.x == start && test.y == end ) || (test.x == end && test.y == start) ) { connections.removeElementAt(i); return(true); } } return(false); } } public class DArea { Region area; Region edgeArea; Point centroid; public DArea(Region newArea) { this.area = newArea; this.edgeArea = getEdge(this.area); this.centroid = this.findCentroid(); } public boolean contains(int x,int y) { return(this.contains(new Point(x,y))); } public Point findCentroid() { int count = 0; int xs = 0; int ys = 0; for ( int i = this.area.getBounds().x ; i < this.area.getBounds().x + this.area.getBounds().width ; i++ ) { for ( int j = this.area.getBounds().y ; j < this.area.getBounds().y + this.area.getBounds().height ; j++ ) { if ( this.contains(i,j) ) { xs += i; ys += j; count++; } } } return(new Point(xs/count,ys/count)); } public boolean contains(Point p) { if ( this.area.contains(p) ) return(true); else return(false); } } public class StratArea { public Region area; Region edgeArea; Vector connectedTo; Point centroid; short[] areaTgaData; public Point editCentroid; public StratArea() { } public StratArea(Region newArea) { this.area = newArea; this.edgeArea = getEdge(this.area); this.connectedTo = new Vector(); centroid = this.findCentroid(); editCentroid = new Point(centroid.x/3+110,centroid.y/3+40); } public boolean contains(int x,int y) { return(this.contains(new Point(x,y))); } public void setEditCentroid(Point aPoint) { this.editCentroid = aPoint; } public int[] getConnectionArray() { int[] data = new int[maxMaps]; for ( int i = 0 ; i < maxMaps ; i++ ) { if ( this.isConnected(i) ) { data[i] = connData.findIndex(areas.indexOf(this),i); } else { data[i] = -1; } } return(data); } public boolean contains(Point aPoint) { if ( this.area.contains(aPoint) ) return(true); else return(false); } public void addConnection(int index) { if ( index != areas.indexOf(this) && connectedTo.size() < 8 && connectedTo.contains(new Integer(index)) == false && connData.connections.size() < maxArrows) { this.connectedTo.addElement(new Integer(index)); connData.addConnection(areas.indexOf(this),index); } } public void removeConnection(int index) { this.connectedTo.removeElement(new Integer(index)); connData.removeConnection(areas.indexOf(this),index); } public boolean isConnected(int index) { if ( this.connectedTo.contains(new Integer(index)) ) return(true); else return(false); } public int[] getConnections() { int[] conns = new int[this.connectedTo.size()]; for ( int i = 0 ; i < conns.length ; i++ ) { conns[i] = ((Integer)(connectedTo.elementAt(i))).intValue(); } return(conns); } public Point findCentroid() { int count = 0; int xs = 0; int ys = 0; for ( int i = this.area.getBounds().x ; i < this.area.getBounds().x + this.area.getBounds().width ; i++ ) { for ( int j = this.area.getBounds().y ; j < this.area.getBounds().y + this.area.getBounds().height ; j++ ) { if ( this.contains(i,j) ) { xs += i; ys += j; count++; } } } return(new Point(xs/count,ys/count)); } public Point getCenter() { return(this.centroid); //Rectangle bounds = this.area.getBounds(); //Point center = new Point(bounds.x+(bounds.width/2),bounds.y+(bounds.height/2)); //return(center); } } public StratArea getByCenter(Point center) { for (int i = 0 ; i < areas.size() ; i++ ) { StratArea currentArea = (StratArea)(areas.elementAt(i)); if ( currentArea.getCenter().equals(center) ) return(currentArea); } return(null); } public class DebriefArea extends Control { public Bitmap graphBitmap; public Bitmap maskBitmap; Graphics offG; Bitmap offBitmap; public Graphics thisG; public DebriefArea() { thisG = this.createGraphics(); } public void repaint() { this.redraw(this,null); } protected void onPaint(PaintEvent e) { thisG.drawImage(offBitmap,0,0); } public void redraw(Object sender,PaintEvent e) { if ( offG == null ) { offBitmap = ((Bitmap)graphBitmap.clone()); offG = offBitmap.getGraphics(); } this.setSize(graphBitmap.getSize()); if ( maskOn ) offG.drawImage(this.maskBitmap,0,0); else offG.drawImage(this.graphBitmap,0,0); thisG.drawImage(offBitmap,0,0); for ( int i = 0 ; i < dareas.size() ; i++ ) { DArea currentArea = ((DArea)(dareas.elementAt(i))); offG.setBackColor(Color.BLACK); offG.drawString(" " + Integer.toString(i)+ " ",currentArea.centroid.x-9,currentArea.centroid.y-9); offG.setBackColor(Color.LIGHTGRAY); offG.drawString(" " + Integer.toString(i)+ " ",currentArea.centroid.x-10,currentArea.centroid.y-10); offG.setBackColor(Color.WHITE); offG.fill(currentArea.edgeArea,new Brush(Color.YELLOW)); } thisG.drawImage(offBitmap,0,0); } } public class EditorArea extends Control { public Bitmap graphBitmap; Graphics offG; Bitmap offBitmap; public Graphics thisG; public EditorArea() { thisG = this.createGraphics(); } public void repaint() { this.redraw(this,null); } protected void onPaint(PaintEvent e) { thisG.drawImage(offBitmap,0,0); } public void redraw(Object sender,PaintEvent e) { if ( offG == null ) { offBitmap = ((Bitmap)graphBitmap.clone()); offG = offBitmap.getGraphics(); } double divX = 1708/564; double divY = 1129/411; Point start = new Point(144,55); this.setSize(graphBitmap.getSize()); offG.drawImage(this.graphBitmap,0,0); offG.setPen(new Pen(Color.RED,PenStyle.SOLID,1)); for ( int i = 0 ; i < areas.size() ; i++ ) { System.out.println(i); StratArea currentArea = ((StratArea)(areas.elementAt(i))); Point center = currentArea.editCentroid; Point[] vertices = new Point[5]; vertices[0] = new Point(center.x,center.y-8); vertices[1] = new Point(center.x-15,center.y); vertices[2] = new Point(center.x,center.y+8); vertices[3] = new Point(center.x+15,center.y); vertices[4] = new Point(center.x,center.y-8); offG.drawPolygon(vertices); String index = new String(Integer.toString(i)); while ( index.length() < 2 ) index = "0" + index; offG.setFont(new Font(Font.DEFAULT_GUI,(float)4,FontSize.MILLIMETERS)); offG.drawString(index,new Point(center.x-15,center.y-16)); } for ( int i = 0 ; i < navals.size() ; i++ ) { Point aNaval = (Point)navals.elementAt(i); offG.drawRect(aNaval.x,aNaval.y,32,15); if ( navalConns[i] != -1 ) { Point areaNaval = ((StratArea)(areas.elementAt(navalConns[i]))).editCentroid; offG.drawLine(areaNaval,new Point(aNaval.x+32,aNaval.y+15)); } } if ( navalDragPoint.x != -1 && navalDragPoint.y != -1 ) { System.out.println(navalDragPoint); offG.drawLine(navalDragPoint,navalStartPoint); } thisG.drawImage(offBitmap,0,0); } } public class DrawArea extends Control { public Bitmap graphBitmap; public Bitmap maskBitmap; Graphics offG; Bitmap offBitmap; public Graphics thisG; public DrawArea() { thisG = this.createGraphics(); } public void repaint() { this.redraw(this,null); } protected void onPaint(PaintEvent e) { //redraw(this,e); thisG.drawImage(offBitmap,0,0); } public double getAngle(Point start,Point end) { double diffX = end.x - start.x; double diffY = end.y - start.y; double k; double angle; try { k = diffY / diffX; angle = Math.atan(diffY/(diffX)); } catch ( ArithmeticException aE ) { angle = Math.PI;} if ( end.x == start.x ) diffX = 0.0000000001; //double angle = Math.abs(180*Math.atan(diffX/diffY)/Math.PI); /*double boost = 0; if ( diffX > 0 && diffY > 0 ) boost = -180; if ( diffX < 0 && diffY > 0 ) boost = 180; if ( diffX > 0 && diffY < 0 ) boost = 0; // OK if ( diffX < 0 && diffY < 0 ) boost = -360; if ( diffX < 0 && diffY == 0 ) boost = 0; if ( diffX > 0 && diffY == 0 ) boost = -360; angle = Math.abs(angle + boost); */ return(angle); } public void drawArrow(Graphics g,Point starts,Point ends) { if ( starts.x == ends.x && starts.y == ends.y ) return; double dist = distance(starts,ends); Point startsP = new Point((int)((0.6*dist*starts.x+0.4*dist*ends.x)/(dist)),(int)((0.6*dist*starts.y+0.4*dist*ends.y)/(dist))); Point endsP = new Point((int)(((0.2)*dist*starts.x+(0.8)*dist*ends.x)/(dist)),(int)(((0.2)*dist*starts.y+(0.8)*dist*ends.y)/(dist))); Point endsP2 = new Point((int)((0.15*dist*starts.x+0.85*dist*ends.x)/(dist)),(int)((0.15*dist*starts.y+0.85*dist*ends.y)/(dist))); Pen tempP = g.getPen(); double a = 30; double b= distance(starts,ends)-a; Point linePoint = new Point(((int)( (a*startsP.x+b*endsP.x)/(a+b) )),((int)((a*startsP.y+b*endsP.y)/(a+b)))); double angle; Point transLinePoint = new Point(linePoint.x-endsP.x,linePoint.y-endsP.y); angle = Math.PI/4; Point arrowP1 = new Point(((int)(transLinePoint.x*Math.cos(angle)-transLinePoint.y*Math.sin(angle))) ,((int)(transLinePoint.x*Math.sin(angle)+transLinePoint.y*Math.cos(angle)))); Point arrowH1 = new Point(arrowP1.x+endsP.x,arrowP1.y+endsP.y); angle = -1*angle; Point arrowP2 = new Point(((int)(transLinePoint.x*Math.cos(angle)-transLinePoint.y*Math.sin(angle))) ,((int)(transLinePoint.x*Math.sin(angle)+transLinePoint.y*Math.cos(angle)))); Point arrowH2 = new Point(arrowP2.x+endsP.x,arrowP2.y+endsP.y); Color arrowC = new Color(223,223,223); Point[] points = new Point[3]; Color shadow1 = new Color(96,96,96); Color light1 = new Color(244,244,244); g.setPen(new Pen(shadow1,PenStyle.SOLID,4)); g.drawLine(new Point(startsP.x+2,startsP.y+2),new Point(endsP.x+2,endsP.y+2)); points[0] = new Point(arrowH1.x+2,arrowH1.y+2); points[1] = new Point(endsP2.x+2,endsP2.y+2); points[2] = new Point(arrowH2.x+2,arrowH2.y+2); g.fill(Region.createPolygonal(points), new Brush(shadow1,BrushStyle.SOLID)); Color shadow2 = new Color(144,144,144); g.setPen(new Pen(shadow2,PenStyle.SOLID,4)); g.drawLine(new Point(startsP.x+1,startsP.y+1),new Point(endsP.x+1,endsP.y+1)); points[0] = new Point(arrowH1.x+1,arrowH1.y+1); points[1] = new Point(endsP2.x+1,endsP2.y+1); points[2] = new Point(arrowH2.x+1,arrowH2.y+1); g.fill(Region.createPolygonal(points), new Brush(shadow2,BrushStyle.SOLID)); g.setPen(new Pen(light1,PenStyle.SOLID,4)); g.drawLine(new Point(startsP.x-1,startsP.y-1),new Point(endsP.x-1,endsP.y-1)); points[0] = new Point(arrowH1.x-1,arrowH1.y-1); points[1] = new Point(endsP2.x-1,endsP2.y-1); points[2] = new Point(arrowH2.x-1,arrowH2.y-1); g.fill(Region.createPolygonal(points), new Brush(light1,BrushStyle.SOLID)); g.setPen(new Pen(arrowC,PenStyle.SOLID,4)); g.drawLine(startsP,endsP); points[0] = arrowH1;points[1] = endsP2;points[2] = arrowH2; g.fill(Region.createPolygonal(points), new Brush(arrowC,BrushStyle.SOLID)); g.setPen(tempP); } public void redraw(Object sender,PaintEvent e) { if ( offG == null ) { offBitmap = ((Bitmap)graphBitmap.clone()); offG = offBitmap.getGraphics(); } this.setSize(graphBitmap.getSize()); if ( maskOn ) offG.drawImage(maskBitmap,0,0); else offG.drawImage(graphBitmap,0,0); offG.setBrush(new Brush(Color.LIGHTGRAY,BrushStyle.SOLID)); for ( int i = 0; i < areas.size() ; i++ ) { StratArea currentArea = (StratArea)(areas.elementAt(i)); offG.fill(currentArea.edgeArea); offG.setFont(new Font(Font.DEFAULT_GUI,(float)14,FontSize.POINTS)); offG.setBackColor(Color.BLACK); offG.drawString(" " + Integer.toString(i)+ " ",currentArea.getCenter().x+1,currentArea.getCenter().y+1); offG.setBackColor(Color.LIGHTGRAY); offG.drawString(" " + Integer.toString(i)+ " ",currentArea.getCenter()); offG.setBackColor(Color.WHITE); if ( onRegion != -1 ) { currentArea = (StratArea)(areas.elementAt(onRegion)); offG.fill(currentArea.edgeArea,new Brush(yellColor) ); int[] conns = currentArea.getConnections(); for ( int h = 0 ; h < conns.length ; h++ ) { drawArrow(offG,currentArea.getCenter(),((StratArea)(areas.elementAt((int)conns[h]))).getCenter()); } } if ( start.equals(new Point(-1,-1)) == false && end.equals(new Point(-1,-1)) == false ) { drawArrow(offG,((StratArea)(areas.elementAt((isRegion(start))))).getCenter(),((StratArea)(areas.elementAt((isRegion(end))))).getCenter()); if ( delete || connData.connections.size() >= maxArrows) { Pen tempB = offG.getPen(); Point p2 = ((StratArea)(areas.elementAt((isRegion(end))))).getCenter(); offG.setPen(new Pen(Color.BLACK,PenStyle.SOLID,4)); offG.drawLine(p2.x-9,p2.y-9,p2.x+11,p2.y+11); offG.drawLine(p2.x+11,p2.y-9,p2.x-9,p2.y+11); offG.setPen(new Pen(Color.ORANGE,PenStyle.SOLID,4)); if ( delete ) offG.setPen(new Pen(Color.RED,PenStyle.SOLID,4)); offG.drawLine(p2.x-10,p2.y-10,p2.x+10,p2.y+10); offG.drawLine(p2.x+10,p2.y-10,p2.x-10,p2.y+10); offG.setPen(tempB); } } } thisG.drawImage(offBitmap,0,0); } } }