159.235 Graphics & Graphical

designpadΤεχνίτη Νοημοσύνη και Ρομποτική

1 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

89 εμφανίσεις

159.235

Graphics

1

159.235 Graphics & Graphical
Programming

Lecture 10
-

Data Models &
Simulations


159.235

Graphics

2

Data Models & Simulations
-

Outline


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

159.235

Graphics

3

1
-
D cellular Automata Example


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

159.235

Graphics

4

The CA Rule


Rule can be encodes as a number:


159.235

Graphics

5

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”

159.235

Graphics

6

How do we Render our CA?


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)


159.235

Graphics

7

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] ];



}


}




}


159.235

Graphics

8


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();


}




159.235

Graphics

9


// 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

159.235

Graphics

10

Output of MyProg06



159.235

Graphics

11

Summary


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


1.
Input some parameters

2.
Calculate or simulate

3.
Render



Our CA example produced
an image
-

we will look at
apparatus for manipulating
images in more depth later.