@@ -174,19 +174,27 @@ void imageRenderParallel(HitTable scene, int spp, String filename) {
174174
175175 void windowRender (HitTable scene , int spp ) {
176176 Color [] pixelColors = new Color [image_height *image_width ];
177+
177178 for (int i = 0 ; i <image_height *image_width ; ++i )
178179 pixelColors [i ] = new Color (0 );
179180
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+ }
181186
187+ private JFrame getjFrame (HitTable scene , int spp , Color [] pixelColors ) {
182188 JFrame window = new JFrame ("RayTracing" ) {
183189 @ Override
184190 public void paint (Graphics g ) {
185191 super .paint (g );
186192 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 );
187195
188196 // 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 ) {
190198 long start_time = System .currentTimeMillis (); // Time for Benchmark
191199 for (int y = 0 ; y < image_height ; ++y ) {
192200 for (int x = 0 ; x < image_width ; ++x ) {
@@ -198,31 +206,28 @@ public void paint(Graphics g) {
198206 pixelColors [y * image_width + x ].equalAdd (rayColor (camera .get_ray (u , v ), scene , depth ));
199207 g .setColor (writeAwtColor (pixelColors [y * image_width + x ], s + 1 ));
200208 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+ }
201214 }
202215 }
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 );
204225 }
205226 }
206227 };
207228
208229
209230 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 ;
227232 }
228233}
0 commit comments