-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLandscapeDisplay.java
More file actions
139 lines (122 loc) · 4.18 KB
/
LandscapeDisplay.java
File metadata and controls
139 lines (122 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* File: Cell.java
* Author: Jon Lee
* Date: 09/29/2018
* Class: CS231
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LandscapeDisplay extends JFrame
{
protected Landscape scape;
private LandscapePanel canvas;
private int gridScale; // width (and height) of each square in the grid
/**
* Initializes a display window for a Landscape.
* @param scape the Landscape to display
* @param scale controls the relative size of the display
*/
public LandscapeDisplay(Landscape scape, int scale)
{
// setup the window
super("Game of Life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.scape = scape;
this.gridScale = scale;
// create a panel in which to display the Landscape
this.canvas = new LandscapePanel( (int) this.scape.getCols() * this.gridScale,
(int) this.scape.getRows() * this.gridScale);
// add the panel to the window, layout, and display
this.add(this.canvas, BorderLayout.CENTER);
this.pack();
this.setVisible(true);
}
/**
* Saves an image of the display contents to a file. The supplied
* filename should have an extension supported by javax.imageio, e.g.
* "png" or "jpg".
*
* @param filename the name of the file to save
*/
public void saveImage(String filename)
{
// get the file extension from the filename
String ext = filename.substring(filename.lastIndexOf('.') + 1, filename.length());
// create an image buffer to save this component
Component tosave = this.getRootPane();
BufferedImage image = new BufferedImage(tosave.getWidth(), tosave.getHeight(),
BufferedImage.TYPE_INT_RGB);
// paint the component to the image buffer
Graphics g = image.createGraphics();
tosave.paint(g);
g.dispose();
// save the image
try
{
ImageIO.write(image, ext, new File(filename));
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
/**
* This inner class provides the panel on which Landscape elements
* are drawn.
*/
private class LandscapePanel extends JPanel
{
/**
* Creates the panel.
* @param width the width of the panel in pixels
* @param height the height of the panel in pixels
*/
public LandscapePanel(int width, int height)
{
super();
this.setPreferredSize(new Dimension(width, height));
this.setBackground(Color.white);
}
/**
* Method overridden from JComponent that is responsible for
* drawing components on the screen. The supplied Graphics
* object is used to draw.
*
* @param g the Graphics object used for drawing
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
scape.draw( g, gridScale );
} // end paintComponent
} // end LandscapePanel
public void update() {
Graphics g = canvas.getGraphics();
this.requestFocus();
canvas.paintComponent( g );
}
public static void main(String[] args) throws InterruptedException {
Landscape scape = new Landscape(50,50);
Random gen = new Random();
double density = 0.3;
for (int i = 0; i < scape.getRows(); i++) {
for (int j = 0; j < scape.getCols(); j++ ) {
scape.getCell( i, j ).setAlive( gen.nextDouble() <= density );
}
}
LandscapeDisplay display = new LandscapeDisplay(scape, 10);
display.repaint();
}
}