Hi everyone I'm new to java and are making a big project for the first time therefor I want to creative some menu's. In this conetext I'm using borderlayout. In my SOUTH reagion, I have a gridlayout with some buttons and info boxes, this is working fine. In my CENTER region I have my "canvas" where I use Graphics to draw, this is where my game is drawed. In my EAST region, I'm trying to use BoxLayout to make another menu, so far I have added a headline, a gridLayout with a lot of bottun and another headline. This gridlayout keeps bugging for me and I dont know what to do. Here are my code:
public class VectorTD { public static GameFrame frame; public static void main(String[] args) { // Creates the display frame = new GameFrame(); } }
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 int round = 0; static public int interest = 3; 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); Vectoid.drawVectoids(g); Vectoid.spawnVectoids(); 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"); } } } }
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 = 40; 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 * width, j * width); } } 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 -= width; xDist += width; } else if (xDist < 0) { x1 += width; xDist -= width; } else if (yDist > 0) { y1 -= width; yDist += width; } else { y1 += width; yDist -= width; } } else if (i == makeMap.length - 1) { if (xDist > 0) { x2 += width; xDist += width; } else if (xDist < 0) { x2 -= width; xDist -= width; } else if (yDist > 0) { y2 += width; yDist += width; } else { y2 -= width; yDist -= width; } } 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; } }
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]; static int countDead = 0; 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 slowTime = 0; float slowSpeed = 0; int health = 100; Color bodyColor = new Color(255, 0, 0); boolean dead = false; static long timer; 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; countDead++; VectorTD.frame.livesLabel.setText("Lives: " + GamePanel.lives); if (countDead == maxNumberOfVectoids) { GamePanel.roundStart = false; } } } public void takeDamage(int damage) { health -= damage; if (health <= 0) { dead = true; GamePanel.money += 10; } } public static void drawVectoids(Graphics g) { if (GamePanel.roundStart == true) { for (int i = 0; i < Vectoid.currentNumberOfVectoids; i++) { if (listOfVectoids[i].dead == false) { listOfVectoids[i].draw(g); listOfVectoids[i].move(); } } } } public static void makeVectoids() { for (int i = 0; i < maxNumberOfVectoids; i++) { listOfVectoids[i] = new Vectoid(); } } public static void spawnVectoids() { if ((currentNumberOfVectoids < maxNumberOfVectoids) && (System.nanoTime() - timer) > 300000000) { listOfVectoids[currentNumberOfVectoids].dead = false; listOfVectoids[currentNumberOfVectoids].health = 100; listOfVectoids[currentNumberOfVectoids].distance = 0; currentNumberOfVectoids++; timer = System.nanoTime(); } } public static void newRound() { if (GamePanel.roundStart == false) { GamePanel.roundStart = true; currentNumberOfVectoids = 0; timer = System.nanoTime(); } } }
//pakke der styrer farver import java.awt.Color; import javax.swing.*; //Pakke der hjælper med at lave vinduet import javax.swing.JFrame; import javax.swing.JButton; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; public class GameFrame extends JFrame { static final int SCREEN_WIDTH = 1100; static final int SCREEN_HEIGHT = 800; final JLabel moneyLabel = new JLabel("Money: " + GamePanel.money + "$", SwingConstants.CENTER); final JLabel livesLabel = new JLabel("Lives: " + GamePanel.lives, SwingConstants.CENTER); final JLabel roundLabel = new JLabel("Round: " + GamePanel.round, SwingConstants.CENTER); final JLabel interestLabel = new JLabel("Interest: " + GamePanel.interest + "%", SwingConstants.CENTER); JPanel topMenu; JPanel screen; GameFrame() { screen = new JPanel(); screen.setLayout(new BorderLayout()); // adding top menu addTopMenu(); // adding game screen.add(new GamePanel(), BorderLayout.CENTER); // adding side menu addSideMenu(); this.setContentPane(screen); // Stopper programmet når vinduet lukkes setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Vinduets størrelse setSize(SCREEN_WIDTH, SCREEN_HEIGHT); // Vinduets baggrundsfarve this.setBackground(Color.BLACK); // Viser vinduet setVisible(true); // Gør at man ikke kan ændre størrelse på vinduet setResizable(false); } void addSideMenu() { JPanel sideMenu = new JPanel(); sideMenu.setLayout(new BoxLayout(sideMenu, BoxLayout.Y_AXIS)); //sideMenu.CENTER_ALIGNMENT; sideMenu.setBackground(Color.BLACK); JPanel towerMenu = new JPanel(); towerMenu.setLayout(new GridLayout(2, 4, 0, 0)); towerMenu.setBackground(Color.BLACK); JButton Green_Laser_Mk1 = new JButton(new ImageIcon("Images/Green_Laser_Mk1.png")); towerMenu.add(Green_Laser_Mk1); JButton Purple_Power_Mk1 = new JButton(new ImageIcon("Images/Purple_Power_Mk1.png")); towerMenu.add(Purple_Power_Mk1); JButton Orange_Incinerator_Mk1 = new JButton(new ImageIcon("Images/Orange_Incinerator_Mk1.png")); towerMenu.add(Orange_Incinerator_Mk1); JButton Blue_Rays_Mk1 = new JButton(new ImageIcon("Images/Blue_Rays_Mk1.png")); towerMenu.add(Blue_Rays_Mk1); JButton Green_Laser_Mk2 = new JButton(new ImageIcon("Images/Green_Laser_Mk2.png")); towerMenu.add(Green_Laser_Mk2); JButton Purple_Power_Mk2 = new JButton(new ImageIcon("Images/Purple_Power_Mk2.png")); towerMenu.add(Purple_Power_Mk2); JButton Orange_Incinerator_Mk2 = new JButton(new ImageIcon("Images/Orange_Incinerator_Mk2.png")); towerMenu.add(Orange_Incinerator_Mk2); JButton Blue_Rays_Mk2 = new JButton(new ImageIcon("Images/Blue_Rays_Mk2.png")); towerMenu.add(Blue_Rays_Mk2); final JLabel towerMenuName = new JLabel("Towers"); towerMenuName.setForeground(Color.white); towerMenuName.setBackground(new Color(4, 23, 22)); towerMenuName.setFont(new Font("Verdana", Font.PLAIN, 25)); final JLabel towerInformation = new JLabel("Orange_Incinerator_Mk1", SwingConstants.CENTER); towerInformation.setForeground(Color.white); towerInformation.setBackground(new Color(4, 23, 22)); towerInformation.setFont(new Font("Verdana", Font.PLAIN, 25)); sideMenu.add(towerMenuName); sideMenu.add(towerMenu); sideMenu.add(towerInformation); screen.add(sideMenu, BorderLayout.EAST); //screen.add(towerMenu, BorderLayout.EAST); } void addTopMenu() { topMenu = new JPanel(); topMenu.setLayout(new GridLayout(2, 3, 10, 10)); topMenu.setBackground(Color.BLACK); JButton roundStart = new JButton("Next round"); roundStart.setBackground(new Color(245, 28, 92)); roundStart.setFocusPainted(false); roundStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { Vectoid.newRound(); } }); JButton autoStart = new JButton("Auto start: " + GamePanel.roundStart); autoStart.setBackground(new Color(245, 28, 92)); autoStart.setFocusPainted(false); addLabel(moneyLabel); addLabel(livesLabel); topMenu.add(roundStart); addLabel(roundLabel); addLabel(interestLabel); topMenu.add(autoStart); screen.add(topMenu, BorderLayout.NORTH); } void addLabel(JLabel jLabel) { jLabel.setOpaque(true); jLabel.setForeground(Color.white); jLabel.setBackground(new Color(4, 23, 22)); topMenu.add(jLabel); } }
Also I have a folder with images.