java-gnucleon: 5ff6aa68fed3361868eaa8a8421a06eff18a6b48

     1: import com.jgoodies.forms.layout.CellConstraints;
     2: import com.jgoodies.forms.layout.FormLayout;
     3: import java.awt.*;
     4: import java.awt.BorderLayout;
     5: import java.awt.Container;
     6: import java.awt.Dimension;
     7: import java.awt.event.WindowAdapter;
     8: import java.awt.event.WindowEvent;
     9: import java.awt.event.ActionEvent;
    10: import java.awt.event.ActionListener;
    11: import javax.swing.*;
    12: import javax.swing.Box;
    13: import javax.swing.ImageIcon;
    14: import javax.swing.JButton;
    15: import javax.swing.JFrame;
    16: import javax.swing.JPanel;
    17: 
    18: public class Gnucleon extends JPanel
    19: {
    20: 
    21:    /**
    22:     * Default constructor
    23:     */
    24:    public Gnucleon()
    25:    {
    26:    	//jpanel1 = new JPanel();
    27:    	//cc = new CellConstraints();
    28:    	/*widthLabel = new JLabel("Width:");
    29:    	heightLabel = new JLabel("Height:");
    30:    	widthEntry = new JFormattedTextField();
    31:    	heightEntry = new JFormattedTextField();
    32:    	playersLabel = new JLabel("Players:");
    33:    	playerEntry = new JFormattedTextField();
    34:    	startButton = new JButton("Start");
    35:    	startButton.addActionListener(new ActionListener() {
    36: 			public void actionPerformed(ActionEvent ae) {
    37: 				getValues();
    38: 				setupBoard();
    39: 			}
    40: 		});*/
    41:    	width = 12;
    42:    	height = 10;
    43:    	players = 2;
    44:    	setupBoard();
    45:    	currentPlayer = 1;
    46:       initializePanel();
    47:    }
    48: 
    49: 	/*private void getValues() {
    50: 		width = Integer.parseInt(widthEntry.getText());
    51: 		height = Integer.parseInt(heightEntry.getText());
    52: 		players = Integer.parseInt(playerEntry.getText());
    53: 	}*/
    54: 
    55: 	private void setupBoard() {
    56: 		values = new int[width][height];
    57: 		limits = new int[width][height];
    58: 		owners = new int[width][height];
    59: 		buttons = new JButton[width][height];
    60: 		createPanel();
    61: 		for (int i=0; i<width; i++) {
    62: 			for (int j=0; j<height; j++) {
    63: 				values[i][j] = 0;
    64: 				owners[i][j] = 0;
    65: 				limits[i][j] = 4;
    66: 				//makeButton(i, j);
    67: 				//jpanel1.add(buttons[i][j], cc.xy(i+2,j+2));
    68: 			}
    69: 			limits[i][0] = 3;
    70: 			limits[i][height-1] = 3;
    71: 		}
    72: 		//jpanel1.add(playersLabel, cc.xy(2, height+2));
    73: 		//jpanel1.add(playerEntry, cc.xy(3, height+2));
    74: 		//jpanel1.add(widthLabel, cc.xy(4, height+2));
    75: 		//jpanel1.add(widthEntry, cc.xy(5, height+2));
    76: 		//jpanel1.add(heightLabel, cc.xy(6, height+2));
    77: 		//jpanel1.add(heightEntry, cc.xy(7, height+2));
    78: 		//jpanel1.add(startButton, cc.xy(8, height+2));
    79: 		for (int i=0; i<height; i++) {
    80: 			limits[0][i] = 3;
    81: 			limits[width-1][i] = 3;
    82: 		}
    83: 		limits[0][0] = 2;
    84: 		limits[0][height-1] = 2;
    85: 		limits[width-1][0] = 2;
    86: 		limits[width-1][height-1] = 2;
    87: 	}
    88: 
    89:    /**
    90:     * Main method for panel
    91:     */
    92:    public static void main(String[] args)
    93:    {
    94:    	  switch (args.length)
    95:    	  {
    96:    	  	case 0: playerNumber = 2;
    97:    	  	break;
    98:    	  	case 1: playerNumber = args[0];
    99:    	  	break;
   100:    	  	default:
   101:    	  	playerNumber = 2;
   102:    	  }
   103: 
   104: 
   105:       JFrame frame = new JFrame();
   106:       frame.setSize(535, 290);
   107:       frame.setLocation(100, 100);
   108:       frame.getContentPane().add(new Gnucleon());
   109:       frame.setVisible(true);
   110: 
   111:       frame.addWindowListener( new WindowAdapter()
   112:       {
   113:          public void windowClosing( WindowEvent evt )
   114:          {
   115:             System.exit(0);
   116:          }
   117:       });
   118:    }
   119: 
   120:    /**
   121:     * Adds fill components to empty cells in the first row and first column of the grid.
   122:     * This ensures that the grid spacing will be the same as shown in the designer.
   123:     * @param cols an array of column indices in the first row where fill components should be added.
   124:     * @param rows an array of row indices in the first column where fill components should be added.
   125:     */
   126:    void addFillComponents( Container panel, int[] cols, int[] rows )
   127:    {
   128:       Dimension filler = new Dimension(10,10);
   129: 
   130:       boolean filled_cell_11 = false;
   131:       //CellConstraints cc = new CellConstraints();
   132:       if ( cols.length > 0 && rows.length > 0 )
   133:       {
   134:          if ( cols[0] == 1 && rows[0] == 1 )
   135:          {
   136:             /** add a rigid area  */
   137:             panel.add( Box.createRigidArea( filler ), cc.xy(1,1) );
   138:             filled_cell_11 = true;
   139:          }
   140:       }
   141: 
   142:       for( int index = 0; index < cols.length; index++ )
   143:       {
   144:          if ( cols[index] == 1 && filled_cell_11 )
   145:          {
   146:             continue;
   147:          }
   148:          panel.add( Box.createRigidArea( filler ), cc.xy(cols[index],1) );
   149:       }
   150: 
   151:       for( int index = 0; index < rows.length; index++ )
   152:       {
   153:          if ( rows[index] == 1 && filled_cell_11 )
   154:          {
   155:             continue;
   156:          }
   157:          panel.add( Box.createRigidArea( filler ), cc.xy(1,rows[index]) );
   158:       }
   159: 
   160:    }
   161: 
   162:    /**
   163:     * Helper method to load an image file from the CLASSPATH
   164:     * @param imageName the package and name of the file to load relative to the CLASSPATH
   165:     * @return an ImageIcon instance with the specified image file
   166:     * @throws IllegalArgumentException if the image resource cannot be loaded.
   167:     */
   168:    public ImageIcon loadImage( String imageName )
   169:    {
   170:       try
   171:       {
   172:          ClassLoader classloader = getClass().getClassLoader();
   173:          java.net.URL url = classloader.getResource( imageName );
   174:          if ( url != null )
   175:          {
   176:             ImageIcon icon = new ImageIcon( url );
   177:             return icon;
   178:          }
   179:       }
   180:       catch( Exception e )
   181:       {
   182:          e.printStackTrace();
   183:       }
   184:       throw new IllegalArgumentException( "Unable to load image: " + imageName );
   185:    }
   186: 
   187:    public JPanel createPanel()
   188:    {
   189:       JPanel jpanel1 = new JPanel();
   190:       FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE","CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
   191:       cc = new CellConstraints();
   192:       jpanel1.setLayout(formlayout1);
   193: 
   194: 	buttons = new JButton[12][10];
   195: 	for (i=0; i<12; i++) {
   196: 		for (j=0; j<10; j++) {
   197: 			makeButton(i, j);
   198: 			jpanel1.add(buttons[i][j], cc.xy(i+2,j+2));
   199: 		}
   200: 	}
   201: 	//jpanel1.add(playersLabel, cc.xy(2, height+2));
   202: 	//jpanel1.add(playerEntry, cc.xy(3, height+2));
   203: 	//jpanel1.add(widthLabel, cc.xy(4, height+2));
   204: 	//jpanel1.add(widthEntry, cc.xy(5, height+2));
   205: 	//jpanel1.add(heightLabel, cc.xy(6, height+2));
   206: 	//jpanel1.add(heightEntry, cc.xy(7, height+2));
   207: 	//jpanel1.add(startButton, cc.xy(8, height+2));
   208: 
   209:       addFillComponents(jpanel1,new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14 },new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12 });
   210:       return jpanel1;
   211:    }
   212: 
   213:    /**
   214:     * Initializer
   215:     */
   216:    protected void initializePanel()
   217:    {
   218:       setLayout(new BorderLayout());
   219:       add(createPanel(), BorderLayout.CENTER);
   220:    }
   221: 
   222: 	private void play(int x, int y, boolean pwn) {
   223: 		if ((!pwn && owners[x][y] == 0) || (!pwn && owners[x][y] == currentPlayer) || pwn) {
   224: 			owners[x][y] = currentPlayer;
   225: 			values[x][y]++;
   226: 			buttons[x][y].setText("" + values[x][y]);
   227: 			if (values[x][y] >= limits[x][y]) {
   228: 				System.out.println("Bang");
   229: 				explode(x, y);
   230: 				if (values[x][y] == 0) {
   231: 					owners[x][y] = 0;
   232: 					buttons[x][y].setBackground(Color.white);
   233: 				}
   234: 			}
   235: 			buttons[x][y].setText("" + values[x][y]);
   236: 			if (values[x][y] != 0) {
   237: 				switch (currentPlayer) {
   238: 					case 1:
   239: 						buttons[x][y].setBackground(Color.blue);
   240: 						break;
   241: 					case 2:
   242: 						buttons[x][y].setBackground(Color.green);
   243: 						break;
   244: 					case 3:
   245: 						buttons[x][y].setBackground(Color.red);
   246: 						break;
   247: 					case 4:
   248: 						buttons[x][y].setBackground(Color.magenta);
   249: 						break;
   250: 					case 5:
   251: 						buttons[x][y].setBackground(Color.yellow);
   252: 						break;
   253: 					case 6:
   254: 						buttons[x][y].setBackground(Color.cyan);
   255: 						break;
   256: 					case 7:
   257: 						buttons[x][y].setBackground(Color.orange);
   258: 						break;
   259: 					case 8:
   260: 						buttons[x][y].setBackground(Color.gray);
   261: 						break;
   262: 					case 9:
   263: 						buttons[x][y].setBackground(Color.pink);
   264: 						break;
   265: 				}
   266: 			}
   267: 			if (!pwn) {
   268: 				if (currentPlayer >= players) {
   269: 					currentPlayer = 1;
   270: 				}
   271: 				else {
   272: 					currentPlayer++;
   273: 				}
   274: 			}
   275: 		}
   276: 
   277: 	}
   278: 
   279: 	private void explode(int x, int y) {
   280: 		boolean up = true;
   281: 		boolean down = true;
   282: 		boolean left = true;
   283: 		boolean right = true;
   284: 		while (values[x][y] >= limits[x][y]) {
   285: 			values[x][y] = values[x][y] - limits[x][y];
   286: 			if (x == 0) {
   287: 				left = false;
   288: 			}
   289: 			if (x == 11) {
   290: 				right = false;
   291: 			}
   292: 			if (y == 0) {
   293: 				up = false;
   294: 			}
   295: 			if (y == 9) {
   296: 				down = false;
   297: 			}
   298: 			if (up) {
   299: 				play(x, y-1, true);
   300: 			}
   301: 			if (down) {
   302: 				play(x, y+1, true);
   303: 			}
   304: 			if (left) {
   305: 				play(x-1, y, true);
   306: 			}
   307: 			if (right) {
   308: 				play(x+1, y, true);
   309: 			}
   310: 		}
   311: 	}
   312: 
   313: 	private int[][] limits;
   314: 	private int[][] values;
   315: 	private int[][] owners;
   316: 	private int givenPlayers;
   317: 	private int width;
   318: 	private int height;
   319: 	private int players;
   320: 	private int currentPlayer;
   321: 
   322: 	private JButton[][] buttons;
   323: 	private JButton startButton;
   324: 	private JFormattedTextField playerEntry;
   325: 	private JFormattedTextField widthEntry;
   326: 	private JFormattedTextField heightEntry;
   327: 	private JLabel playersLabel;
   328: 	private JLabel widthLabel;
   329: 	private JLabel heightLabel;
   330: 	private int i;
   331: 	private int j;
   332: 	private CellConstraints cc;
   333: 	//private JPanel jpanel1;
   334: 
   335: 	private void makeButton(final int i, final int j) {
   336: 		buttons[i][j] = new JButton();
   337: 		buttons[i][j].setActionCommand("JButton");
   338: 		buttons[i][j].setText("0");
   339: 		buttons[i][j].setBackground(Color.white);
   340: 		buttons[i][j].addActionListener(new ActionListener() {
   341: 			public void actionPerformed(ActionEvent ae) {
   342: 				play(i, j, false);
   343: 			}
   344: 		});
   345: 	}
   346: 
   347: }

Generated by git2html.