📄 mapcanvas.java
字号:
import java.awt.*;import java.awt.image.*;import java.awt.event.*;public class MapCanvas extends Canvas { private Dimension preferredSize = new Dimension(50, 50); private Dimension minimumSize = new Dimension(50,50); private Image offScreenImage; private Graphics s; Image image; boolean imageloaded = false; Timer polyTimer; Tool tool; JavaMapperFrame frame; String mode = "rect"; int imageW,imageH; public MapCanvas(JavaMapperFrame frame) { super(); this.frame = frame; tool = new Tool(this); addMouseListener(tool); addMouseMotionListener(tool); polyTimer = new Timer(this); polyTimer.start(); } public void paint(Graphics g) { update(g); } public void update(Graphics g) { Dimension d = getSize(); offScreenImage = createImage(d.width,d.height); s = offScreenImage.getGraphics(); s.setColor(Color.gray); s.fillRect(0,0,d.width,d.height); if (imageloaded) { s.drawImage(image,0,0,this); tool.draw(s); } g.drawImage(offScreenImage,0,0,this); } public Dimension getMinimumSize() { return minimumSize; } public Dimension getPreferredSize() { return preferredSize; } public void loadImage (Image image, String filename) { this.image = image; imageW = image.getWidth(this); imageH = image.getHeight(this); System.out.println("Recieved "+filename+" ("+String.valueOf(imageW)+"x"+String.valueOf(imageH)+")"); preferredSize = new Dimension(imageW,imageH); getPreferredSize(); setSize(imageW,imageH); frame.pack(); imageloaded = true; repaint(); } public void closeImage() { imageloaded = false; image = null; int w = imageW; int h = imageH; imageW = 0; imageH = 0; preferredSize = new Dimension(imageW,imageH); getPreferredSize(); tool.deleteAllShapes(); frame.refreshDialogs(); } public void setTool (String mode) { this.mode = mode; if (this.mode=="select") setCursor(Frame.DEFAULT_CURSOR); else setCursor(Frame.CROSSHAIR_CURSOR); tool.endEverything(); } public void setCursor (int cursor) { if (cursor != frame.getCursorType()) { frame.setCursor(cursor); } } // -- Tool Class -------------------------------------- class Tool extends MouseAdapter implements MouseMotionListener { int offsetX,offsetY,mouseX,mouseY; boolean drawActive,highlightActive,selectActive,dragActive; Rect rect[] = new Rect[200]; int nRect; Circle circle[] = new Circle[200]; int nCircle; Poly p,poly[] = new Poly[200]; int nPoly; ShapeInterface currentShape,testShape,shapes[] = new ShapeInterface[600]; int nShapes; MapCanvas canvas; Color color = Color.blue; public Tool(MapCanvas canvas) { this.canvas = canvas; } public void startDraw(int x, int y) { if (!drawActive) { if (mode=="rect" && nRect<200) { rect[nRect] = new Rect(x,y); rect[nRect].setIndex(nRect); currentShape = rect[nRect++]; } else if (mode=="circle" && nCircle<200) { circle[nCircle] = new Circle(x,y); circle[nCircle].setIndex(nCircle); currentShape = circle[nCircle++]; } else if (mode=="poly" && nPoly<200) { poly[nPoly] = new Poly(x,y); poly[nPoly].setIndex(nPoly); currentShape = poly[nPoly++]; poly[currentShape.getIndex()].nextShown = false; } if (mode!="select") { drawActive = true; frame.mDeleteAllShapes.setEnabled(true); } } else if (currentShape.getType()=="poly") { if (!poly[currentShape.getIndex()].checkNewPoint(x,y)) { continueDraw(x,y); poly[currentShape.getIndex()].nextShown = false; } else { poly[currentShape.getIndex()].setDrawing(false); if (poly[currentShape.getIndex()].getPoints()<=2) { deleteShape(); } else { print("Created "+currentShape.getType()+" "+String.valueOf(currentShape.getIndex())); } currentShape = null; drawActive = false; } } } public void continueDraw(int x, int y) { currentShape.newPoint(x,y); repaint(); } public void finishDraw() { if (currentShape.getW()==0 && currentShape.getH()==0) { deleteShape(); } else { print("Created "+currentShape.getType()+" "+String.valueOf(currentShape.getIndex())); } currentShape = null; drawActive = false; } public boolean highlightShape(int x, int y) { testShape = null; boolean shapefound; for (int i=nRect-1;i>=0;i--) { if (rect[i].contains(x,y)) { testShape = rect[i]; break; } } for (int i=nCircle-1;i>=0;i--) { if (circle[i].contains(x,y)) { testShape = circle[i]; break; } } for (int i=nPoly-1;i>=0;i--) { if (poly[i].contains(x,y)) { testShape = poly[i]; break; } } if (testShape!=null) { if (currentShape!=testShape && currentShape!=null) currentShape.setHighlighted(false); testShape.setHighlighted(true); if (currentShape!=testShape) repaint(); currentShape = testShape; highlightActive = true; } else { if (currentShape!=null) { currentShape.setHighlighted(false); currentShape = null; repaint(); highlightActive = false; } } return highlightActive; } public void deleteShape() { print("deleted "+currentShape.getType()+" "+String.valueOf(currentShape.getIndex())); currentShape.setSelected(false); frame.mDeleteShape.setEnabled(false); selectActive = false; if (currentShape.getType()=="rect") { for (int i=currentShape.getIndex();i<nRect-1;i++) { rect[i] = rect[i+1]; rect[i].setIndex(i); } nRect -= 1; } if (currentShape.getType()=="circle") { for (int i=currentShape.getIndex();i<nCircle-1;i++) { circle[i] = circle[i+1]; circle[i].setIndex(i); } nCircle -= 1; } if (currentShape.getType()=="poly") { for (int i=currentShape.getIndex();i<nPoly-1;i++) { poly[i] = poly[i+1]; poly[i].setIndex(i); } nPoly -= 1; } if (nPoly==0 && nCircle==0 && nRect==0) frame.mDeleteAllShapes.setEnabled(false); currentShape = null; canvas.frame.refreshDialogs(); repaint(); } public void deleteAllShapes() { endEverything(); nRect = 0; nCircle = 0; nPoly = 0; frame.mDeleteAllShapes.setEnabled(false); canvas.frame.refreshDialogs(); repaint(); } public void selectShape() { currentShape.setSelected(true); frame.mDeleteShape.setEnabled(true); highlightActive = false; selectActive = true; } public void deselectShape() { if (currentShape==null) return; currentShape.setSelected(false); frame.mDeleteShape.setEnabled(false); selectActive = false; } public int getSelectedIndex() { if (currentShape==null) return -1; remakeShapes(); for (int i=0;i<nShapes;i++) { if (shapes[i]==currentShape) return i; } return -1; } public boolean startDragShape(int x, int y) { if (currentShape.contains(x,y)) { offsetX = x-currentShape.getX(); offsetY = y-currentShape.getY(); dragActive = true; setCursor(Frame.MOVE_CURSOR); return true; } else return false; } public void dragShape(int x, int y) { currentShape.moveTo(x-offsetX,y-offsetY); repaint(); } public void endEverything() { if (drawActive && currentShape!=null && currentShape.getType()=="poly") { poly[currentShape.getIndex()].setDrawing(false); if (poly[currentShape.getIndex()].getPoints()<=2) { deleteShape(); } } if (currentShape!=null) { currentShape.setSelected(false); currentShape.setHighlighted(false); currentShape = null; } drawActive = false; highlightActive = false; selectActive = false; repaint(); } public void draw(Graphics s) { s.setColor(color); for (int i=0;i<nRect;i++){ rect[i].draw(s); } for (int i=0;i<nCircle;i++){ circle[i].draw(s); } for (int i=0;i<nPoly;i++){ poly[i].draw(s); } } public void mousePressed(MouseEvent e) { if (!imageloaded) return; int x = e.getX(); int y = e.getY(); if (highlightActive) { // if highligted and clicked, select it selectShape(); startDragShape(x,y); } else if (selectActive) { if ((currentShape.getType()=="circle" || currentShape.getType()=="rect") && currentShape.getResize(x,y)!=Frame.DEFAULT_CURSOR) { } else if (currentShape.getType()=="poly" && currentShape.getResize(x,y)!=-1) { polyTimer.setInfo("poly",e,x,y,currentShape.getResize(x,y)); polyTimer.resume(); } else if (!startDragShape(x,y)) { // start drag or deselect deselectShape(); if (highlightShape(x,y)) { // check if clicked another and select/drag it selectShape(); startDragShape(x,y); } } } else { startDraw(x,y); } repaint(); } public void mouseDragged(MouseEvent e) { if (!imageloaded) return; int x = e.getX(); int y = e.getY(); mouseX = x; mouseY = y; frame.mouseXY.setText("("+String.valueOf(x)+","+String.valueOf(y)+")"); if (drawActive) { // place another point if (currentShape.getType()=="poly") { poly[currentShape.getIndex()].changePoint(x,y); repaint(); } else continueDraw(x,y); } else if (selectActive && currentShape.resize(x,y)) { // resize repaint(); } else if (dragActive) { // drag it dragShape(x,y); } } public void mouseMoved(MouseEvent e) { if (!imageloaded) return; int x = e.getX(); int y = e.getY(); mouseX = x; mouseY = y; frame.mouseXY.setText("("+String.valueOf(x)+","+String.valueOf(y)+")"); if (mode=="select") { if (!selectActive) highlightShape(x,y); else if (!dragActive) { if (currentShape.getType()=="poly") { if (currentShape.getResize(x,y)!=-1) setCursor(Frame.MOVE_CURSOR); else setCursor(Frame.DEFAULT_CURSOR); } else setCursor(currentShape.getResize(x,y)); } } else if (drawActive && currentShape.getType()=="poly") { poly[currentShape.getIndex()].showNext(x,y); repaint(); } } public void mouseReleased(MouseEvent e) { if (!imageloaded) return; int x = e.getX(); int y = e.getY(); if (drawActive) { if (currentShape.getType()=="poly") { poly[currentShape.getIndex()].showNext(x,y); poly[currentShape.getIndex()].nextShown = true; } else finishDraw(); } else if (dragActive) { setCursor(Frame.DEFAULT_CURSOR); dragActive = false; } canvas.frame.refreshDialogs(); repaint(); } public void print(String s) { System.out.println(s); } public void showPolyPopup() { if (mouseX>=polyTimer.x-1 && mouseX<=polyTimer.x+1 && mouseY>=polyTimer.y-1 && mouseY<=polyTimer.y+1) { frame.polyPopup.show(polyTimer.e.getComponent(),mouseX,mouseY); } } public void deletePolyPoint() { poly[currentShape.getIndex()].deletePoint(polyTimer.node); repaint(); } public void insertPolyPoint() { poly[currentShape.getIndex()].insertPoint(polyTimer.node); repaint(); } public void remakeShapes() { nShapes = 0; for (int i=0;i<nRect;i++) { shapes[nShapes++] = rect[i]; } for (int i=0;i<nCircle;i++) { shapes[nShapes++] = circle[i]; } for (int i=0;i<nPoly;i++) { shapes[nShapes++] = poly[i]; } } public String getCode(String type) { remakeShapes(); String code = ""; if (type=="client") { code = "<map name=\""+frame.mapName+"\">\n"; for (int i=0;i<nShapes;i++) { code += "<area "; if (frame.saveNames) code += "name=\""+shapes[i].getName()+"\" "; code += "shape=\""+shapes[i].getType()+"\" coords=\""+shapes[i].getCoords(type)+"\" href=\""+shapes[i].getURL()+"\">\n"; } code += "</map>\n"; } else if (type=="server") { for (int i=0;i<nShapes;i++) { code += shapes[i].getType()+" "+shapes[i].getURL()+" "+shapes[i].getCoords(type)+"\n";; } } return code; } } // -- Timer Class -------------------------------------- class Timer extends Thread { MapCanvas canvas; boolean active = false; String command; int x,y,node; MouseEvent e; public Timer(MapCanvas canvas) { this.canvas = canvas; } public void setInfo(String command, MouseEvent e, int x, int y, int node) { this.command = command; this.e = e; this.x = x; this.y = y; this.node = node; } public void run() { while (true) { try { sleep(900); if (this.command=="poly") { canvas.tool.showPolyPopup(); } } catch (InterruptedException e) { } suspend(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -