PennDraw.java
/**
     * Save onscreen image to file - suffix must be png, jpg, or gif.
     * @param filename the name of the file with one of the required suffixes
     */
Save onscreen image to file - suffix must be png, jpg, or gif.
@param filename the name of the file with one of the required suffixes
Save onscreen image to file - suffix must be png jpg or gif . @param filename the name of the file with one of the required suffixes
public static void save(String filename) {
        File file = new File(filename);
        String suffix = filename.substring(filename.lastIndexOf('.') + 1);
        if (suffix.toLowerCase().equals("png")) {
            try { ImageIO.write(onscreenImage, suffix, file); }
            catch (IOException e) { e.printStackTrace(); }
        }
        else if (suffix.toLowerCase().equals("jpg")) {
            WritableRaster raster = onscreenImage.getRaster();
            WritableRaster newRaster;
            newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2});
            DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel();
            DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(),
                                                          cm.getRedMask(),
                                                          cm.getGreenMask(),
                                                          cm.getBlueMask());
            BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false,  null);
            try { ImageIO.write(rgbBuffer, suffix, file); }
            catch (IOException e) { e.printStackTrace(); }
        }
        else {
            System.out.println("Invalid image file type: " + suffix);
        }
    }
public static void save ( String filename ) { File file = new File ( filename ) ; String suffix = filename . substring ( filename . lastIndexOf ( '.' ) + 1 ) ; if ( suffix . toLowerCase ( ) . equals ( "png" ) ) { try { ImageIO . write ( onscreenImage , suffix , file ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } else if ( suffix . toLowerCase ( ) . equals ( "jpg" ) ) { WritableRaster raster = onscreenImage . getRaster ( ) ; WritableRaster newRaster ; newRaster = raster . createWritableChild ( 0 , 0 , width , height , 0 , 0 , new int [ ] { 0 , 1 , 2 } ) ; DirectColorModel cm = ( DirectColorModel ) onscreenImage . getColorModel ( ) ; DirectColorModel newCM = new DirectColorModel ( cm . getPixelSize ( ) , cm . getRedMask ( ) , cm . getGreenMask ( ) , cm . getBlueMask ( ) ) ; BufferedImage rgbBuffer = new BufferedImage ( newCM , newRaster , false , null ) ; try { ImageIO . write ( rgbBuffer , suffix , file ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } else { System . out . println ( "Invalid image file type: " + suffix ) ; } }
Save onscreen image to file - suffix must be png, jpg, or gif.
Save onscreen image to file - suffix must be png jpg or gif .
// png files // need to change from ARGB to RGB for jpeg // reference: http://archives.java.sun.com/cgi-bin/wa?A2=ind0404&L=java2d-interest&D=0&P=2727