|
001 //start auto-imports
002 //end auto-imports
003 //start auto-imports
004 importimport means to make the classes and/or packages available in this program java.util.*;
005 //end auto-imports
006 importimport means to make the classes and/or packages available in this program javax.swing.*;
007 importimport means to make the classes and/or packages available in this program java.awt.*;
008 importimport means to make the classes and/or packages available in this program java.util.Random;
009
010 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects Map extendsextends means to customize or extend the functionality of a class JPanel
011 {open braces start code blocks and must be matched with a close brace
012
013 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color CITY =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 214, 217, 223 );
014 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color DESERT =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 255, 204, 102 );
015 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color DIRT_ROAD =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 153, 102, 0 );
016 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color FOREST =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 0, 102, 0 );
017 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color HILLS =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 51, 153, 0 );
018 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color LAKE =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 0, 153, 153 );
019 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color MOUNTAINS =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 102, 102, 255 );
020 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color OCEAN =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 0, 0, 153 );
021 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color PAVED_ROAD =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 51, 51, 0 );
022 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color PLAINS =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color( 102, 153, 0 );
023
024 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays TERRAIN =this assignment operator makes the left side equal to the right side {open braces start code blocks and must be matched with a close brace
025 CITY,
026 DESERT,
027 DIRT_ROAD,
028 FOREST,
029 HILLS,
030 LAKE,
031 MOUNTAINS,
032 OCEAN,
033 PAVED_ROAD,
034 PLAINS
035 }close braces end code blocks and must match an earlier open brace;
036
037 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer NUM_ROWS =this assignment operator makes the left side equal to the right side 75;
038 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer NUM_COLS =this assignment operator makes the left side equal to the right side 90;
039
040 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer PREFERRED_GRID_SIZE_PIXELS =this assignment operator makes the left side equal to the right side 10;
041
042 // In reality you will probably want a class here to represent a map tile,
043 // which will include things like dimensions, color, properties in the
044 // game world. Keeping simple just to illustrate.
045 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) Color[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays terrainGrid;
046
047 publicpublic is used to indicate unrestricted access (any other class can have access) Map()
048 {open braces start code blocks and must be matched with a close brace
049 thisthis means the current object (the implicit parameter).terrainGrid =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color[brackets are typically used to declare, initialize and index (indicate which element of) arrays NUM_ROWS ]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrays NUM_COLS ]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
050 Random r =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Random();
051 // Randomize the terrain
052 forfor is a looping structure for repeatedly executing a block of code ( intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i < NUM_ROWS; i++this is the increment operator, which increases the variable by 1 )
053 {open braces start code blocks and must be matched with a close brace
054 forfor is a looping structure for repeatedly executing a block of code ( intint is the type for whole numbers and it is short for integer j =this assignment operator makes the left side equal to the right side 0; j < NUM_COLS; j++this is the increment operator, which increases the variable by 1 )
055 {open braces start code blocks and must be matched with a close brace
056 intint is the type for whole numbers and it is short for integer randomTerrainIndex =this assignment operator makes the left side equal to the right side r.nextInt( TERRAIN.length );
057 Color randomColor =this assignment operator makes the left side equal to the right side TERRAIN[brackets are typically used to declare, initialize and index (indicate which element of) arrays randomTerrainIndex ]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
058 thisthis means the current object (the implicit parameter).terrainGrid[brackets are typically used to declare, initialize and index (indicate which element of) arrays i ]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrays j ]brackets are typically used to declare, initialize and index (indicate which element of) arrays =this assignment operator makes the left side equal to the right side randomColor;
059 }close braces end code blocks and must match an earlier open brace
060 }close braces end code blocks and must match an earlier open brace
061 intint is the type for whole numbers and it is short for integer preferredWidth =this assignment operator makes the left side equal to the right side NUM_COLS * PREFERRED_GRID_SIZE_PIXELS;
062 intint is the type for whole numbers and it is short for integer preferredHeight =this assignment operator makes the left side equal to the right side NUM_ROWS * PREFERRED_GRID_SIZE_PIXELS;
063 setPreferredSize( newnew is used to create objects by calling the constructor Dimension( preferredWidth, preferredHeight ) );
064 }close braces end code blocks and must match an earlier open brace
065
066 @Override
067 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value paintComponent( Graphics g )
068 {open braces start code blocks and must be matched with a close brace
069 // Important to call super class method
070 super.paintComponent( g );
071 // Clear the board
072 g.clearRect( 0, 0, getWidth(), getHeight() );
073 // Draw the grid
074 intint is the type for whole numbers and it is short for integer rectWidth =this assignment operator makes the left side equal to the right side getWidth() / NUM_COLS;
075 intint is the type for whole numbers and it is short for integer rectHeight =this assignment operator makes the left side equal to the right side getHeight() / NUM_ROWS;
076
077 forfor is a looping structure for repeatedly executing a block of code ( intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i < NUM_ROWS; i++this is the increment operator, which increases the variable by 1 )
078 {open braces start code blocks and must be matched with a close brace
079 forfor is a looping structure for repeatedly executing a block of code ( intint is the type for whole numbers and it is short for integer j =this assignment operator makes the left side equal to the right side 0; j < NUM_COLS; j++this is the increment operator, which increases the variable by 1 )
080 {open braces start code blocks and must be matched with a close brace
081 // Upper left corner of this terrain rect
082 intint is the type for whole numbers and it is short for integer x =this assignment operator makes the left side equal to the right side i * rectWidth;
083 intint is the type for whole numbers and it is short for integer y =this assignment operator makes the left side equal to the right side j * rectHeight;
084 Color terrainColor =this assignment operator makes the left side equal to the right side terrainGrid[brackets are typically used to declare, initialize and index (indicate which element of) arrays i ]brackets are typically used to declare, initialize and index (indicate which element of) arrays[brackets are typically used to declare, initialize and index (indicate which element of) arrays j ]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
085 g.setColor( terrainColor );
086 g.fillRect( x, y, rectWidth, rectHeight );
087 }close braces end code blocks and must match an earlier open brace
088 }close braces end code blocks and must match an earlier open brace
089 }close braces end code blocks and must match an earlier open brace
090
091 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.( String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args )
092 {open braces start code blocks and must be matched with a close brace
093 // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
094 SwingUtilities.invokeLater( newnew is used to create objects by calling the constructor Runnable()
095 {open braces start code blocks and must be matched with a close brace
096 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value run()
097 {open braces start code blocks and must be matched with a close brace
098 JFrame frame =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor JFrame( "Game" );
099 Map map =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Map();
100 frame.add( map );
101 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
102 frame.pack();
103 frame.setVisible( truetrue is the boolean value that is the opposite of false );
104 }close braces end code blocks and must match an earlier open brace
105 }close braces end code blocks and must match an earlier open brace
106 );
107 }close braces end code blocks and must match an earlier open brace
108 }close braces end code blocks and must match an earlier open brace
|