/*============================================================================= | | NAME | | PrenticeCanvas | | DESCRIPTION | | WhatItDoes | | LEGAL | | Prentice Version 1.1 - An Artist's Software Apprentice. | Copyright (C) 2003-2008 by Sean Erik O'Connor. All Rights Reserved. | | This program is free software; you can redistribute it and/or | modify it under the terms of the GNU General Public License | as published by the Free Software Foundation; version 2 | of the License. | | This program is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | | You should have received a copy of the GNU General Public License | along with this program; if not, write to the Free Software | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | USA. | | The author's address is artifex@seanerikoconnor.freeservers.com. | +============================================================================*/ package Model ; // Package name for this project. import java.awt.* ; // AWT basic window handling. import java.awt.geom.* ; // 2D graphics. import java.awt.image.* ; // Buffered images and operations. import java.io.* ; // File I/O. import javax.swing.* ; // Swing main GUI package. import javax.swing.event.* ; // Swing GUI event handling. import javax.imageio.* ; // ImageIO. import Prentice.* ; // Prentice stuff. // Color model + raster image. public class PrenticeCanvas extends BufferedImage { // Shared by all instances. int imageWidth ; int imageHeight ; Prentice app ; public int getWidth() { return imageWidth ; } public int getHeight() { return imageHeight ; } // Construct a hi-res image buffer. public PrenticeCanvas( Prentice app, int imageWidth, int imageHeight ) { // True color image with red, green and blue integer pixels. super( imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB ) ; this.app = app ; this.imageWidth = imageWidth ; this.imageHeight = imageHeight ; // Obtain a graphics context for this image. Graphics g = createGraphics() ; Graphics2D g2d = (Graphics2D) g ; // Fill the image background with solid cyan. g.setColor( Color.cyan ) ; g.fillRect( 0, 0, imageWidth - 1, imageHeight - 1 ) ; // Read a JPEG image asynchronously from file. String currentDir = System.getProperty("user.dir") ; DebugLog.println( "Current dir = " + currentDir ) ; String path ; String suffix = "Build" ; String suffix2 = "build" ; String suffix3 = "build" + File.separator + "PrenticeNetBeans"; String suffix4 = "Build" + File.separator + "PrenticeNetBeans" ; if (currentDir.endsWith( suffix ) || currentDir.endsWith( suffix2 )) { path = currentDir.substring( 0, currentDir.length() - suffix.length()-1) ; DebugLog.println( "Modified current dir = " + path ) ; } else if (currentDir.endsWith( suffix3 )) { path = currentDir.substring( 0, currentDir.length() - suffix3.length()-1) ; DebugLog.println( "Modified current dir = " + path ) ; } else if (currentDir.endsWith( suffix4 )) { path = currentDir.substring( 0, currentDir.length() - suffix4.length()-1) ; DebugLog.println( "Modified current dir = " + path ) ; } else { path = currentDir ; DebugLog.println( "Unmodified current dir = " + path ) ; } Image img = Toolkit.getDefaultToolkit().getImage( path + File.separator + "data/arcadia.jpg" ) ; currentDir = null ; suffix = null ; // Create a media tracker object with a blank container to // wait until the image is loaded. MediaTracker mediaTracker = new MediaTracker( new Container() ) ; mediaTracker.addImage( img, 0 ) ; try { mediaTracker.waitForID( 0 ) ; } catch( InterruptedException e ) { System.out.println( e ) ; System.exit( 1 ) ; } // Draw the image into the display. g.drawImage( img, 0, 0, // Upper left corner location in target. null // ImageObserver to be notified (none). ) ; // -----------------< Access individual pixels >--------------------- // Access a single pixel near the center. int pixel = getRGB( imageWidth / 2 + 20, imageHeight / 2 + 20 ) ; // Parse into the RGB and alpha channel components. int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; // XOR the colors, leaving the alpha channel alone. red = 256 - red ; green = 256 - green ; blue = 256 - blue ; // Pack the pixel again. pixel = (alpha << 24) | (red << 16) | (green << 8) | blue; // Write it back into the image at (20, 20). setRGB( imageWidth / 2 + 20, imageHeight / 2 + 20, pixel ) ; int boxX = imageWidth / 2 - 20 ; int boxY = imageHeight / 2 - 20 ; int boxWidth = 40 ; int boxHeight = 40 ; int offset = 0 ; int arrayStride = boxWidth ; int [] rgbArray = new int[ boxWidth * boxHeight ] ; try { //pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)]; for (int x = 0 ; x < boxWidth ; ++x) { for (int y = 0 ; y < boxHeight ; ++y) { rgbArray[ y * arrayStride + x ] = 2 * y ; } } setRGB( boxX, boxY, boxWidth, boxHeight, rgbArray, offset, arrayStride ) ; } catch( ArrayIndexOutOfBoundsException e ) { System.err.println( "setRGB past bounds of array." ) ; System.err.println( "Message :" + e.getMessage() ) ; System.err.println( "Stack trace begin" ) ; e.printStackTrace() ; System.err.println( "end stack trace" ) ; System.exit( 1 ) ; } // ---------------------------< Draw grid >------------------------- g.setColor( Color.orange ) ; for (int gridY = 0 ; gridY < imageHeight ; gridY += (imageHeight / 10)) { // Horizontal endpoints of line. Point2D.Double p1 = new Point2D.Double( 0, gridY ) ; Point2D.Double p2 = new Point2D.Double( imageWidth - 1, gridY ) ; // Create the line and draw it. Line2D.Double line = new Line2D.Double( p1, p2 ) ; g2d.draw( line ) ; } for (int gridX = 0 ; gridX < imageWidth ; gridX += (imageHeight / 10)) { // Vertical endpoints of line. Point2D.Double p1 = new Point2D.Double( gridX, 0 ) ; Point2D.Double p2 = new Point2D.Double( gridX, imageHeight - 1) ; // Create the line and draw it. Line2D.Double line = new Line2D.Double( p1, p2 ) ; g2d.draw( line ) ; } // Save the image to file as both png and jpeg types. try { File pingHandle = new File( path + File.separator + "data" + File.separator + "rendered.png"); ImageIO.write( this, "png", pingHandle ) ; File jpegHandle = new File( path + File.separator + "data" + File.separator + "rendered.jpg"); ImageIO.write( this, "jpg", jpegHandle ) ; } catch( IOException e ) { System.out.println( e ) ; System.exit( 1 ) ; } // Don't need graphics context anymore. g.dispose() ; } // end PrenticeImage constructor // Repaint the canvas and tell scrollable image to update itself. public void update() { repaint() ; app.getView().getScrollableImage().update() ; } public void repaint() { // ----------------------< Graphics for Orbital >------------------------- // Obtain a graphics context for this image. Graphics g = createGraphics() ; Graphics2D g2d = (Graphics2D) g ; // Get the wire frame model. GeneralPath polyline = app.getModel().getWireFrame() ; // Set color to green and draw the entire polyline. g2d.setColor( Color.green ) ; g2d.draw( polyline ) ; polyline = null ; } } // end PrenticeImage