public void mouseClicked(MouseEvent e) { Tile t = (Tile) e.getSource(); ImageIcon temp = (ImageIcon) t.getIcon(); currTileImg = new ImageIcon(scaleImage(temp.getImage(), DISPLAY_SCALE)); currTileDisplay.setIcon(new ImageIcon(scaleImage(temp.getImage(), DISPLAY_SCALE * 2))); currTileLoc = t.getSource(); }
/** * Loads the tileset defined in the constructor into a JPanel which it then returns. Makes no * assumptions about height or width, which means it needs to read an extra 64 tiles at worst (32 * down to check height and 32 across for width), since the maximum height/width is 32 tiles. */ public JPanel loadTileset() { int height = MAX_TILESET_SIZE; int width = MAX_TILESET_SIZE; boolean maxHeight = false; boolean maxWidth = false; // find width int j = 0; while (!maxWidth) { try { File f = new File(tileDir + "/" + j + "_" + 0 + ".png"); ImageIO.read(f); } catch (IOException e) { width = j; maxWidth = true; } j += TILE_SIZE; } // find height int i = 0; while (!maxHeight) { try { File f = new File(tileDir + "/" + 0 + "_" + i + ".png"); ImageIO.read(f); } catch (IOException e) { height = i; maxHeight = true; } i += TILE_SIZE; } JPanel tileDisplay = new JPanel(); tileDisplay.setLayout(new GridLayout(height / TILE_SIZE, width / TILE_SIZE)); tileDisplay.setMinimumSize(new Dimension(width, height)); tileDisplay.setPreferredSize(new Dimension(width, height)); tileDisplay.setMaximumSize(new Dimension(width, height)); for (i = 0; i < height; i += TILE_SIZE) { for (j = 0; j < width; j += TILE_SIZE) { String fPath = tileDir + "/" + j + "_" + i; try { int[] mov = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; Image icon = getTile(tileDir, j, i, 1); Tile tile = new Tile(new ImageIcon(icon), "palette", 0, 0, mov, "none", false, "" + j + "_" + i); tile.addMouseListener(new PaletteButtonListener()); tile.setMaximumSize(new Dimension(TILE_SIZE, TILE_SIZE)); tile.setPreferredSize(new Dimension(TILE_SIZE, TILE_SIZE)); tile.setMinimumSize(new Dimension(TILE_SIZE, TILE_SIZE)); tileDisplay.add(tile); } catch (IOException e) { } } } return tileDisplay; }
private void addTile() { List<Tile> list = availableSpace(); if (!availableSpace().isEmpty()) { int index = (int) (Math.random() * list.size()) % list.size(); Tile emptyTime = list.get(index); emptyTime.value = Math.random() < 0.9 ? 2 : 4; } }
public Map loadMap(File input) throws FileNotFoundException { Scanner s = new Scanner(input); tileDir = s.nextLine(); int width = s.nextInt(); int height = s.nextInt(); Map toReturn = new Map(width, height, tileDir); s.nextLine(); // eat up rest of line. for (int y = 0; y < height; y++) { String line = s.nextLine(); Scanner lineReader = new Scanner(line); List<Tile> tList = new ArrayList<Tile>(); for (int x = 0; x < width; x++) { String[] values = lineReader.next().split("/"); String name = values[0]; int[] picLocation = new int[2]; for (int i = 0; i < picLocation.length; i++) { picLocation[i] = Integer.parseInt(values[1].split("_")[i]); } ImageIcon img = null; try { img = new ImageIcon(getTile(tileDir, picLocation[0], picLocation[1], DISPLAY_SCALE)); } catch (IOException e) { System.out.println("Could not find image."); img = new ImageIcon( new BufferedImage( TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE, BufferedImage.TYPE_INT_RGB)); } int avoid = Integer.parseInt(values[2]); int def = Integer.parseInt(values[3]); String[] movString = values[4].split(","); int[] moveCost = new int[movString.length]; for (int i = 0; i < moveCost.length; i++) { moveCost[i] = Integer.parseInt(movString[i]); } String special = values[5]; Tile t = new Tile( img, name, avoid, def, moveCost, special, true, "" + picLocation[0] + "_" + picLocation[1]); tList.add(t); t.setMaximumSize(new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.setPreferredSize(new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.addMouseListener(new MapButtonListener()); } toReturn.addRow(tList); } return toReturn; }
private List<Tile> availableSpace() { final List<Tile> list = new ArrayList<Tile>(16); for (Tile t : myTiles) { if (t.isEmpty()) { list.add(t); } } return list; }
public void actionPerformed(ActionEvent e) { for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { Tile t = backEnd.getTile(x, y); t.setIcon(new ImageIcon(scaleImage(currTileImg.getImage(), DISPLAY_SCALE))); // System.out.println("fillButton actionListener"); t.setSource(currTileLoc); } } }
private float x2(int x, int y) { Transcaler ts = _tile.getTranscaler(); PointsView.Orientation pvo = _points.getOrientation(); if (pvo == PointsView.Orientation.X1RIGHT_X2UP) { Projector p = _tile.getHorizontalProjector(); return (float) p.v(ts.y(y)); } else { Projector p = _tile.getVerticalProjector(); return (float) p.v(ts.x(x)); } }
private int y(float x1, float x2) { Transcaler ts = _tile.getTranscaler(); PointsView.Orientation pvo = _points.getOrientation(); if (pvo == PointsView.Orientation.X1RIGHT_X2UP) { Projector p = _tile.getHorizontalProjector(); return ts.y(p.u(x2)); } else { Projector p = _tile.getVerticalProjector(); return ts.y(p.u(x1)); } }
/** * Returns a map with the specified dimensions filled with the currTile image. The image should be * the upper-left tile of a given tileset if this is called from the constructor. */ public Map emptyMap(int width, int height) { Map toReturn = new Map(width, height, tileDir); int[] mov = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; for (int i = 0; i < height; i++) { List<Tile> tList = new ArrayList<Tile>(); for (int j = 0; j < width; j++) { Tile t = new Tile(currTileImg, "Test", 0, 0, mov, "none", true, currTileLoc); tList.add(t); // t.setMargin(new Insets(0,0,0,0)); t.setMaximumSize(new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.setPreferredSize(new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); // tile.setPreferredSize(new Dimension(TILE_SIZE*DISPLAY_SCALE, TILE_SIZE*DISPLAY_SCALE)); t.addMouseListener(new MapButtonListener()); } toReturn.addRow(tList); } return toReturn; }
private void drawTile(Graphics g2, Tile tile, int x, int y) { Graphics2D g = ((Graphics2D) g2); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); int value = tile.value; int xOffset = offsetCoors(x); int yOffset = offsetCoors(y); g.setColor(tile.getBackground()); g.fillRoundRect(xOffset, yOffset, TILE_SIZE, TILE_SIZE, 14, 14); g.setColor(tile.getForeground()); final int size = value < 100 ? 36 : value < 1000 ? 32 : 24; final Font font = new Font(FONT_NAME, Font.BOLD, size); g.setFont(font); String s = String.valueOf(value); final FontMetrics fm = getFontMetrics(font); final int w = fm.stringWidth(s); final int h = -(int) fm.getLineMetrics(s, g).getBaselineOffsets()[2]; if (value != 0) g.drawString(s, xOffset + (TILE_SIZE - w) / 2, yOffset + TILE_SIZE - (TILE_SIZE - h) / 2 - 2); if (myWin || myLose) { g.setColor(new Color(255, 255, 255, 30)); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(new Color(78, 139, 202)); g.setFont(new Font(FONT_NAME, Font.BOLD, 48)); if (myWin) { g.drawString("You won!", 68, 150); } if (myLose) { g.drawString("Game over!", 50, 130); g.drawString("You lose!", 64, 200); } if (myWin || myLose) { g.setFont(new Font(FONT_NAME, Font.PLAIN, 16)); g.setColor(new Color(128, 128, 128, 128)); g.drawString("Press ESC to play again", 80, getHeight() - 40); } } g.setFont(new Font(FONT_NAME, Font.PLAIN, 18)); g.drawString("Score: " + myScore, 200, 365); }
private List<TilePanel> getValidMovementPanels(Tile from, Unit unit) { List<TilePanel> result = new ArrayList<TilePanel>(); List<Position> validMoves = movementStrategy.getValidMoves(unit, from.getPosition(), unitMap); for (Position validMove : validMoves) { TilePanel e = map.get(board.getTileAtPosition(validMove)); if (e != null) { result.add(e); } } return result; }
protected void setActive(Component component, boolean active) { if (component instanceof Tile) { Tile tile = (Tile) component; if (active) { tile.addTiledView(_points); tile.addMouseListener(_ml); tile.addMouseWheelListener(_mwl); InputMap im = tile.getInputMap(); ActionMap am = tile.getActionMap(); im.put(KS_BACK_SPACE, "backspace"); im.put(KS_UP, "up"); im.put(KS_DOWN, "down"); am.put("backspace", _bsa); am.put("up", _uaa); am.put("down", _daa); } else { tile.removeTiledView(_points); tile.removeMouseListener(_ml); tile.removeMouseWheelListener(_mwl); InputMap im = tile.getInputMap(); ActionMap am = tile.getActionMap(); im.remove(KS_BACK_SPACE); im.remove(KS_UP); im.remove(KS_DOWN); am.remove("backspace"); am.remove("up"); am.remove("down"); } } }
private void removeMotionListener() { _tile.removeMouseMotionListener(_mml); _hasMotionListener = false; }
private void addMotionListener() { _tile.addMouseMotionListener(_mml); _hasMotionListener = true; }
private void resetPositions() { for (Tile tile : board.getTiles()) { unitMap.put(tile.getPosition(), tile.getUnits()); } }
public void mouseClicked(MouseEvent e) { Tile t = (Tile) e.getSource(); t.setIcon(new ImageIcon(scaleImage(currTileImg.getImage(), 1))); t.setSource(currTileLoc); }
@Override protected void paintComponent(Graphics graphics) { BufferedImage backImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D backGraphics = backImage.createGraphics(); BufferedImage frontImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D frontGraphics = frontImage.createGraphics(); BufferedImage particleImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D particleGraphics = particleImage.createGraphics(); Graphics2D graphics2D = (Graphics2D) graphics; graphics.setColor(Color.BLACK); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics2D.translate(-cameraX, -cameraY); backGraphics.translate(-cameraX, -cameraY); frontGraphics.translate(-cameraX, -cameraY); particleGraphics.translate(-cameraX, -cameraY); for (int y = cameraY / 64; y < (cameraY / 64) + (getHeight() / 64) + 2; y++) { for (int x = cameraX / 64; x < (cameraX / 64) + (getWidth() / 64) + 2; x++) { Tile tile = world.getTileAt(x, y); if (tile != null) { backGraphics.drawImage( client.getTextureManager().getTexture("grass"), x * 64, y * 64, null); Unit unit = tile.getUnit(); if (unit != null) { if (unit instanceof Dragon) { BufferedImage texture; if (unit.getDX() > 0) { texture = currentDragonFrame == 0 ? client.getTextureManager().getTexture("dragon_right_1") : client.getTextureManager().getTexture("dragon_right_2"); } else if (unit.getDX() < 0) { texture = currentDragonFrame == 0 ? client.getTextureManager().getTexture("dragon_left_1") : client.getTextureManager().getTexture("dragon_left_2"); } else if (unit.getDY() > 0) { texture = currentDragonFrame == 0 ? client.getTextureManager().getTexture("dragon_down_1") : client.getTextureManager().getTexture("dragon_down_2"); } else if (unit.getDY() < 0) { texture = currentDragonFrame == 0 ? client.getTextureManager().getTexture("dragon_up_1") : client.getTextureManager().getTexture("dragon_up_2"); } else { texture = currentDragonFrame == 0 ? client.getTextureManager().getTexture("dragon_down_1") : client.getTextureManager().getTexture("dragon_down_2"); } frontGraphics.drawImage( texture, (x * 64) + unit.getXOffset(), (y * 64) + unit.getYOffset(), null); particleGraphics.setColor(Color.BLACK); if (unit.getAttackTarget() != null && ((abs(unit.getAttackTarget().getTile().getX() - unit.getTile().getX()) == 1 && unit.getAttackTarget().getTile().getY() == unit.getTile().getY()) || (abs(unit.getAttackTarget().getTile().getY() - unit.getTile().getY()) == 1 && unit.getAttackTarget().getTile().getX() == unit.getTile().getX()))) { int xStart = min(unit.getTile().getX(), unit.getAttackTarget().getTile().getX()); int xEnd = max(unit.getTile().getX(), unit.getAttackTarget().getTile().getX()); int yStart = min(unit.getTile().getY(), unit.getAttackTarget().getTile().getY()); int yEnd = max(unit.getTile().getY(), unit.getAttackTarget().getTile().getY()); Random random = new Random(); for (int pX = (xStart * 64) + 28; pX < (xEnd * 64) + 34; pX++) { for (int pY = (yStart * 64) + 28; pY < (yEnd * 64) + 34; pY++) { switch (random.nextInt(3)) { case 0: particleGraphics.setColor(Color.RED); break; case 1: particleGraphics.setColor(Color.ORANGE); break; case 2: particleGraphics.setColor(Color.YELLOW); break; } particleGraphics.fillOval( pX - 3 + random.nextInt(3), pY - 3 + random.nextInt(3), 4, 4); } } } } else if (unit instanceof Wall) { TextureManager textureManager = client.getTextureManager(); BufferedImage texture = textureManager.getTexture("tower"); int offset = 128; Tile upTile = unit.getTile().getAdjacent(0, -1); boolean up = upTile != null && upTile.getUnit() != null && upTile.getUnit() instanceof Wall && upTile.getUnit().isComplete(); Tile downTile = unit.getTile().getAdjacent(0, 1); boolean down = downTile != null && downTile.getUnit() != null && downTile.getUnit() instanceof Wall && downTile.getUnit().isComplete(); Tile leftTile = unit.getTile().getAdjacent(-1, 0); boolean left = leftTile != null && leftTile.getUnit() != null && leftTile.getUnit() instanceof Wall && leftTile.getUnit().isComplete(); Tile rightTile = unit.getTile().getAdjacent(1, 0); boolean right = rightTile != null && rightTile.getUnit() != null && rightTile.getUnit() instanceof Wall && rightTile.getUnit().isComplete(); if (unit.isComplete()) { if (up) { if (down) { if (left) { if (right) { texture = textureManager.getTexture("tower_wall_up_down_left_right"); } else { texture = textureManager.getTexture("tower_wall_up_down_left"); } } else if (right) { texture = textureManager.getTexture("tower_wall_up_down_right"); } else { texture = textureManager.getTexture("wall_ver"); offset = 64; } } else if (left) { if (right) { texture = textureManager.getTexture("tower_wall_up_left_right"); } else { texture = textureManager.getTexture("tower_wall_up_left"); } } else if (right) { texture = textureManager.getTexture("tower_wall_up_right"); } else { texture = textureManager.getTexture("tower_wall_up"); } } else if (down) { if (left) { if (right) { texture = textureManager.getTexture("tower_wall_down_left_right"); } else { texture = textureManager.getTexture("tower_wall_down_left"); } } else if (right) { texture = textureManager.getTexture("tower_wall_down_right"); } else { texture = textureManager.getTexture("tower_wall_down"); } } else if (left) { if (right) { texture = textureManager.getTexture("wall_hor"); offset = 64; } else { texture = textureManager.getTexture("tower_wall_left"); } } else if (right) { texture = textureManager.getTexture("tower_wall_right"); } } else { texture = textureManager.getTexture("wall_in_progress"); offset = 64; } frontGraphics.drawImage( texture, (x * 64) + unit.getXOffset(), (y * 64) + unit.getYOffset() - offset, null); } else if (unit instanceof Flag) { BufferedImage texture = null; if (currentFlagFrame == 0) texture = client.getTextureManager().getTexture("flag_1"); else if (currentFlagFrame == 1) texture = client.getTextureManager().getTexture("flag_2"); else if (currentFlagFrame == 2) texture = client.getTextureManager().getTexture("flag_3"); else if (currentFlagFrame == 3) texture = client.getTextureManager().getTexture("flag_4"); if (texture != null) frontGraphics.drawImage(texture, (x * 64), (y * 64) - 64, null); } } } } } int mouseTileX = ((cameraX - (int) getLocationOnScreen().getX() + (int) MouseInfo.getPointerInfo().getLocation().getX()) / 64); int mouseTileY = ((cameraY - (int) getLocationOnScreen().getY() + (int) MouseInfo.getPointerInfo().getLocation().getY()) / 64); Tile mouseTile = getWorld().getTileAt(mouseTileX, mouseTileY); if (mouseTile != null) { if (mouseTile.getUnit() == null) { if (client.getShopPanel().getSelectedItem() == null) { backGraphics.setColor(new Color(0F, 1F, 0F, 0.5F)); } else { backGraphics.setColor(new Color(0F, 0F, 1F, 0.5F)); } } else { backGraphics.setColor(new Color(1F, 0F, 0F, 0.5F)); } backGraphics.fillRect(mouseTileX * 64, mouseTileY * 64, 64, 64); if (mouseTile.getUnit() == null) { if (client.getShopPanel().getSelectedItem() == null) { backGraphics.setColor(Color.GREEN); } else { backGraphics.setColor(Color.BLUE); } } else { backGraphics.setColor(Color.RED); } backGraphics.drawRect( ((cameraX - (int) getLocationOnScreen().getX() + (int) MouseInfo.getPointerInfo().getLocation().getX()) / 64) * 64, ((cameraY - (int) getLocationOnScreen().getY() + (int) MouseInfo.getPointerInfo().getLocation().getY()) / 64) * 64, 64, 64); } graphics2D.translate(cameraX, cameraY); backGraphics.translate(cameraX, cameraY); frontGraphics.translate(cameraX, cameraY); particleGraphics.translate(cameraX, cameraY); graphics.drawImage(backImage, 0, 0, null); graphics.drawImage(frontImage, 0, 0, null); graphics.drawImage(particleImage, 0, 0, null); backGraphics.dispose(); frontGraphics.dispose(); particleGraphics.dispose(); backImage.flush(); frontImage.flush(); particleImage.flush(); }
public void actionPerformed(ActionEvent e) { int tempW = mapWidth; int tempH = mapHeight; int targetW; int targetH; try { targetW = Integer.parseInt(widthField.getText()); targetH = Integer.parseInt(heightField.getText()); if (targetH <= 0 || targetW <= 0) { JOptionPane.showMessageDialog(null, "Both x and y must be above 0."); } else { // shrink width if necessary while (targetW < mapWidth) { for (int i = mapHeight * mapWidth - 1; i >= 0; i -= mapWidth) { map.remove(i); } mapWidth--; mapScroll.revalidate(); map.repaint(); backEnd.removeColumn(backEnd.getWidth() - 1); } // shrink height if necessary while (targetH < mapHeight) { for (int i = 1; i <= mapWidth; i++) { map.remove(mapHeight * mapWidth - i); } mapHeight--; // map.setLayout(new GridLayout(mapHeight,mapWidth)); map.repaint(); backEnd.removeRow(backEnd.getHeight() - 1); } // Grow if necessary if (targetW > mapWidth || targetH > mapHeight) { // add new rows and columns, then rebuild and re-add display int[] mov = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; while (targetW > mapWidth) { // add new column to backEnd List<Tile> tList = new ArrayList<Tile>(); for (int i = 0; i < mapHeight; i++) { Tile t = new Tile(currTileImg, "Test", 0, 0, mov, "none", true, currTileLoc); t.setPreferredSize( new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.addMouseListener(new MapButtonListener()); tList.add(t); } backEnd.addColumn(tList); mapWidth++; } while (targetH > mapHeight) { // add new row to backEnd List<Tile> tList = new ArrayList<Tile>(); for (int i = 0; i < mapWidth; i++) { Tile t = new Tile(currTileImg, "Test", 0, 0, mov, "none", true, currTileLoc); t.setPreferredSize( new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.addMouseListener(new MapButtonListener()); tList.add(t); } backEnd.addRow(tList); mapHeight++; } GridBagConstraints gbc = new GridBagConstraints(); for (int i = 0; i < mapHeight; i++) { gbc.gridy = i; for (int j = 0; j < mapWidth; j++) { gbc.gridx = j; map.add(backEnd.getTile(j, i), gbc); } } map.revalidate(); map.repaint(); parentPanel.revalidate(); parentPanel.repaint(); ((MapBuilder) SwingUtilities.getWindowAncestor(parentPanel)).pack(); } } } catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Both x and y must be valid integers."); } }