14
תתתתתתת תתתתת תתתתת תתתתת תתתתת6 - GUI

עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI: Swing Basic components Event handling Containers Layouts

Embed Size (px)

Citation preview

Page 1: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

עקרונות תכנות מונחה עצמים

GUI - 6תרגול

Page 2: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

:GUI סיכום ביניים

Swing

Basic components

Event handling

Containers

Layouts

Page 3: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Outline

Game of Life

Painting

Page 4: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Case Study

Game of Life

Page 5: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Conway’s Game of Life

Page 6: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Basic Rules

Each cell in the grid is either live or dead

The user determines the initial positions of all live cells.

The game proceeds in rounds. In each round some new organisms born, and others die, according to predefined evolution rules.

Page 7: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Neighbors

The neighbors of a cell are the cells that surround it.

Page 8: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Neighbors

The neighbors of a cell are the cells that surround it.

Page 9: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Evolution Rules

Any live cell with fewer than two live neighbors dies, as if caused by under-population.

Any live cell with two or three live neighbors lives on to the next generation.

Any live cell with more than three live neighbors dies as if by overcrowding.

Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction

Page 10: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Program clasess

Game – Main class that creates the graphical user interface.

Board – A class that represents the game board.

Holds an array of integers and an array of buttons

ButtonPressed – An ActionListener for setting the initial position

MODEL VIEW

Page 11: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Game Class

Page 12: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

Board Class

Page 13: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

public void next ( ) { int [ ] [ ] lastState = new int [N+2] [N+2] ; for(int i = 1 ; i <= N ; i++){ // copy current state to new array.

for ( int j =1 ; j <= N ; j++){ lastState [i][j] = array[i-1][j-1]; } } // top and buttom row (i = 0,N+1), left and right culomn(j= 0,N+1) initiate to 0

for ( int i = 0 ; i < N ; i++){ // update current state.for(int j < 1; j <= N ; j++ ){ int count = 0;

count = countNeighbors(lastState,i+1,j+1); if(count < 2 || count > 3 ){ //check for under-population or overcrowding

killCell(i,j); } if(count == 3) { // check for reproduction reviveCell(i,j); } } }}

private int countNeighbors(int[][] lastState,int x, int y){ int count = lastState[x-1][y-1] + lastState[x-1][y]+lastState[x-1][y+1] // all 3 neighbors left to current cell + lastState[x][y-1] + lastState[x][y+1] // the cell above current and the cell below + lastState[x+1][y-1] + lastState[x+1][y]+lastState[x+1][y+1] ; // all 3 neighbors left to current cell return count;}

private void killCell( int i , int j ){ array[i][j] = 0; buttons[i][j].setIcon( null );}

private void reviveCell( int i , int j ){ array[i][j] = 1; buttons[i][j].setIcon(new ImageIcon( “button.gif” ));}}//end of Board class

Page 14: עקרונות תכנות מונחה עצמים תרגול 6 - GUI. סיכום ביניים GUI:  Swing  Basic components  Event handling  Containers  Layouts

ButtonPressed Class