I'm new to java and are working on my first big projekt which is a game. In this tower defence game I want a couple of menues, to both display information and have bottuns. To make this i'm using BorderLayout(), but I can't figure out how to use it proberly. Here are my code:
Main class:
public class VectorTD{ public static void main(String[] args) { // Creates the display GameFrame frame = new GameFrame(); } }
GameFrame class that draws everthing:
/pakke der styrer farver import java.awt.Color; import javax.swing.*; //Pakke der hjælper med at lave vinduet import javax.swing.JFrame; import java.awt.*; public class GameFrame extends JFrame { static final int SCREEN_WIDTH = 1000; static final int SCREEN_HEIGHT = 1000; GameFrame() { this.setLayout(new BorderLayout()); //the bottun this.add(new Menu(), BorderLayout.NORTH); //the game this.add(new GamePanel(), BorderLayout.CENTER); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(SCREEN_WIDTH, SCREEN_HEIGHT); this.setBackground(Color.BLACK); setVisible(true); setResizable(false); } }
Class that should make the bottun
import java.awt.*; import javax.swing.*; import java.awt.Graphics; import javax.swing.JButton; public class Menu extends JPanel{ Menu() { JButton b=new JButton("Click Here"); b.setBounds(50,100,95,30); this.add(b); } private void makeTopMenu() { } }
GamePanel class:
import java.awt.*; import javax.swing.*; import java.awt.Graphics; public class GamePanel extends JPanel implements Runnable { static Graphics graphics; final static int FPS = 100; final static public long SKIP_TICKS = 1000000000 / FPS; static public int lives = 25; static public int money = 100; static public boolean roundStart = false; GamePanel() { new Thread(this).start(); Square.makeGrid(); Vectoid.makeVectoids(); } public void paint(Graphics g) { Image image = createImage(GameFrame.SCREEN_WIDTH, GameFrame.SCREEN_HEIGHT); graphics = image.getGraphics(); draw(graphics); g.drawImage(image, 0, 0, this); } public void draw(Graphics g) { Square.drawGrid(g); if (roundStart==true){ for (int i = 0; i < Vectoid.currentNumberOfVectoids; i++) { if (Vectoid.listOfVectoids[i].dead == false) { Vectoid.listOfVectoids[i].draw(g); Vectoid.listOfVectoids[i].move(); } } } Toolkit.getDefaultToolkit().sync(); } // Gameloop public void run() { // long then = System.nanoTime(); // The gameloop runs until the program is closed while (true) { // Saves the current time long now = System.nanoTime(); repaint(); // After the game has been updated, the program needs to wait until next // gameupdate. This waiting time is determined by the FPS variable. long sleeptime = SKIP_TICKS - (System.nanoTime() - now); if (sleeptime >= 0) { try { Thread.sleep(sleeptime / 1000000); } catch (Exception e) { System.out.println("Error: Can't sleep."); } } // If sleeptime < 0, the program is not updated fast enough else { System.out.println("We are running behind"); } } } }
Square class:
import java.awt.*; import java.awt.Color; public class Square extends Rectangle { static int rows = 15; static int collums = 15; public static Square grid[][] = new Square[rows][collums]; public static int[][] makeMap = { { 0, 0 }, { 5, 0 }, { 5, 5 }, { 3, 5 }, { 3, 3 }, { 0, 3 }, { 0, 10 }, { 10, 10 }, { 10, 5 }, { 14, 5 } }; int x, y; static int width = 50; int strokeWeigth = 2; Color bodyColor = new Color(29, 69, 64); Color strokeColor = new Color(0, 0, 0); boolean isTowerPlacebel = true; public static int[][] vectoidRoute; Square(int x, int y) { this.x = x; this.y = y; } private void draw(Graphics g) { g.setColor(strokeColor); g.fillRect(x, y, width, width); g.setColor(bodyColor); g.fillRect(x + strokeWeigth, y + strokeWeigth, width - strokeWeigth * 2, width - strokeWeigth * 2); } public static void makeGrid() { for (int i = 0; i < collums; i++) { for (int j = 0; j < rows; j++) { grid[i][j] = new Square(i * 50, j * 50 + 130); } } makeMap(); calculateRouteLength(); makeVectoidRoute(); } public static void drawGrid(Graphics g) { for (int i = 0; i < collums; i++) { for (int j = 0; j < rows; j++) { grid[i][j].draw(g); } } } private static void makeMap() { for (int i = 1; i < makeMap.length; i++) { if (makeMap[i - 1][0] < makeMap[i][0]) { for (int j = makeMap[i - 1][0]; j <= makeMap[i][0]; j++) { grid[j][makeMap[i][1]].isTowerPlacebel = false; grid[j][makeMap[i][1]].bodyColor = new Color(0, 41, 0); } } else if (makeMap[i - 1][1] < makeMap[i][1]) { for (int j = makeMap[i - 1][1]; j <= makeMap[i][1]; j++) { grid[makeMap[i][0]][j].isTowerPlacebel = false; grid[makeMap[i][0]][j].bodyColor = new Color(0, 41, 0); } } else if (makeMap[i - 1][0] > makeMap[i][0]) { for (int j = makeMap[i - 1][0]; j >= makeMap[i][0]; j--) { grid[j][makeMap[i][1]].isTowerPlacebel = false; grid[j][makeMap[i][1]].bodyColor = new Color(0, 41, 0); } } else if (makeMap[i - 1][1] > makeMap[i][1]) { for (int j = makeMap[i - 1][1]; j >= makeMap[i][1]; j--) { grid[makeMap[i][0]][j].isTowerPlacebel = false; grid[makeMap[i][0]][j].bodyColor = new Color(0, 41, 0); } } } } private static void calculateRouteLength() { int length = 0; for (int i = 1; i < makeMap.length; i++) { length += Math.abs(grid[makeMap[i][0]][makeMap[i][1]].x - grid[makeMap[i - 1][0]][makeMap[i - 1][1]].x); length += Math.abs(grid[makeMap[i][0]][makeMap[i][1]].y - grid[makeMap[i - 1][0]][makeMap[i - 1][1]].y); } length += width * 2; vectoidRoute = new int[length + 1][2]; } private static void makeVectoidRoute() { int number = 0; int x2 = 0; int y2 = 0; for (int i = 1; i < makeMap.length; i++) { int x1 = grid[makeMap[i - 1][0]][makeMap[i - 1][1]].x; int y1 = grid[makeMap[i - 1][0]][makeMap[i - 1][1]].y; x2 = grid[makeMap[i][0]][makeMap[i][1]].x; y2 = grid[makeMap[i][0]][makeMap[i][1]].y; int xDist = x2 - x1; int yDist = y2 - y1; if (i == 1) { if (xDist > 0) { x1 -= 50; xDist += 50; } else if (xDist < 0) { x1 += 50; xDist -= 50; } else if (yDist > 0) { y1 -= 50; yDist += 50; } else { y1 += 50; yDist -= 50; } } else if (i == makeMap.length - 1) { if (xDist > 0) { x2 += 50; xDist += 50; } else if (xDist < 0) { x2 -= 50; xDist -= 50; } else if (yDist > 0) { y2 += 50; yDist += 50; } else { y2 -= 50; yDist -= 50; } } if (xDist > 0 || yDist > 0) { for (int j = 0; j != xDist + yDist; j++) { if (yDist == 0) { vectoidRoute[number][0] = x1 + j; vectoidRoute[number][1] = y1; } else { vectoidRoute[number][0] = x1; vectoidRoute[number][1] = y1 + j; } number++; } } else { for (int j = 0; j != Math.abs(xDist + yDist); j++) { if (yDist == 0) { vectoidRoute[number][0] = x1 - j; vectoidRoute[number][1] = y1; } else { vectoidRoute[number][0] = x1; vectoidRoute[number][1] = y1 - j; } number++; } } } vectoidRoute[number][0] = x2; vectoidRoute[number][1] = y2; } }
Last class:
import java.awt.*; import java.awt.Color; public class Vectoid { static int maxNumberOfVectoids = 10; static int currentNumberOfVectoids = 0; public static Vectoid listOfVectoids[] = new Vectoid[10]; int radius = 50; float distance = 0; int x = Square.vectoidRoute[Math.round(distance)][0]; int y = Square.vectoidRoute[Math.round(distance)][1]; float speed = 3; int timer = 0; int slowTime = 0; float slowSpeed = 0; int health = 100; Color bodyColor = new Color(255, 0, 0); boolean dead = false; public void draw(Graphics g) { x = Square.vectoidRoute[Math.round(distance)][0]; y = Square.vectoidRoute[Math.round(distance)][1]; g.setColor(bodyColor); g.fillOval(x, y, radius, radius); } public void move() { distance += speed; outOfMap(); } private void outOfMap() { if (distance >= Square.vectoidRoute.length) { dead = true; GamePanel.lives -= 1; } } public void takeDamage(int damage) { health -= damage; if (health <= 0) { dead = true; GamePanel.money += 10; } } public static void makeVectoids() { for (int i = 0; i < maxNumberOfVectoids; i++) { listOfVectoids[i] = new Vectoid(); } } public static void newRound() { GamePanel.roundStart = true; for (int i = 0; i < maxNumberOfVectoids; i++) { listOfVectoids[i].dead = false; listOfVectoids[i].health = 100; currentNumberOfVectoids++; } } }