private void initImages(Grid grid) { // Initialize all images fullFieldImg = new BufferedImage(Settings.FIELD_WIDTH, Settings.FIELD_HEIGHT, BufferedImage.TYPE_INT_RGB); fieldBackgroundImg = new BufferedImage(Settings.FIELD_WIDTH, Settings.FIELD_HEIGHT, BufferedImage.TYPE_INT_RGB); // Generate image graphics Graphics2D g2dBackground = fieldBackgroundImg.createGraphics(); generateFieldBackground(g2dBackground, grid); Larva.initImages(); Hive.initImages(); }
@Override public void draw(Graphics g) { GamePanel myPanel = GamePanel.getInstance(); // Get the 2D graphics Graphics2D g2dPanel = (Graphics2D) g; Graphics2D g2dField = (Graphics2D) fullFieldImg.getGraphics(); if (Settings.ANTI_ALIASING) { Util.optimizeGraphics(g2dField); } // Draw the background of the panel g2dPanel.setColor(Color.GRAY); g2dPanel.fillRect(0, 0, myPanel.getWidth(), myPanel.getHeight()); // Draw the background of the field g2dField.drawImage(fieldBackgroundImg, 0, 0, null); /* * Draw all game objects/entities */ // Based on player vs. AI // Draw all hive mucus for (Player p : activePlayers) { p.drawAllHiveMucus(g2dField); } // Draw all hive main bases for (Player p : activePlayers) { p.drawAllHiveMains(g2dField); } // Draw the spawn destinations for my hives if (activePlayers.contains(myPlayer)) { myPlayer.drawAllHiveSpawnDests(g2dField); } // Draw all larvae for (Player p : activePlayers) { p.drawAllLarvae(g2dField); } // If my player isn't yet defeated: if (activePlayers.contains(myPlayer)) { // Draw all active pods myPlayer.drawAllPods(g2dField); // Draw a mock hive, if in hive placement mode if (placingHive) { Hive.drawMock(g2dField, mouseGlobal); } } /* * Draw the selection box and hive placement, if applicable */ // Draw the rectangular selection box drawSelectionBox(g2dField); /* * Draw the visible portion of the full field image to the JPanel's graphics */ globalOffset.constrain( -Settings.FIELD_WIDTH + myPanel.getWidth(), -Settings.FIELD_HEIGHT - Settings.MINI_MAP_HEIGHT + myPanel.getHeight(), 0, 0); BufferedImage visibleField = fullFieldImg.getSubimage( -globalOffset.intX(), -globalOffset.intY(), myPanel.getWidth(), myPanel.getHeight() - Settings.MINI_MAP_HEIGHT); g2dPanel.drawImage(visibleField, 0, 0, myPanel); /* * Draw the HUD */ // Draw the mini-map if (showMiniMap) { // Draw an border around the current view area Stroke oldStroke = g2dField.getStroke(); g2dField.setStroke(new BasicStroke(10)); g2dField.setColor(Color.RED); g2dField.drawRect( -globalOffset.intX(), -globalOffset.intY(), myPanel.getWidth(), myPanel.getHeight() - Settings.MINI_MAP_HEIGHT); g2dField.setStroke(oldStroke); // Draw the mini-map itself int topLeftX = miniMapRect.x; int topLeftY = miniMapRect.y; int botRightX = topLeftX + miniMapRect.width; int botRightY = topLeftY + miniMapRect.height; g2dPanel.drawImage( fullFieldImg, topLeftX, topLeftY, botRightX, botRightY, 0, 0, fullFieldImg.getWidth(), fullFieldImg.getHeight(), myPanel); // Draw a border around the minimap Stroke original = g2dPanel.getStroke(); g2dPanel.setStroke(new BasicStroke(3)); g2dPanel.setColor(Color.GREEN); g2dPanel.drawRect(topLeftX, topLeftY, Settings.MINI_MAP_WIDTH, Settings.MINI_MAP_HEIGHT); g2dPanel.setStroke(original); } // Draw the control group dock if (showControlGroupDock) { // Draw the background g2dPanel.setColor(Color.LIGHT_GRAY); int width = myPanel.getWidth() - Settings.MINI_MAP_WIDTH; Rectangle dockRect = new Rectangle( 0, myPanel.getHeight() - Settings.MINI_MAP_HEIGHT, width, Settings.MINI_MAP_HEIGHT); g2dPanel.fill(dockRect); // Draw a border around the dock Stroke oldStroke = g2dPanel.getStroke(); g2dPanel.setStroke(new BasicStroke(3)); g2dPanel.setColor(Color.GREEN); g2dPanel.draw(dockRect); g2dPanel.setStroke(oldStroke); // Draw an indicator for each control group g2dPanel.setColor(Color.BLACK); int widthOffset = 200; int numGroups = ControlGroup.numGroups(); int interval = (width - widthOffset) / numGroups * 2; for (int i = 0; i < numGroups; i++) { int y = myPanel.getHeight() - 2 * Settings.MINI_MAP_HEIGHT / 3; if (i > 4) { y += Settings.MINI_MAP_HEIGHT / 3; } int x = (i % 5) * interval + interval / 3 + widthOffset; g2dPanel.drawString("Group: " + i, x, y); g2dPanel.drawString("# Units: " + ControlGroup.sizeOfGroup(i), x, y + 20); } // Print various utility messages into the dock // Print the current elapsed game time (in mins) g2dPanel.setColor(Color.BLACK); float xAlign = widthOffset / 2 - 60; drawElapsedTime( g2dPanel, xAlign, myPanel.getHeight() - Settings.MINI_MAP_HEIGHT + Settings.MINI_MAP_HEIGHT / 2); // Tell the player how to exit the game AttributedString abortMsg = new AttributedString("Press ESC to abort"); abortMsg.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); g2dPanel.drawString(abortMsg.getIterator(), xAlign, myPanel.getHeight() - 160); // Print whether or not the human player is placing a hive if (Debugging.printHivePlacement) { g2dPanel.drawString("Placing Hive: " + placingHive, xAlign, myPanel.getHeight() - 20); } // Print the number of larvae the human player has if (Debugging.printLarvaeCount) { g2dPanel.drawString( "Larvae Count: " + myPlayer.getNumLarvae() + " / " + Player.MAX_NUM_LARVAE, xAlign, myPanel.getHeight() - 50); } } }