120 likes | 277 Views
159.235 Graphics & Graphical Programming. Lecture 10 - Data Models & Simulations. Often want a graphics program to interact with some simulation program or game code Need to understand how to write integrated simulation/rendering codes Java is really well suited for this.
E N D
159.235 Graphics & Graphical Programming Lecture 10 - Data Models & Simulations Graphics
Often want a graphics program to interact with some simulation program or game code Need to understand how to write integrated simulation/rendering codes Java is really well suited for this Java Programming Cellular Automata Example Data Models & Simulations - Outline Graphics
This is a model that we want to render The Automata world is a 1-dimensional set of cells Each cell can be zero or one (empty of filled) The cells evolve in time according to a function or rule that takes as inputs a cell’s Current state and the states of its two neighbouring cells 1-D cellular Automata Example Graphics
The CA Rule • Rule can be encodes as a number: Graphics
Implementing the CA Rule • Use of a lookup table • Three inputs centre (i) and left and right cells • Two possible values 0, 1 • So Lookup table has 8 entries (2^3) • The bits of the rule can be encoded as a number we are looking at “Rule 30” Graphics
MyProg06 is the usual Java wrappers Developed to resize itself to what we ask Decide to render as red and white rectangles Red for 1, White for 0 Space (just 1 dimension will be horizontal) Time will be “down the display” So we can render a space-time diagram as a 2-d picture (image) How do we Render our CA? Graphics
import javax.swing.*; // the Java eXtensions "Swing" graphics kit import java.awt.*; // the Java Abstract Windowing Toolkit import java.awt.geom.*; // AWT geometry // a test class to experiment with simple graphics in a JFrame public class MyProg06 extends JFrame{ private static final int DISPLAY_WIDTH = 512; // constants denoting the private static final int DISPLAY_HEIGHT = 512; // size we want for DisplayArea private static final int CA_WIDTH = 64; // or 512 private static final int CA_HEIGHT = 64; private int cells[][]; // 1-D Cellular Automata cells[row][col] // col (x) is space, row (y) is time private static int lookUpTable[][][] = new int[2][2][2]; public void initialiseCA(){ lookUpTable[0][0][0] = 0; // encodes the Cellular Automaton Rule lookUpTable[0][0][1] = 1; lookUpTable[0][1][0] = 1; lookUpTable[0][1][1] = 1; lookUpTable[1][0][0] = 1; // eg Rule 30 is 00011110 lookUpTable[1][0][1] = 0; lookUpTable[1][1][0] = 0; lookUpTable[1][1][1] = 0; cells = new int[CA_WIDTH][CA_HEIGHT]; for(int row=0;row<1;row++){ for(int col=0;col<CA_WIDTH;col++){ cells[row][col] = 0; // first row is all zero } } cells[0][CA_WIDTH/2] = 1; // except the centre cell with is set for(int row=1;row<CA_HEIGHT;row++){ for(int col=0;col<CA_WIDTH;col++){ int plus = col < CA_WIDTH-1 ? col+1 : 0; // periodic boundary conditions int minus = col > 0 ? col-1 : CA_WIDTH-1; cells[row][col] = lookUpTable[ cells[row-1][plus] ][ cells[row-1][col] ][ cells[row-1][minus] ]; } } } Graphics
public static void main( String args[] ){ // bootstraps our MyProg Object new MyProg06(); } public MyProg06(){ // the default (no arguments) constructor super("MyProg in a JFrame"); // calls the JFrame's constructor setDefaultCloseOperation(EXIT_ON_CLOSE); // allow us to close with mouse click // setResizable(false); // disallow user to resize at run time with mouse getContentPane().setLayout(null); // disengage default layout manager // create a DisplayArea object and add to the MyProg object getContentPane().add( new DisplayArea( new Rectangle( 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT) ) ); initialiseCA(); // runs our data model - ie a CA setVisible(true); // bring up the JFrame resizeToInternalSize( DISPLAY_WIDTH, DISPLAY_HEIGHT ); // the size we actually want // this is only useful once the JFrame etc is actually visible - it will read // system dependent run-time information, depending on Windows systems we run on } // cunning trickery to get the JFrame to give us the size we want not including borders public void resizeToInternalSize( int internalWidth, int internalHeight ){ Insets insets = getInsets(); final int newWidth = internalWidth + insets.left + insets.right; final int newHeight = internalHeight + insets.top + insets.bottom; Runnable resize = new Runnable(){ // an anonymous inner class public void run(){ setSize( newWidth, newHeight); } }; if(!SwingUtilities.isEventDispatchThread() ){ try{ SwingUtilities.invokeAndWait( resize ); }catch( Exception e ) { } } else{ resize.run(); } validate(); } Graphics
// an inner class to hold our graphical code public class DisplayArea extends JPanel{ // extends Java Swing JPanel public DisplayArea( Rectangle bounds ){ // default constructor setLayout(null); setBounds( bounds); setOpaque(false); } public void paintComponent( Graphics g ){ Graphics2D g2 = (Graphics2D)g; // we will use a Graphics2D capabilities // render our CA space-time diagram as red(1) and white(0) squares g2.setColor( Color.white ); g2.fillRect( 0, 0, getWidth()-1, getHeight()-1 ); g2.setColor( Color.red ); int cellWidth = DISPLAY_WIDTH / CA_WIDTH ; int cellHeight = DISPLAY_HEIGHT / CA_HEIGHT; for(int row=0;row<CA_HEIGHT;row++){ for(int col=0;col<CA_WIDTH;col++){ int x1 = col * cellWidth; int y1 = row * cellHeight; if( cells[row][col] == 1 )g2.fillRect( x1, y1, cellWidth, cellHeight ); } } } } } Prog06 builds on 1-5 Has new resize method Internal data structure for CA cells CA Initialise method Rendering is in paintComponent() method as usual We can adjust the size of display we want Graphics
Output of MyProg06 Graphics
We can use a programming language like Java to integrate graphics rendering with simulation or game code The Cellular Automaton is a very simple “model” - although it has been quite extensively studied This programming “pattern” is quite common and useful Input some parameters Calculate or simulate Render Our CA example produced an image - we will look at apparatus for manipulating images in more depth later. Summary Graphics