Map

From JavaWIDE Sandbox

Jump to: navigation, search

001 //start auto-imports
002 //end auto-imports
003 //start auto-imports
004 import java.util.*;
005 //end auto-imports
006 import javax.swing.*;
007 import java.awt.*;
008 import java.util.Random;
009 
010 public class Map extends JPanel
011 {
012 
013   public static final Color CITY = new Color214217223 );
014   public static final Color DESERT = new Color255204102 );
015   public static final Color DIRT_ROAD = new Color153102);
016   public static final Color FOREST = new Color0102);
017   public static final Color HILLS = new Color51153);
018   public static final Color LAKE = new Color0153153 );
019   public static final Color MOUNTAINS = new Color102102255 );
020   public static final Color OCEAN = new Color00153 );
021   public static final Color PAVED_ROAD = new Color5151);
022   public static final Color PLAINS = new Color102153);
023   
024   public static final Color[] TERRAIN = {
025                                             CITY,
026                                             DESERT,
027                                             DIRT_ROAD,
028                                             FOREST,
029                                             HILLS,
030                                             LAKE,
031                                             MOUNTAINS,
032                                             OCEAN,
033                                             PAVED_ROAD,
034                                             PLAINS
035                                         };
036                                         
037   public static final int NUM_ROWS = 75;
038   public static final int NUM_COLS = 90;
039   
040   public static final int PREFERRED_GRID_SIZE_PIXELS = 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   private final Color[][] terrainGrid;
046   
047   public Map()
048   {
049     this.terrainGrid = new Color[ NUM_ROWS ][ NUM_COLS ];
050     Random r = new Random();
051     // Randomize the terrain
052     for int = 0; i < NUM_ROWS; i++ )
053     {
054       for int = 0; j < NUM_COLS; j++ )
055       {
056         int randomTerrainIndex = r.nextIntTERRAIN.length );
057         Color randomColor = TERRAIN[ randomTerrainIndex ];
058         this.terrainGrid[ ][ ] = randomColor;
059       }
060     }
061     int preferredWidth = NUM_COLS * PREFERRED_GRID_SIZE_PIXELS;
062     int preferredHeight = NUM_ROWS * PREFERRED_GRID_SIZE_PIXELS;
063     setPreferredSizenew DimensionpreferredWidth, preferredHeight ) );
064   }
065   
066   @Override
067   public void paintComponentGraphics g )
068   {
069     // Important to call super class method
070     super.paintComponent);
071     // Clear the board
072     g.clearRect00, getWidth(), getHeight() );
073     // Draw the grid
074     int rectWidth = getWidth() / NUM_COLS;
075     int rectHeight = getHeight() / NUM_ROWS;
076     
077     for int = 0; i < NUM_ROWS; i++ )
078     {
079       for int = 0; j < NUM_COLS; j++ )
080       {
081         // Upper left corner of this terrain rect
082         int = i * rectWidth;
083         int = j * rectHeight;
084         Color terrainColor = terrainGrid[ ][ ];
085         g.setColorterrainColor );
086         g.fillRectx, y, rectWidth, rectHeight );
087       }
088     }
089   }
090   
091   public static void mainString[] args )
092   {
093     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
094     SwingUtilities.invokeLaternew Runnable()
095                                 {
096                                   public void run()
097                                   {
098                                     JFrame frame = new JFrame"Game" );
099                                     Map map = new Map();
100                                     frame.addmap );
101                                     frame.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE );
102                                     frame.pack();
103                                     frame.setVisibletrue );
104                                   }
105                                 }
106                               );
107   }
108 }


Download/View Map.java






Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter