📄 boardview1.java
字号:
public void addBoardViewListener(BoardViewListener listener) { if (!boardListeners.contains(listener)) { boardListeners.addElement(listener); } } /** * Removes the specified board listener. * * @param listener the board listener. */ public void removeBoardViewListener(BoardViewListener listener) { boardListeners.removeElement(listener); } /** * Notifies attached board listeners of the event. * * @param event the board event. */ public void processBoardViewEvent(BoardViewEvent event) { if (boardListeners == null) { return; } for (Enumeration e = boardListeners.elements(); e.hasMoreElements();) { BoardViewListener l = (BoardViewListener) e.nextElement(); switch (event.getType()) { case BoardViewEvent.BOARD_HEX_CLICKED: case BoardViewEvent.BOARD_HEX_DOUBLECLICKED: case BoardViewEvent.BOARD_HEX_DRAGGED: l.hexMoused(event); break; case BoardViewEvent.BOARD_HEX_CURSOR: l.hexCursor(event); break; case BoardViewEvent.BOARD_HEX_HIGHLIGHTED: l.boardHexHighlighted(event); break; case BoardViewEvent.BOARD_HEX_SELECTED: l.hexSelected(event); break; case BoardViewEvent.BOARD_FIRST_LOS_HEX: l.firstLOSHex(event); break; case BoardViewEvent.BOARD_SECOND_LOS_HEX: l.secondLOSHex(event, getFirstLOS()); break; case BoardViewEvent.FINISHED_MOVING_UNITS: l.finishedMovingUnits(event); break; case BoardViewEvent.SELECT_UNIT: l.unitSelected(event); break; } } } private void addMovingUnit(Entity entity, Vector movePath) { if (!movePath.isEmpty()) { Object[] o = new Object[2]; o[0] = entity; o[1] = movePath; movingUnits.addElement(o); GhostEntitySprite ghostSprite = new GhostEntitySprite(entity); ghostEntitySprites.add(ghostSprite); // Center on the starting hex of the moving unit. UnitLocation loc = ((UnitLocation) movePath.elementAt(0)); centerOnHex(loc.getCoords()); } } public void addDisplayable(Displayable disp) { displayables.addElement(disp); } /** * Specify the scrollbars that control this view's positioning. * * @param vertical - the vertical <code>Scrollbar</code> * @param horizontal - the horizontal <code>Scrollbar</code> */ public void setScrollbars(JScrollBar vertical, JScrollBar horizontal) { vScrollbar = vertical; hScrollbar = horizontal; // When the scroll bars are adjusted, update our offset. vScrollbar.addAdjustmentListener(this); hScrollbar.addAdjustmentListener(this); } /** * Update ourself when a scroll bar is adjusted. * * @param event - the <code>AdjustmentEvent</code> that caused this call. */ public void adjustmentValueChanged(AdjustmentEvent event) { Point oldPt = scroll; Point newPt = new Point(oldPt.x, oldPt.y); if (event.getAdjustable().getOrientation() == Adjustable.VERTICAL) { newPt.y = event.getValue(); } else { newPt.x = event.getValue(); } scroll.setLocation(newPt); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); // Limit our size to the viewport of the scroll pane. final Dimension size = getSize(); // final long startTime = System.currentTimeMillis(); // commentme // Make sure our scrollbars have the right sizes. // N.B. A buggy Sun implementation makes me to do this here instead // of updateBoardSize() (which is where *I* think it belongs). if (vScrollbar != null) { vScrollbar.setVisibleAmount(size.height); vScrollbar.setBlockIncrement(size.height); vScrollbar.setUnitIncrement((int) (scale * HEX_H / 2.0)); vScrollbar.setMaximum(boardSize.height); } if (hScrollbar != null) { hScrollbar.setVisibleAmount(size.width); hScrollbar.setBlockIncrement(size.width); hScrollbar.setUnitIncrement((int) (scale * HEX_W / 2.0)); hScrollbar.setMaximum(boardSize.width); } // update view, offset view.setLocation(scroll); view.setSize(getOptimalView(size)); offset.setLocation(getOptimalOffset(size)); if (!isTileImagesLoaded()) { g.drawString(Messages.getString("BoardView1.loadingImages"), 20, 50); //$NON-NLS-1$ if (!tileManager.isStarted()) { System.out.println("boardview1: loading images for board"); //$NON-NLS-1$ tileManager.loadNeededImages(game); } return; } // make sure back buffer is valid if (backGraph == null || !view.getSize().equals(backSize)) { // make new back buffer backSize = view.getSize(); backImage = createImage(backSize.width, backSize.height); backGraph = backImage.getGraphics(); } // make sure board rectangle contains our current view rectangle if (boardImage == null || !boardRect.union(view).equals(boardRect)) { updateBoardImage(); } // draw onto the back buffer: // draw the board backGraph.drawImage(boardImage, 0, 0, this); // draw wrecks if (GUIPreferences.getInstance().getShowWrecks()) { drawSprites(wreckSprites); } // Minefield signs all over the place! drawMinefields(); // Artillery targets drawArtilleryHexes(); // draw highlight border drawSprite(highlightSprite); // draw cursors drawSprite(cursorSprite); drawSprite(selectedSprite); drawSprite(firstLOSSprite); drawSprite(secondLOSSprite); // draw deployment indicators if (m_plDeployer != null) { drawDeployment(); } // draw C3 links drawSprites(C3Sprites); // draw onscreen entities drawSprites(entitySprites); // draw moving onscreen entities drawSprites(movingEntitySprites); // draw ghost onscreen entities drawSprites(ghostEntitySprites); // draw onscreen attacks drawSprites(attackSprites); // draw movement, if valid drawSprites(pathSprites); // added by kenn // draw the ruler line if (rulerStart != null) { Point start = getCentreHexLocation(rulerStart); if (rulerEnd != null) { Point end = getCentreHexLocation(rulerEnd); backGraph.setColor(Color.yellow); backGraph.drawLine(start.x - boardRect.x, start.y - boardRect.y, end.x - boardRect.x, end.y - boardRect.y); backGraph.setColor(rulerEndColor); backGraph.fillRect(end.x - boardRect.x - 1, end.y - boardRect.y - 1, 2, 2); } backGraph.setColor(rulerStartColor); backGraph.fillRect(start.x - boardRect.x - 1, start.y - boardRect.y - 1, 2, 2); } // end kenn // draw all the "displayables" for (int i = 0; i < displayables.size(); i++) { Displayable disp = (Displayable) displayables.elementAt(i); disp.draw(backGraph, backSize); } // draw the back buffer onto the screen // first clear the entire view if the map has been zoomed if (scale < 1.00f) { Image tmpImage = createImage(size.width, size.height); Graphics tmpGraphics = tmpImage.getGraphics(); tmpGraphics.drawImage(backImage, offset.x, offset.y, this); g.drawImage(tmpImage, 0, 0, this); tmpGraphics.dispose(); } else { g.drawImage(backImage, offset.x, offset.y, this); } //g.drawString(""+scale, 10, 10); // final long finishTime = System.currentTimeMillis();//commentme // System.out.println("BoardView1: updated screen in " + (finishTime - startTime) + " ms."); //commentme } /** * Updates the boardSize variable with the proper values for this board. */ private void updateBoardSize() { int width = game.getBoard().getWidth() * (int) (HEX_WC * scale) + (int) (HEX_W / 4 * scale); int height = game.getBoard().getHeight() * (int) (HEX_H * scale) + (int) (HEX_H / 2 * scale); boardSize = new Dimension(width, height); } /** * Think up the size of the view rectangle based on the size of the component * and the size of board */ private Dimension getOptimalView(Dimension size) { return new Dimension(Math.min(size.width, boardSize.width), Math.min(size.height, boardSize.height)); } /** * Where should the offset be for this screen size? */ private Point getOptimalOffset(Dimension size) { int ox = 0; int oy = 0; if (size.width > boardSize.width) { ox = (size.width - boardSize.width) / 2; } if (size.height > boardSize.height) { oy = (size.height - boardSize.height) / 2; } return new Point(ox, oy); } /** * Repaint the bounds of a sprite, offset by view */ private void repaintBounds(Rectangle bounds) { if (view != null) { repaint(bounds.x - view.x + offset.x, bounds.y - view.y + offset.y, bounds.width, bounds.height); } } /** * Looks through a vector of buffered images and draws them if they're * onscreen. */ private synchronized void drawSprites(ArrayList<Sprite> sprites) { for (Sprite sprite : sprites) { drawSprite(sprite); } } /** * Draws a sprite, if it is in the current view */ private void drawSprite(Sprite sprite) { if (view.intersects(sprite.getBounds()) && !sprite.hidden) { final int drawX = sprite.getBounds().x - view.x; final int drawY = sprite.getBounds().y - view.y; if (!sprite.isReady()) { sprite.prepare(); } sprite.drawOnto(backGraph, drawX, drawY, this); } } /** * Manages a cache of scaled images. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -