import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class MainPanel extends JPanel implements MouseListener, MouseMotionListener { public int width = 20; public Tile[][] tileArray = new Tile[width][width]; public int size; public boolean selectionMode = false; public boolean attackMode = false; public boolean action = false; public boolean miss = false; public int[] selection = {0, 0}; public int[] attaction = {0, 0}; public int[] location = {0, 0}; private int len; private Color RED = new Color(165, 66, 66); private Color YELLOW = new Color(250, 198, 116); private Color GREEN = new Color(181, 189, 104); private Color BLACK = new Color(29, 31, 33); private Color WHITE = new Color(197, 200, 198); private Color BLUE = new Color(95, 129, 157); private Color ORANGE = new Color(222, 147, 95); private Color DARKGREEN = new Color(140, 148, 64); private Color BROWN = new Color(51, 41, 33); private Random rand = new Random(); public MainPanel () { for (int row = 0; row < width; row++) { for (int col = 0; col < width; col++) { tileArray[row][col] = new Tile(row, col, this.size, this.width); } } tileArray[0][0].entityType = EntityType.PLAYER; for (int i = 0; i < 35; i++) { int row = rand.nextInt(width); int col = rand.nextInt(width); int type = rand.nextInt(4); if (tileArray[row][col].entityType == EntityType.NULL) { if (type == 0) { tileArray[row][col].entityType = EntityType.ENEMY; } else { tileArray[row][col].entityType = EntityType.TREE; } } } this.addMouseListener(this); this.addMouseMotionListener(this); } public void paintComponent(Graphics g) { if (this.getWidth() > this.getHeight()) { this.size = this.getHeight(); } else { this.size = this.getWidth(); } super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int bar = this.size/20; g2d.setColor(WHITE); g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); g2d.setColor(BLACK); g2d.fillRect(0, this.size-bar, this.size-bar, bar); g2d.setColor(WHITE); g2d.setFont(new Font("Arial", Font.BOLD, bar-bar/10)); if (this.miss) { g2d.drawString("MISS", this.size-bar-(bar-bar/10)*3, this.size-bar/10); } if (action) { g2d.drawString("PAS", 0, this.size-bar/10); g2d.drawString("ATK", (bar-bar/10)*3, this.size-bar/10); } for (int row = 0; row < width; row++) { for (int col = 0; col < width; col++) { this.tileArray[row][col].calc(row, col, this.size-bar); int x = this.tileArray[row][col].x; int y = this.tileArray[row][col].y; this.len = this.tileArray[row][col].len; int border = len/100; if (this.tileArray[row][col].tileType == TileType.GRASS) { g2d.setColor(GREEN); g2d.fillRect(x, y, len, len); g2d.setColor(BLACK); g2d.setStroke(new BasicStroke(len/50)); g2d.drawRect(x, y, len, len); } else if ( this.tileArray[row][col].tileType == TileType.WATER) { g2d.setColor(Color.BLUE); g2d.fillRect(x, y, len, len); g2d.setColor(BLACK); g2d.setStroke(new BasicStroke(len/50)); g2d.drawRect(x, y, len, len); } int spacing = len/12; if (this.tileArray[row][col].entityType == EntityType.PLAYER) { g2d.setColor(BLUE); g2d.fillRect( x+spacing, y+spacing, len-spacing*2, len-spacing*2); this.location[0] = x; this.location[1] = y; this.selection[0] = row; this.selection[1] = col; } if (this.tileArray[row][col].entityType == EntityType.ENEMY) { g2d.setColor(RED); g2d.fillRect( x+spacing, y+spacing, len-spacing*2, len-spacing*2); } if (this.tileArray[row][col].entityType == EntityType.TREE) { g2d.setColor(DARKGREEN); g2d.fillRect( x+spacing, y+spacing, len-spacing*2, len-spacing*2); g2d.setColor(BROWN); g2d.fillRect(x+len/3, y+len/3, len/3, len/3); } if (this.tileArray[row][col].selected) { if (this.tileArray[row][col].entityType == EntityType.NULL) { g2d.setColor(YELLOW); g2d.fillRect(x+len/3, y+len/3, len/3, len/3); } } if (this.tileArray[row][col].attack) { g2d.setColor(ORANGE); g2d.setStroke(new BasicStroke(this.len/6)); g2d.drawRect(x+len/4, y+len/4, len*2/4, len*2/4); } } } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseMoved(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (attackMode) { for (int row = 0; row < width; row++) { for (int col = 0; col < width; col++) { this.tileArray[row][col].attack = false; if ( x > this.tileArray[row][col].x && x < this.tileArray[row][col].x + this.tileArray[row][col].len && y > this.tileArray[row][col].y && y < this.tileArray[row][col].y + this.tileArray[row][col].len ) { this.tileArray[row][col].attack = true; } } } } this.repaint(); } public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); int bar = this.size/20; boolean stop = false; this.miss = false; if ( x > 0 && x < (bar-bar/10)*3 && y > this.size-bar && y < this.size ) { this.action = false; } else if ( x > (bar-bar/10)*3 && x < (bar-bar/10)*6 && y > this.size-bar && y < this.size ) { this.attackMode = true; this.action = false; } else { for (int row = 0; row < width; row++) { for (int col = 0; col < width; col++) { if ( x > this.tileArray[row][col].x && x < this.tileArray[row][col].x + this.tileArray[row][col].len && y > this.tileArray[row][col].y && y < this.tileArray[row][col].y + this.tileArray[row][col].len ) { if (this.selectionMode) { if (this.tileArray[row][col].selected) { this.tileArray[this.selection[0]][this.selection[1]].entityType = EntityType.NULL; this.tileArray[row][col].entityType = EntityType.PLAYER; this.selectionMode = false; this.action = true; } } else if (this.attackMode) { int dist = (int) Math.sqrt(Math.pow(row - this.selection[0], 2) + Math.pow(col - this.selection[1], 2)); if (rand.nextInt(13) > dist) { if (this.tileArray[row][col].entityType == EntityType.ENEMY) { this.tileArray[row][col].entityType = EntityType.NULL; } else if (this.tileArray[row][col].entityType == EntityType.TREE) { this.tileArray[row][col].entityType = EntityType.NULL; } } else { this.miss = true; } this.tileArray[row][col].attack = false; this.attackMode = false; } else if (this.tileArray[row][col].entityType == EntityType.PLAYER) { this.selectionMode = true; this.selection[0] = row; this.selection[1] = col; for (int srow = -4; srow <= 4; srow++) { for (int scol = -4; scol <= 4; scol++) { if (Math.abs(srow) + Math.abs(scol) <= 4) { try { if (this.tileArray[row+srow][col+scol].entityType == EntityType.NULL) { this.tileArray[row+srow][col+scol].selected = true; } } catch (ArrayIndexOutOfBoundsException ex) {} } } } } } } } if (!selectionMode) { for (int row = 0; row < width; row++) { for (int col = 0; col < width; col++) { this.tileArray[row][col].selected = false; } } } } this.repaint(); } }