@@ -174,19 +174,27 @@ void imageRenderParallel(HitTable scene, int spp, String filename) {
174
174
175
175
void windowRender (HitTable scene , int spp ) {
176
176
Color [] pixelColors = new Color [image_height *image_width ];
177
+
177
178
for (int i = 0 ; i <image_height *image_width ; ++i )
178
179
pixelColors [i ] = new Color (0 );
179
180
180
- long init_time = System .currentTimeMillis (); // Time for Benchmark
181
+ JFrame window = getjFrame (scene , spp , pixelColors );
182
+ window .setVisible (true );
183
+ window .setResizable (false );
184
+ window .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
185
+ }
181
186
187
+ private JFrame getjFrame (HitTable scene , int spp , Color [] pixelColors ) {
182
188
JFrame window = new JFrame ("RayTracing" ) {
183
189
@ Override
184
190
public void paint (Graphics g ) {
185
191
super .paint (g );
186
192
System .out .println (image_width + "x" + image_height + ", " + spp + " samples per pixel" );
193
+ long init_time = System .currentTimeMillis (); // Time for Benchmark
194
+ BufferedImage image = new BufferedImage (image_width , image_height , BufferedImage .TYPE_INT_RGB );
187
195
188
196
// For each sample per pixel, render the whole image
189
- for (long s = 0 ; s < spp ; ++s ) {
197
+ for (long s = 0 ; s < spp ; ++s ) {
190
198
long start_time = System .currentTimeMillis (); // Time for Benchmark
191
199
for (int y = 0 ; y < image_height ; ++y ) {
192
200
for (int x = 0 ; x < image_width ; ++x ) {
@@ -198,31 +206,28 @@ public void paint(Graphics g) {
198
206
pixelColors [y * image_width + x ].equalAdd (rayColor (camera .get_ray (u , v ), scene , depth ));
199
207
g .setColor (writeAwtColor (pixelColors [y * image_width + x ], s + 1 ));
200
208
g .drawRect (x , y , 1 , 1 );
209
+
210
+ // Save the pixel to the image
211
+ if (s == spp - 1 ) {
212
+ image .setRGB (x , y , writeAwtColor (pixelColors [y * image_width + x ], s + 1 ).getRGB ());
213
+ }
201
214
}
202
215
}
203
- System .out .print ("\r " + "Sample: " + (s +1 ) + " " + "in " + (double )(System .currentTimeMillis ()-start_time )/1000 +"s" );
216
+ System .out .println ("Sample: " + (s +1 ) + " " + "in " + (double )(System .currentTimeMillis ()-start_time )/1000 +"s" );
217
+ }
218
+ try {
219
+ ImageIO .write (image , "png" , new java .io .File ("Output.png" ));
220
+ System .out .println ("\n Image saved to Output.png" );
221
+ System .out .println ("Rendering finished in " + (double )(System .currentTimeMillis ()-init_time )/1000 +"s" );
222
+ }
223
+ catch (IOException e ) {
224
+ throw new RuntimeException (e );
204
225
}
205
226
}
206
227
};
207
228
208
229
209
230
window .setSize (image_width , image_height );
210
- window .setVisible (true );
211
- window .setResizable (false );
212
- window .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
213
-
214
- // Save the image to a file
215
- try {
216
- BufferedImage image = new BufferedImage (image_width , image_height , BufferedImage .TYPE_INT_RGB );
217
- Graphics2D graphics = image .createGraphics ();
218
- window .paint (graphics );
219
- ImageIO .write (image , "png" , new java .io .File ("Output.png" ));
220
- System .out .println ("\n Image saved to Output.png" );
221
- System .out .println ("Rendering finished in " + (double )(System .currentTimeMillis ()-init_time )/1000 +"s" );
222
-
223
- }
224
- catch (IOException e ) {
225
- throw new RuntimeException (e );
226
- }
231
+ return window ;
227
232
}
228
233
}
0 commit comments