From 73d3739ac1b4357eab40d730bad2953185276760 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 2 Jun 2012 15:09:55 -0400 Subject: Added variable size tiles --- src/com/fourisland/frigidearth/Main.java | 72 ++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) (limited to 'src/com') diff --git a/src/com/fourisland/frigidearth/Main.java b/src/com/fourisland/frigidearth/Main.java index 4301118..2e320cc 100644 --- a/src/com/fourisland/frigidearth/Main.java +++ b/src/com/fourisland/frigidearth/Main.java @@ -4,6 +4,11 @@ */ package com.fourisland.frigidearth; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; import javax.swing.JFrame; /** @@ -14,14 +19,73 @@ public class Main { static final int GAME_WIDTH = 15; static final int GAME_HEIGHT = 10; - static final int TILE_WIDTH = 32; - static final int TILE_HEIGHT = 32; + static int TILE_WIDTH = 32; + static int TILE_HEIGHT = 32; + + private static JFrame mainWindow; + private static Color[][] grid; + private static int drawOffsetX = 0; + private static int drawOffsetY = 0; public static void main(String[] args) { - JFrame mainWindow = new JFrame("Frigid Earth"); - mainWindow.setSize(GAME_WIDTH*TILE_WIDTH, GAME_HEIGHT*TILE_HEIGHT); + mainWindow = new JFrame("Frigid Earth"); + mainWindow.setSize(GAME_WIDTH*TILE_WIDTH, GAME_HEIGHT*TILE_HEIGHT + mainWindow.getInsets().top); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + mainWindow.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent ce) + { + drawOffsetX = 0; + drawOffsetY = 0; + + TILE_WIDTH = mainWindow.getContentPane().getWidth() / GAME_WIDTH; + drawOffsetX = mainWindow.getWidth() % GAME_WIDTH / 2; + TILE_HEIGHT = mainWindow.getContentPane().getHeight() / GAME_HEIGHT; + drawOffsetY = mainWindow.getHeight() % GAME_HEIGHT / 2; + + if (TILE_WIDTH > TILE_HEIGHT) + { + TILE_WIDTH = TILE_HEIGHT; + drawOffsetX = (mainWindow.getContentPane().getWidth() - (GAME_WIDTH * TILE_WIDTH)) / 2; + } else if (TILE_HEIGHT > TILE_WIDTH) + { + TILE_HEIGHT = TILE_WIDTH; + drawOffsetY = (mainWindow.getContentPane().getHeight() - (GAME_HEIGHT * TILE_HEIGHT)) / 2; + } + + redrawScreen(); + } + }); mainWindow.setVisible(true); + + Random r = new Random(); + grid = new Color[GAME_WIDTH][GAME_HEIGHT]; + + for (int x=0; x