Week10 (Animation Appications and Printing)
Week10 (Animation Appications and Printing)
ANIMATION-PRINTING
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA
- A cellular automaton is a simple iterative system on a grid that evolves based
on a fixed set of rules.
- Many cellular automata generate surprisingly complex patterns
- A 2D cellular automaton is defined on a 2D grid
- Each cell has two states: black and white (also called live and dead)
- A cell has eight neighbors. In some systems, only four neighbors are
considered
- The iteration of the system proceeds by assigning the next state of each cell
based on the previous configuration
- Each cell follows the same set of rules, and the new state depends only upon
the current states of the same cell and its neighbors
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA- EXAMPLE
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA RULES
• The Game of Life is a 2D cellular automation. The rules
are quite simple:
1. (birth). A dead cell becomes live if it exactly three live
neighbors
2. (survival). A live cell remains live if it has two or three live
neighbors
3. (death). Otherwise, a cell dies
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA IMPLEMENTATION
• Each cell of the automata can be seen as a Boolean value with 0 or 1
• The whole automata is a 2D array of type bool
Declaring initializatio
1. import java.awt.*; n
Cell
shading
1. g2.setColor(Color.lightGray);
2. int p = 0; Backgroun 1. g2.setColor(Color.black);
3. int c = 16; d 2. for (int i = 0; i < n; i++) {
4. int len = c*n; grid 3. for (int j = 0; j < n; j++) {
5. for (int i = 0; i <= n; i++) { 4. if (cells1[i][j]) {
6. g2.drawLine(0, p, len, p); 5. int x = i*c;
7. g2.drawLine(p, 0, p, len); 6. int y = j*c;
8. p += c; 7. g2.fillOval(x, y, c, c);
9. } 8. }
9. }
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA UPDATING
• Cell content is updated by applying the 3 rules
Updating
12
• The PrinterJob class is the principal class that controls printing.
• An application calls methods in this class to set up a job, optionally to invoke a print dialog with the
user, and then to print the pages of the job.
• PrinterJob pj=PrinterJob.getPrinterJob();
• pj.setPrintable(painter);//painter is a variable of a class that extends jpanel and implements Printable
• At an action such as button press, we can call the print command
• if (pj.printDialog()) {
• try {
• pj.print();
● The method returns true if the user chooses to proceed with the printing
Void print( ) throws PrinterException;
● The actual graphics contents of printing are defined using the Printable
interface
void setPrintable(Printable painter);
• The last four retrieve the rectangle of the printable area of the page
16
PRINTGRAPHICS
public void actionPerformed(ActionEvent e) {
import java.awt.*; if (pj.printDialog()) {
import java.awt.geom.*; try {
import java.awt.event.*; pj.print();
import java.awt.print.*; } catch (PrinterException ex) {
import javax.swing.*; ex.printStackTrace();
}
public class PrintGraphics extends JFrame implements }
ActionListener { }
public static void main(String[] args) {
JFrame frame = new PrintGraphics(); public PrintGraphics() {
frame.setTitle("Printing"); Container cp = this.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cp.setLayout(new BorderLayout());
frame.pack(); JButton button = new JButton("Print");
frame.setVisible(true); cp.add(button, BorderLayout.SOUTH);
} button.addActionListener(this);
painter = new PrintPanel();
PrinterJob pj; cp.add(painter, BorderLayout.CENTER);
PrintPanel painter; pj = PrinterJob.getPrinterJob();
pj.setPrintable(painter); 17
}
}
PRINTGRAPHICS
class PrintPanel extends JPanel implements Printable {
public PrintPanel() { public void paintComponent(Graphics g) {
setPreferredSize(new Dimension(800, 400)); super.paintComponent(g);
setBackground(Color.white); draw(g);
} }
public int print(Graphics g, PageFormat pf, int pageIndex) { private void draw(Graphics g) {
switch (pageIndex) { g.setFont(new Font("Serif", Font.BOLD, 144));
case 0: g.drawString("Welcome!", 200, 300);
draw(g); }
break; }
case 1:
g.translate(-(int)pf.getImageableWidth(), 0);
draw(g);
break;
default:
return NO_SUCH_PAGE;
}
return PAGE_EXISTS;
}
18
PRINTGRAPHICS
19
PRINTGRAPHICS
20