0% found this document useful (0 votes)
3 views

Week10 (Animation Appications and Printing)

Uploaded by

cheery.bowl7899
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Week10 (Animation Appications and Printing)

Uploaded by

cheery.bowl7899
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

ADVANCED TOPICS-

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

2. boolean[][] cells1; for (int i = 0; i < n; i++) {


3. cells1 = new boolean[n][n]; for (int j = 0; j < n; j++) {
cells1[i][j] = Math.random() < 0.1;
cells2[i][j] = false;
}
}
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA IMPLEMENTATION
• The background is a grid of lines that can be implemented as follows

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

1. int nb = neighbors(cells, i, j);


2. if (nb == 3)
3. cells2[i][j] = true;
4. if (nb < 2 || nb > 3)
5. cells2[i][j] = false;
ANIMATION WITH TIMER EXAMPLE
1.
CELLULAR AUTOMATA FULL CODE
import java.awt.*;
2. import java.awt.event.*; class LifePanel extends JPanel implements
3. import javax.swing.*; ActionListener{
4. import java.awt.geom.*; int n = 30;
boolean[][] cells1;
5. public class Life extends JApplet { boolean[][] cells2;
6. public static void main(String s[]) {
7. JFrame frame = new JFrame(); public LifePanel() {
setPreferredSize(new Dimension(400, 400));
8. frame.setDefaultCloseOperation(JF setBackground(Color.white);
rame.EXIT_ON_CLOSE); cells1 = new boolean[n][n];
9. JApplet applet = new Life(); cells2 = new boolean[n][n];
10. applet.init(); for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
11. cells1[i][j] = Math.random() < 0.1;
frame.getContentPane().add(apple cells2[i][j] = false;
t); } }
12. frame.pack(); Timer timer = new Timer(1000, this);
timer.start();
13. frame.setVisible(true);}
}
14. public void init() {
ANIMATION WITH TIMER EXAMPLE
1. public CELLULAR
void AUTOMATA FULL CODE CONT.
paintComponent(Graphics g) { 1. public void
actionPerformed(ActionEvent e) {
2. super.paintComponent(g);
2. boolean[][] cells = cells1;
3. Graphics2D g2 = (Graphics2D)g;
3. for (int i = 0; i < n; i++) {
4. g2.setColor(Color.lightGray);
4. for (int j = 0; j < n; j++) {
5. int p = 0;
5. cells2[i][j] = cells[i][j];
6. int c = 16;
6. int nb = neighbors(cells, i, j);
7. int len = c*n;
7. if (nb == 3)
8. for (int i = 0; i <= n; i++) {
8. cells2[i][j] = true;
9. g2.drawLine(0, p, len, p);
9. if (nb < 2 || nb > 3)
10. g2.drawLine(p, 0, p, len);
10. cells2[i][j] = false;
11. p += c; }
11. }
12. g2.setColor(Color.black);
12. }
13. for (int i = 0; i < n; i++) {
13.
14. for (int j = 0; j < n; j++) {
14. cells1 = cells2;
15. if (cells1[i][j]) {
15. cells2 = cells;
16. int x = i*c;
16. repaint();
17. int y = j*c;
17. }
18. g2.fillOval(x, y, c, c);
18.
19. } } } }
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA FULL CODE CONT.
1. private int neighbors(boolean[][]
cells, int x, int y) {
2. int x1 = (x>0)?x-1:x;
3. int x2 = (x<n-1)?x+1:x;
4. int y1 = (y>0)?y-1:y;
5. int y2 = (y<n-1)?y+1:y;
6. int count = 0;
7. for (int i = x1; i <= x2; i++) {
8. for (int j = y1; j <= y2; j++) {
9. count += (cells[i][j])?1:0;
10. }
11. }
12. if (cells[x][y]) count--;
13. return count;
14. }
15. }
ANIMATION WITH TIMER EXAMPLE
CELLULAR AUTOMATA –SAMPLE OUTPUT.
PRINTING

● Printing is a common task in computer applications.


● The rendering problems involved in printing graphics
are essentially the same as those in drawing on the
screen
● Java API provides convenient facilities for printing
● The PrinterJob class can be used to manage a printing
process static PrinterJob getPrinterJob( );

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

• } catch (PrinterException ex) {}}


PJ.PRINT
public int print(Graphics g, PageFormat pf, int
pageIndex) {
• Prints the page at the specified index into the switch (pageIndex) {
specified Graphics context in the specified
format. 1. case 0: draw(g); break;
2. case 1: g.translate(-(int)pf.getImageableWidth(),
• The format of the page to be drawn is specified
0);
by pageFormat.
3. draw(g); break; default:
• The zero based index of the requested page is
specified by pageIndex. 4. return NO_SUCH_PAGE;
}
• If the requested page does not exist then this
method returns NO_SUCH_PAGE; otherwise 1. return PAGE_EXISTS;
PAGE_EXISTS is returned. }
PRINTING
● At the beginning of a print job the user is usually presented with a
printer selection dialog box
Boolean printDialog( );

● 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 printable interface provides callback structure to define the custom


drawing of the printing job (similar to paintComponent)
int print(Graphics g, PageFormat pf, int pageIndex);
15
PRINTING
• PageFormat object contains information about the printing page
settings
• getOrientation
• getWidth
• getHeight
• getImageableX
• getImageableY
• getImageableWidth
• getImageableHeight

• 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

You might also like