diff options
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/fourisland/frigidearth/Main.java | 72 |
1 files changed, 68 insertions, 4 deletions
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 @@ | |||
4 | */ | 4 | */ |
5 | package com.fourisland.frigidearth; | 5 | package com.fourisland.frigidearth; |
6 | 6 | ||
7 | import java.awt.Color; | ||
8 | import java.awt.Graphics; | ||
9 | import java.awt.event.ComponentAdapter; | ||
10 | import java.awt.event.ComponentEvent; | ||
11 | import java.util.Random; | ||
7 | import javax.swing.JFrame; | 12 | import javax.swing.JFrame; |
8 | 13 | ||
9 | /** | 14 | /** |
@@ -14,14 +19,73 @@ public class Main | |||
14 | { | 19 | { |
15 | static final int GAME_WIDTH = 15; | 20 | static final int GAME_WIDTH = 15; |
16 | static final int GAME_HEIGHT = 10; | 21 | static final int GAME_HEIGHT = 10; |
17 | static final int TILE_WIDTH = 32; | 22 | static int TILE_WIDTH = 32; |
18 | static final int TILE_HEIGHT = 32; | 23 | static int TILE_HEIGHT = 32; |
24 | |||
25 | private static JFrame mainWindow; | ||
26 | private static Color[][] grid; | ||
27 | private static int drawOffsetX = 0; | ||
28 | private static int drawOffsetY = 0; | ||
19 | 29 | ||
20 | public static void main(String[] args) | 30 | public static void main(String[] args) |
21 | { | 31 | { |
22 | JFrame mainWindow = new JFrame("Frigid Earth"); | 32 | mainWindow = new JFrame("Frigid Earth"); |
23 | mainWindow.setSize(GAME_WIDTH*TILE_WIDTH, GAME_HEIGHT*TILE_HEIGHT); | 33 | mainWindow.setSize(GAME_WIDTH*TILE_WIDTH, GAME_HEIGHT*TILE_HEIGHT + mainWindow.getInsets().top); |
24 | mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | 34 | mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
35 | mainWindow.addComponentListener(new ComponentAdapter() { | ||
36 | @Override | ||
37 | public void componentResized(ComponentEvent ce) | ||
38 | { | ||
39 | drawOffsetX = 0; | ||
40 | drawOffsetY = 0; | ||
41 | |||
42 | TILE_WIDTH = mainWindow.getContentPane().getWidth() / GAME_WIDTH; | ||
43 | drawOffsetX = mainWindow.getWidth() % GAME_WIDTH / 2; | ||
44 | TILE_HEIGHT = mainWindow.getContentPane().getHeight() / GAME_HEIGHT; | ||
45 | drawOffsetY = mainWindow.getHeight() % GAME_HEIGHT / 2; | ||
46 | |||
47 | if (TILE_WIDTH > TILE_HEIGHT) | ||
48 | { | ||
49 | TILE_WIDTH = TILE_HEIGHT; | ||
50 | drawOffsetX = (mainWindow.getContentPane().getWidth() - (GAME_WIDTH * TILE_WIDTH)) / 2; | ||
51 | } else if (TILE_HEIGHT > TILE_WIDTH) | ||
52 | { | ||
53 | TILE_HEIGHT = TILE_WIDTH; | ||
54 | drawOffsetY = (mainWindow.getContentPane().getHeight() - (GAME_HEIGHT * TILE_HEIGHT)) / 2; | ||
55 | } | ||
56 | |||
57 | redrawScreen(); | ||
58 | } | ||
59 | }); | ||
25 | mainWindow.setVisible(true); | 60 | mainWindow.setVisible(true); |
61 | |||
62 | Random r = new Random(); | ||
63 | grid = new Color[GAME_WIDTH][GAME_HEIGHT]; | ||
64 | |||
65 | for (int x=0; x<GAME_WIDTH; x++) | ||
66 | { | ||
67 | for (int y=0; y<GAME_HEIGHT; y++) | ||
68 | { | ||
69 | grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | redrawScreen(); | ||
74 | } | ||
75 | |||
76 | private static void redrawScreen() | ||
77 | { | ||
78 | Graphics g = mainWindow.getGraphics(); | ||
79 | g.setColor(Color.black); | ||
80 | g.fillRect(0, 0, mainWindow.getWidth(), mainWindow.getHeight()); | ||
81 | |||
82 | for (int x=0; x<GAME_WIDTH; x++) | ||
83 | { | ||
84 | for (int y=0; y<GAME_HEIGHT; y++) | ||
85 | { | ||
86 | g.setColor(grid[x][y]); | ||
87 | g.fillRect(x*TILE_WIDTH + drawOffsetX, y*TILE_HEIGHT + drawOffsetY + mainWindow.getInsets().top, TILE_WIDTH, TILE_HEIGHT); | ||
88 | } | ||
89 | } | ||
26 | } | 90 | } |
27 | } | 91 | } |