-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPixelife.java
153 lines (135 loc) · 3.01 KB
/
Pixelife.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Pixelife class
* Manages GUI and pixel painting
* @author Nic Manoogian <[email protected]>
* @author Mike Lyons <[email protected]>
*/
public class Pixelife extends JPanel
{
/*
* Buffer that is painted to the screen
*/
private BufferedImage canvas;
/*
* Grid containing Pix objects
*/
private static PixGrid myGrid;
/*
* Creates Pix objects of a certain type
*/
private Spawner spawner;
/*
* Screen dimentions
*/
private int width;
private int height;
/**
* Constructor with width and height
* @param w width in pixels
* @param h height in pixels
* @param n number of random pixels to generate
*/
public Pixelife(int w, int h, int n)
{
canvas = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
width = w;
height = h;
myGrid = new PixGrid(w, h, n);
// Spawner vline_spawner = new Spawner(DirectedPix.class, myGrid, 2, h/2);
// vline_spawner.spawn(5);
// Spawner hline_spawner = new Spawner(DirectedPix.class, myGrid, 0, h/2);
// hline_spawner.spawn(5);
// Spawner lines = new Spawner(ScaredPix.class, myGrid);
// lines.spawn(10);
// spawner = new Spawner(SpiralPix.class, myGrid);
// spawner.spawn(1);
spawner = new Spawner(AvgPix.class, myGrid);
spawner.spawn(5);
spawner = new Spawner(NonconformingPix.class, myGrid);
spawner.spawn(5);
// spawner.spawnXY(SpiralPix.class, w/2, h/2);
// spawner.spawnXY(RainbowPix.class, w/2, h/2+1);
}
/**
* Required by 2DGraphics
*/
public Dimension getPreferredSize()
{
return new Dimension(canvas.getWidth(), canvas.getHeight());
}
/**
* Required by 2DGraphics
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(canvas, null, null);
}
/**
* Update and paint loop
*/
public void run()
{
while(true)
{
myGrid.update();
// line_spawner.update();
// spawner.update();
draw();
// Limit framerate
// try
// {
// Thread.sleep(0);
// }
// catch(InterruptedException e)
// {
// System.out.println("interrupted");
// }
repaint();
}
}
/**
* Draws Pix grid to the canvas object
*/
public void draw()
{
Pix[][] grid = myGrid.getGrid();
for( int i = 0; i < grid.length; i++ )
{
for( int j = 0; j < grid[0].length; j ++ )
{
canvas.setRGB(i, j, grid[i][j].getRGB());
}
}
}
public static void main(String [] args)
{
//Set window size
int width = 640;
int height = 480;
//Make new frame
JFrame frame = new JFrame("main");
//Make new Pixelife object
Pixelife plife = new Pixelife(width, height, 15);
//Add canvas to screen
frame.add(plife);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Loop interaction
plife.run();
}
public static PixGrid getGrid()
{
return myGrid;
}
}