diff --git a/core/PApplet.java b/core/PApplet.java index 876d44fbd..43c0b413a 100644 --- a/core/PApplet.java +++ b/core/PApplet.java @@ -1,12 +1,11 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BApplet - applet base class for the bagel engine + PApplet - applet base class for the bagel engine Part of the Processing project - http://processing.org - Copyright (c) 2001-03 - Ben Fry, Massachusetts Institute of Technology and - Casey Reas, Interaction Design Institute Ivrea + Except where noted, code is written by Ben Fry and + Copyright (c) 2001-04 Massachusetts Institute of Technology This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -24,6 +23,8 @@ Boston, MA 02111-1307 USA */ +package processing.core; + import java.applet.*; import java.awt.*; import java.awt.event.*; @@ -34,45 +35,19 @@ import java.net.URL; import java.text.*; import java.util.*; -#ifdef SERIAL -#ifndef RXTX -import javax.comm.*; -#else -// rxtx uses package gnu.io, but all the class names -// are the same as those used by javax.comm -import gnu.io.*; -#endif -#endif - -public class BApplet extends Applet - implements BConstants, Runnable, +public class PApplet extends Applet + implements PConstants, Runnable, MouseListener, MouseMotionListener, KeyListener -#ifdef SERIAL - , SerialPortEventListener -#endif { - //static final String JDK_VERSION = static final double jdkVersion = toDouble(System.getProperty("java.version").substring(0,3)); - /* - static private boolean isOneTwoOrBetter() - { - return VERSION_MAJOR.equals("1.2") || VERSION_MAJOR.equals("1.3") - || VERSION_MAJOR.equals("1.4") || VERSION_MAJOR.equals("1.5"); - } - */ - public BGraphics g; + public PGraphics g; static final boolean THREAD_DEBUG = true; - // java/memimgsrc specific public int pixels[]; - //DirectColorModel cm; - //MemoryImageSource mis; - //Image image; - // end java/memimgsrc specific public int mouseX, mouseY; public int pmouseX, pmouseY; @@ -80,13 +55,11 @@ public class BApplet extends Applet public boolean mousePressed; public MouseEvent mouseEvent; - //boolean mousePressedBriefly; // internal only public int key; //public int keyCode; public boolean keyPressed; public KeyEvent keyEvent; - //boolean keyPressedBriefly; // internal only long millisOffset; @@ -134,12 +107,10 @@ public class BApplet extends Applet static final int DEFAULT_HEIGHT = 100; int width, height; -#ifdef LIBRARIES int libraryCount; - BLibrary libraries[]; -#endif + PLibrary libraries[]; - // this text isn't seen unless BApplet is used on its + // this text isn't seen unless PApplet is used on its // own and someone takes advantage of leechErr.. not likely static final String LEECH_WAKEUP = "Error while running applet."; PrintStream leechErr; @@ -157,7 +128,7 @@ public class BApplet extends Applet //checkParams(); // can/may be resized later - //g = new BGraphics(DEFAULT_WIDTH, DEFAULT_HEIGHT); + //g = new PGraphics(DEFAULT_WIDTH, DEFAULT_HEIGHT); initg(); addMouseListener(this); @@ -202,42 +173,21 @@ public class BApplet extends Applet this.width = g.width; this.height = g.height; g.applet = this; - -#ifdef SONIC - // init sound engine - // check if sonic() was called - if (!sonicInit) { - // sound not initiated - //print("no sound\n"); - this.length = 0; - - } else if (sonic == null) { - beginSound(); - } -#endif } // override for subclasses (i.e. opengl) // so that init() doesn't have to be replicated public void initg() { - g = new BGraphics(DEFAULT_WIDTH, DEFAULT_HEIGHT); + g = new PGraphics(DEFAULT_WIDTH, DEFAULT_HEIGHT); } public void start() { - //System.out.println("BApplet.start"); + //System.out.println("PApplet.start"); thread = new Thread(this); thread.start(); - -#ifdef SONIC - // start sound engine thread - // TODO: it starts too quickly, causes sound corruption at start - if (sonic != null) { - sonic.start(this); - } -#endif } @@ -265,33 +215,13 @@ public class BApplet extends Applet } */ -#ifdef LIBRARIES for (int i = 0; i < libraryCount; i++) { - libraries[i].stop(); + libraries[i].stop(); // endNet/endSerial etc } -#endif - - // maybe these will have better luck here -#ifdef SERIAL - endSerial(); -#endif - -#ifdef NETWORK - endNet(); -#endif - -#ifdef VIDEO - endVideo(); -#endif - -#ifdef SONIC - endSound(); -#endif } public Dimension getPreferredSize() { - //println("getting pref'd size " + width + " " + height); return new Dimension(width, height); } @@ -324,54 +254,19 @@ public class BApplet extends Applet this.width = g.width; this.height = g.height; - //allocate(); - // set this here, and if not inside browser, getDocumentBase() // will fail with a NullPointerException, and cause applet to // be set to null. might be a better way to deal with that, but.. g.applet = this; - - // do all the defaults down here, because - // subclasses need to go through this function - // NOT TRUE, SO REMOVING - //g.lighting = false; } - /* - public void allocate() { - cm = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff); - - g = new BGraphics(width, height); - pixels = g.pixels; - - // because of a java bug.. unless pixels are registered as - // opaque before their first run, the memimgsrc will flicker - // and run very slowly (as it tries to do broken awt 1.1 alpha) - for (int i = 0; i < pixels.length; i++) pixels[i] = 0xffffffff; - - // setup MemoryImageSource - mis = new MemoryImageSource(width, height, pixels, 0, width); - mis.setFullBufferUpdates(true); // maybe this will help ipaq? - mis.setAnimated(true); - image = Toolkit.getDefaultToolkit().createImage(mis); - } - */ - - boolean updated = false; public void update() { if (firstFrame) firstFrame = false; if (THREAD_DEBUG) println(" 3a update() internal " + firstFrame); - /* - println("update() internal"); - Graphics graphics = this.getGraphics(); - //println("inside update " + graphics); - if (graphics != null) paint(graphics); - */ - //println("new update()"); repaint(); if (THREAD_DEBUG) println(" 3b update() internal " + firstFrame); getToolkit().sync(); // force repaint now (proper method) @@ -406,7 +301,7 @@ public class BApplet extends Applet //Exception e = new Exception(); //e.printStackTrace(); - // moving this into BGraphics caused weird sluggishness on win2k + // moving this into PGraphics caused weird sluggishness on win2k //g.mis.newPixels(pixels, g.cm, 0, width); // must call this // make sure the screen is visible and usable @@ -424,7 +319,7 @@ public class BApplet extends Applet while ((Thread.currentThread() == thread) && !finished) { updated = false; - if (BApplet.THREAD_DEBUG) println("nextFrame()"); + if (PApplet.THREAD_DEBUG) println("nextFrame()"); nextFrame(); // moving this to update() (for 0069+) for linux sync problems @@ -439,9 +334,9 @@ public class BApplet extends Applet //Thread.yield(); // windows doesn't like 'yield', so have to sleep at least // for some small amount of time. - if (BApplet.THREAD_DEBUG) System.out.println("gonna sleep"); + if (PApplet.THREAD_DEBUG) System.out.println("gonna sleep"); Thread.sleep(1); // sleep to make OS happy - if (BApplet.THREAD_DEBUG) System.out.println("outta sleep"); + if (PApplet.THREAD_DEBUG) System.out.println("outta sleep"); } catch (InterruptedException e) { } } } @@ -836,518 +731,6 @@ public class BApplet extends Applet } - // ------------------------------------------------------------ - - // client/server net stuff - -#ifdef NETWORK - - BServer server; - BClient client; - String net; - int net_mode = CLIENT; //0 Sever 1 Client default CLIENT - - - public void beginNet(String host, int port) { //client - net_mode = CLIENT; - client = new BClient(this, host, port); - client.start(); - } - - - public void beginNet(int port) { //server - net_mode = SERVER; - server = new BServer(this, port); - server.start(); - } - - - void netEvent() { // client message - } - - void netEvent(int event) { // server message - } - - - public void netWrite(String message) { - if (net_mode == CLIENT) { - client.writeData(message); - } else if (net_mode == SERVER) { - server.broadcast(message); - } - } - - - public void endNet() { - if (net_mode == CLIENT) { - if (client != null) client.destroy(); - } else if (net_mode == SERVER) { - if (server != null) server.destroy(); - } - } - -#endif - - - // ------------------------------------------------------------ - - // SONIC - -#ifdef SONIC - - BSonic sonic; - - static final int DEFAULT_LENGTH = 512; - - public int samples[]; - public int length = -1; // frame length - public int frequency = -1; // playback frequency - - public boolean sonicInit = false; - - public void beginSound() { - // iniate sonic without sound post processing, soundEvent does not work - if (sonicInit) return; - - sonicInit = true; - - if (length == -1) { - this.length = 0; // size is zero, we are no going to use it - } - - this.sonic = new BSonic(); // start sonic without a samples buffer - } - - public void beginSound(int l) { - // iniate sonic with sound post processing - if (sonicInit) return; - - sonicInit = true; - - if (l <= 0) { - l = DEFAULT_LENGTH; - } - - this.length = l; // set length of samples buffer (frame) - - frequency = BSonic.SAMPLING_RATE; - this.samples = new int[length]; // create samples buffer - this.sonic = new BSonic(samples); // initiate sonic with this samples buffer - } - - public void endSound() { - // kill sound thread if not killed already - if (sonic != null) { - sonic.stop(); - } - } - - public BSound loadSound(String filename) { - if (sonic == null || sonicInit == false) { - //start sonic in lite mode - beginSound(); - } - - BSound sound = sonic.loadSound(filename); - - if (sound == null) { - // error loading, assign empty sound and continue - sound = new BSound(1); - } - return sound; - } - - public BSound generate(int form, int freq) { - if (sonic == null) { - //start sonic in lite mode - beginSound(); - } - - return sonic.generate(form, freq); - } - - public BSound microphone() { - if (sonic == null) { - //start sonic in lite mode - beginSound(); - } - - return sonic.microphone(); - } - - void soundEvent() { } - - public void volume(float v) { - BSonic.volume(v); - } - - public void length(int l) { - // do not change block size once BSonic is created! - if (sonic != null) { - return; - } - - this.length = l; - } - - void frequency(int f) { - // do not change block size once BSonic is created! - if (sonic != null) { - return; - } - // mixer frequency cannot be changed for now - //this.frequency = f; - } - - - // sound properties - - public void volume(BSound sound, float v) { - sound.volume(v); - } - - public void speed(BSound sound,float s) { - sound.speed(s); - } - - public void jump(BSound sound, int s) { - sound.jump(s); - } - - // sound playback - - public void play(BSound sound) { - if (sonic == null) { - //start sonic in lite mode - beginSound(); - } - sonic.play(sound); - } - - public void repeat(BSound sound) { - if (sonic == null) { - //start sonic in lite mode - beginSound(); - } - sonic.repeat(sound); - } - - public void play(BSound sound, int in, int out) { - if (sonic == null) { - //start sonic in lite mode - beginSound(); - } - sonic.play(sound, in, out); - } - - public void repeat(BSound sound, int in, int out) { - if (sonic == null) { - //start sonic in lite mode - beginSound(); - } - sonic.repeat(sound, in, out); - } - - - public void pause(BSound sound) { - if (sonic == null) { - //start sonic in lite mode - beginSound(); - } - sonic.pause(sound); - } - - public void stop(BSound sound) { - if (sonicInit == false) { - //start sonic in lite mode - beginSound(); - } - sonic.stop(sound); - } - -#endif - - - // ------------------------------------------------------------ - - // VIDEO INPUT - -#ifdef VIDEO - - BVideo videoInput; //to be incorporated in BApplet - //int vpixels[]; //pixel array - //BImage vimage; //image containing the actual frame - //int vwidth, vheight; //video resolution - BImage video; - // does this need to be available? - // naming is awkward, and is only requested by user - // could make users keep track of it themselves - //int vfps; - - - public void beginVideo(int vfps) { - beginVideo(0, 0, vfps); - } - - - public void beginVideo(int vwidth, int vheight, int vfps) { - //int[] vpixels = new int[vwidth * vheight]; - //video = new BImage(vpixels, vwidth, vheight, RGB); - - try { - if ((vwidth != 0) && (vheight != 0)) { - // if set, open to a specific size - videoInput = new BVideo(this, vwidth, vheight, vfps, false); - } else { - // if not, open to the default size - videoInput = new BVideo(this, 320, 240, vfps, false); - } - videoInput.start(); - while (videoInput.image == null) { - try { - Thread.sleep(5); - } catch (InterruptedException e) { } - //println("waiting"); - } - video = videoInput.image; - - // hopefully this catches a ClassNotFoundException - // or whatever might come up on runtime if qtjava not available - } catch (Exception e) { - System.err.println("beginVideo() failed.."); - e.printStackTrace(); - } - } - - - public void beginVideo(String filename) { - videoInput = new BVideo(this, filename); - videoInput.play = true; - videoInput.loop = true; - videoInput.start(); - while (videoInput.image == null) { - try { - Thread.sleep(5); - } catch (InterruptedException e) { } - //println("waiting"); - } - video = videoInput.image; - } - - - public void endVideo() { - if (videoInput != null) { - //videoInput.stop(); - videoInput.dispose(); - } - } - - - void videoEvent() { - // weird.. not sure why this would be here.. - //videoInput.getPixelArray(vimage.pixels); - //videoEvent(); - } - - BVideo loadVideo(String requestFile) { - BVideo t = new BVideo(this, requestFile); - return t; - } - - void play(BVideo v) { - v.play(); - } - - void stop(BVideo v) { - v.stop(); - } - - void repeat(BVideo v) { - v.repeat(); - } - - void pause(BVideo v) { - v.pause(); - } - - void image(BVideo v, float x, float y) { - image(v.image, x, y); - } -#endif - - - // ------------------------------------------------------------ - - // SERIAL PORT ACTION - - -#ifdef SERIAL - // properties can be passed in for default values - // otherwise defaults to 9600 N81 - - // these could be made static, which might be a solution - // for the classloading problem.. because if code ran again, - // the static class would have an object that could be closed - - static SerialPort serialPort; - static InputStream serialInput; - static OutputStream serialOutput; - int serial; // last byte of data received - - private String serial_port = "COM1"; - private int serial_rate = 9600; - private char serial_parity = 'N'; - private int serial_databits = 8; - private float serial_stopbits = 1; - - - public void serialProperties(Properties props) { - //System.out.println("setting serial properties"); - serial_port = props.getProperty("serial.port", serial_port); - serial_rate = Integer.parseInt(props.getProperty("serial.rate", "9600")); - serial_parity = props.getProperty("serial.parity", "N").charAt(0); - serial_databits = Integer.parseInt(props.getProperty("serial.databits", "8")); - serial_stopbits = new Float(props.getProperty("serial.stopbits", "1")).floatValue(); - } - - - // opens using the defaults found in pde.properties - public void beginSerial() { - beginSerial(serial_port, serial_rate, - serial_parity, serial_databits, serial_stopbits); - //beginSerial(PdeApplet.getInteger("serial.rate")); - } - - // opens using default port from pde.properties, - public void beginSerial(int rate) { - beginSerial(serial_port, rate, - serial_parity, serial_databits, serial_stopbits); - } - - public void beginSerial(String port, int rate) { - beginSerial(port, rate, - serial_parity, serial_databits, serial_stopbits); - } - - public void beginSerial(String port) { - beginSerial(port, serial_rate, - serial_parity, serial_databits, serial_stopbits); - //beginSerial(PdeApplet.getInteger("serial.rate")); - } - - public void beginSerial(String port, int rate, - char iparity, int databits, float istopbits) { - if (serialPort != null) serialPort.close(); - - int parity = SerialPort.PARITY_NONE; - if (iparity == 'E') parity = SerialPort.PARITY_EVEN; - if (iparity == 'O') parity = SerialPort.PARITY_ODD; - - int stopbits = SerialPort.STOPBITS_1; - if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5; - if (istopbits == 2) stopbits = SerialPort.STOPBITS_2; - - //ortname, rate, SerialPort.PARITY_NONE, - // SerialPort.DATABITS_8, SerialPort.STOPBITS_1); - try { - Enumeration portList = CommPortIdentifier.getPortIdentifiers(); - while (portList.hasMoreElements()) { - CommPortIdentifier portId = - (CommPortIdentifier) portList.nextElement(); - - if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { - if (portId.getName().equals(port)) { - serialPort = (SerialPort)portId.open("bagel applet serial", 2000); - serialInput = serialPort.getInputStream(); - serialOutput = serialPort.getOutputStream(); - serialPort.setSerialPortParams(rate, databits, stopbits, parity); - serialPort.addEventListener(this); - serialPort.notifyOnDataAvailable(true); - } - } - } - //if (serialPort == null) { - //System.err.println("could not find a serial port named " + port); - //} - } catch (Exception e) { - exception = e; - e.printStackTrace(); - serialPort = null; - serialInput = null; - serialOutput = null; - } - //System.out.println("done with beginserial"); - } - - - public void endSerial() { - try { - // do io streams need to be closed first? - if (serialInput != null) serialInput.close(); - if (serialOutput != null) serialOutput.close(); - } catch (Exception e) { - e.printStackTrace(); - } - serialInput = null; - serialOutput = null; - - try { - // close the port - if (serialPort != null) serialPort.close(); - //serialPort = null; // this doesn't seem to help, but maybe on win? - } catch (Exception e) { - e.printStackTrace(); - } - serialPort = null; - } - - - // needs 'public' otherwise kjc assumes private - void serialEvent() { } - - public void serialEvent(SerialPortEvent serialEvent) { - //System.out.println(serialEvent); - if (serialEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { - try { - while (serialInput.available() > 0) { - serial = serialInput.read(); - //serialEvent = true; - serialEvent(); - } - } catch (IOException e) { - System.err.println("problem reading from the serial port"); - } - } - } - - - public void serialWrite(int what) { - //if (serialPort != null) { - try { - serialOutput.write(what & 0xff); - serialOutput.flush(); // hmm, not sure if a good idea - - } catch (Exception e) { // null pointer or serial port dead - //} else { - System.err.println("serial port not working"); - e.printStackTrace(); - } - } - - public void serialWrite(byte bytes[]) { - //if (serialPort != null) { - try { - serialOutput.write(bytes); - serialOutput.flush(); // hmm, not sure if a good idea - - } catch (Exception e) { // null pointer or serial port dead - //} else { - System.err.println("serial port not working"); - e.printStackTrace(); - } - } -#endif - // ------------------------------------------------------------ @@ -1409,7 +792,7 @@ public class BApplet extends Applet int cursor_type = ARROW; // cursor type boolean cursor_visible = true; // cursor visibility flag - BImage invisible_cursor; + PImage invisible_cursor; /** @@ -1429,7 +812,7 @@ public class BApplet extends Applet * Only works with JDK 1.2 and later. * Currently seems to be broken on Java 1.4 for Mac OS X */ - void cursor(BImage image, int hotspotX, int hotspotY) { + void cursor(PImage image, int hotspotX, int hotspotY) { //if (!isOneTwoOrBetter()) { if (jdkVersion < 1.2) { System.err.println("cursor() error: Java 1.2 or higher is " + @@ -1498,8 +881,8 @@ public class BApplet extends Applet if (!cursor_visible) return; // don't hide if already hidden. if (invisible_cursor == null) { - //invisible_cursor = new BImage(new int[32*32], 32, 32, RGBA); - invisible_cursor = new BImage(new int[16*16], 16, 16, RGBA); + //invisible_cursor = new PImage(new int[32*32], 32, 32, RGBA); + invisible_cursor = new PImage(new int[16*16], 16, 16, RGBA); } // was formerly 16x16, but the 0x0 was added by jdf as a fix // for macosx, which didn't wasn't honoring the invisible cursor @@ -1780,7 +1163,7 @@ public class BApplet extends Applet float perlin_amp_falloff = 0.5f; // 50% reduction/octave // [toxi 031112] - // new vars needed due to recent change of cos table in BGraphics + // new vars needed due to recent change of cos table in PGraphics int perlin_TWOPI, perlin_PI; float[] perlin_cosTable; float perlin[]; @@ -1814,7 +1197,7 @@ public class BApplet extends Applet perlin[i] = (float)Math.random(); } // [toxi 031112] - // noise broke due to recent change of cos table in BGraphics + // noise broke due to recent change of cos table in PGraphics // this will take care of it perlin_cosTable=g.cosLUT; perlin_TWOPI=perlin_PI=g.SINCOS_LENGTH; @@ -2078,7 +1461,7 @@ public class BApplet extends Applet * i.e. split("a, b", " ,") -> { "a", "b" } * * To include all the whitespace possibilities, use the variable - * WHITESPACE, found in BConstants: + * WHITESPACE, found in PConstants: * * i.e. split("a | b", WHITESPACE + "|"); -> { "a", "b" } */ @@ -2457,7 +1840,7 @@ public class BApplet extends Applet public Stopper stopper; class Stopper implements Runnable { - //BApplet parent; + //PApplet parent; Thread thread; public Stopper() { @@ -2503,7 +1886,7 @@ public class BApplet extends Applet //System.out.println(e); Point where = ((Frame) e.getSource()).getLocation(); //System.out.println(e); - System.err.println(BApplet.EXTERNAL_MOVE + " " + + System.err.println(PApplet.EXTERNAL_MOVE + " " + where.x + " " + where.y); System.err.flush(); } @@ -2512,7 +1895,7 @@ public class BApplet extends Applet frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { //if (externalRuntime) { - System.err.println(BApplet.EXTERNAL_QUIT); + System.err.println(PApplet.EXTERNAL_QUIT); System.err.flush(); // important //} System.exit(0); @@ -2523,7 +1906,7 @@ public class BApplet extends Applet static public void main(String args[]) { if (args.length < 1) { - System.err.println("error: BApplet "); + System.err.println("error: PApplet "); System.exit(1); } @@ -2537,7 +1920,7 @@ public class BApplet extends Applet Frame frame = new Frame(); frame.pack(); // maybe get insets Class c = Class.forName(args[external ? 1 : 0]); - BApplet applet = (BApplet) c.newInstance(); + PApplet applet = (PApplet) c.newInstance(); applet.init(); applet.start(); @@ -2644,7 +2027,7 @@ public class BApplet extends Applet } - public void alpha(BImage alpha) { + public void alpha(PImage alpha) { g.alpha(alpha); } @@ -2664,7 +2047,7 @@ public class BApplet extends Applet } - public BImage get(int x, int y, int w, int h) { + public PImage get(int x, int y, int w, int h) { return g.get(x, y, w, h); } @@ -2674,7 +2057,7 @@ public class BApplet extends Applet } - public void set(int x, int y, BImage image) { + public void set(int x, int y, PImage image) { g.set(x, y, image); } @@ -2684,7 +2067,7 @@ public class BApplet extends Applet } - public void copy(BImage src, int sx, int sy, int dx, int dy) { + public void copy(PImage src, int sx, int sy, int dx, int dy) { g.copy(src, sx, sy, dx, dy); } @@ -2695,13 +2078,13 @@ public class BApplet extends Applet } - public void copy(BImage src, int sx1, int sy1, int sx2, int sy2, + public void copy(PImage src, int sx1, int sy1, int sx2, int sy2, int dx1, int dy1, int dx2, int dy2) { g.copy(src, sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2); } - public void blend(BImage src, int sx, int sy, int dx, int dy, int mode) { + public void blend(PImage src, int sx, int sy, int dx, int dy, int mode) { g.blend(src, sx, sy, dx, dy, mode); } @@ -2717,7 +2100,7 @@ public class BApplet extends Applet } - public void blend(BImage src, int sx1, int sy1, int sx2, int sy2, + public void blend(PImage src, int sx1, int sy1, int sx2, int sy2, int dx1, int dy1, int dx2, int dy2, int mode) { g.blend(src, sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2, mode); } @@ -2753,7 +2136,7 @@ public class BApplet extends Applet } - public final void addTexture(BImage image) { + public final void addTexture(PImage image) { g.addTexture(image); } @@ -2778,7 +2161,7 @@ public class BApplet extends Applet } - public void texture(BImage image) { + public void texture(PImage image) { g.texture(image); } @@ -2844,7 +2227,7 @@ public class BApplet extends Applet } - public void flat_image(BImage image, int sx1, int sy1) { + public void flat_image(PImage image, int sx1, int sy1) { g.flat_image(image, sx1, sy1); } @@ -2998,12 +2381,12 @@ public class BApplet extends Applet } - public BImage loadImage(String filename) { + public PImage loadImage(String filename) { return g.loadImage(filename); } - public BImage loadImage(String filename, boolean force) { + public PImage loadImage(String filename, boolean force) { return g.loadImage(filename, force); } @@ -3013,45 +2396,45 @@ public class BApplet extends Applet } - public void image(BImage image, float x1, float y1) { + public void image(PImage image, float x1, float y1) { g.image(image, x1, y1); } - public void image(BImage image, + public void image(PImage image, float x1, float y1, float x2, float y2) { g.image(image, x1, y1, x2, y2); } - public void image(BImage image, + public void image(PImage image, float x1, float y1, float x2, float y2, float u1, float v1, float u2, float v2) { g.image(image, x1, y1, x2, y2, u1, v1, u2, v2); } - public void cache(BImage image) { + public void cache(PImage image) { g.cache(image); } - public void cache(BImage images[]) { + public void cache(PImage images[]) { g.cache(images); } - public BFont loadFont(String name) { + public PFont loadFont(String name) { return g.loadFont(name); } - public void textFont(BFont which) { + public void textFont(PFont which) { g.textFont(which); } - public void textFont(BFont which, float size) { + public void textFont(PFont which, float size) { g.textFont(which, size); } @@ -3419,7 +2802,7 @@ public class BApplet extends Applet } - public void background(BImage image) { + public void background(PImage image) { g.background(image); } @@ -3480,7 +2863,7 @@ public class BApplet extends Applet static public byte[] loadBytes(InputStream input) { - return BGraphics.loadBytes(input); + return PGraphics.loadBytes(input); } @@ -3490,7 +2873,7 @@ public class BApplet extends Applet static public String[] loadStrings(InputStream input) { - return BGraphics.loadStrings(input); + return PGraphics.loadStrings(input); } diff --git a/core/PConstants.java b/core/PConstants.java index 832702199..73672b41e 100644 --- a/core/PConstants.java +++ b/core/PConstants.java @@ -1,12 +1,11 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BConstants - numbers shared throughout the bagel engine - Part of the Processing project - http://Proce55ing.net + PConstants - numbers shared throughout processing.core + Part of the Processing project - http://processing.org - Copyright (c) 2001-03 - Ben Fry, Massachusetts Institute of Technology and - Casey Reas, Interaction Design Institute Ivrea + Except where noted, code is written by Ben Fry and + Copyright (c) 2001-04 Massachusetts Institute of Technology This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -24,11 +23,13 @@ Boston, MA 02111-1307 USA */ +package processing.core; + import java.awt.Cursor; import java.awt.event.KeyEvent; -public interface BConstants { +public interface PConstants { // for better parity between c++ version (at no speed cost) diff --git a/core/PFont.java b/core/PFont.java index fce2122e5..5c9244c44 100644 --- a/core/PFont.java +++ b/core/PFont.java @@ -23,21 +23,17 @@ Boston, MA 02111-1307 USA */ +package processing.core; + import java.io.*; import java.util.*; -#ifdef JDK13 -// for font builder.. but prolly no need to ifdef -import java.awt.*; -import java.awt.image.*; -#endif - // value[] could be used to build a char to byte mapping table // as the font is loaded.. // when generating, use the native char mapping. -public class BFont implements BConstants { +public class PFont implements PConstants { /** * This is the union of the Mac Roman and Windows ANSI @@ -98,7 +94,7 @@ public class BFont implements BConstants { //int firstChar = 33; // always int charCount; - BImage images[]; + PImage images[]; // image width, a power of 2 // note! these will always be the same @@ -123,11 +119,11 @@ public class BFont implements BConstants { boolean cached; - public BFont() { } // for BFontAI subclass and font builder + public PFont() { } // for PFontAI subclass and font builder // can this throw an exception instead? - public BFont(String filename, BGraphics parent) throws IOException { + public PFont(String filename, PGraphics parent) throws IOException { //this.parent = parent; //this.valid = false; @@ -145,158 +141,9 @@ public class BFont implements BConstants { } cached = false; size(); - //valid = true; - - //} catch (IOException e) { - //parent.message(COMPLAINT, "could not load font " + filename, e); - //} } -#ifdef JDK13 - - public BFont(String name, int size) throws IOException { - this(new Font(name, Font.PLAIN, size), true); - } - - public BFont(String name, int size, boolean smooth) throws IOException { - this(new Font(name, Font.PLAIN, size), smooth); - } - - public BFont(Font font, boolean smooth) throws IOException { - //int firstChar = 33; - //int lastChar = 126; - - //this.charCount = lastChar - firstChar + 1; - this.charCount = charset.length; - this.mbox = font.getSize(); - - // size for image/texture is next power of 2 over font size - iwidth = iheight = (int) - Math.pow(2, Math.ceil(Math.log(mbox) / Math.log(2))); - - iwidthf = iheightf = (float) iwidth; - - /* - iwidth = (int) - Math.pow(2, Math.ceil(Math.log(mbox) / Math.log(2))); - iheight = (int) - Math.pow(2, Math.ceil(Math.log(mbox) / Math.log(2))); - - iwidthf = (float) iwidth; - iheightf = (float) iheight; - */ - - images = new BImage[charCount]; - - // allocate enough space for the character info - value = new int[charCount]; - height = new int[charCount]; - width = new int[charCount]; - setWidth = new int[charCount]; - topExtent = new int[charCount]; - leftExtent = new int[charCount]; - - int mbox3 = mbox * 3; - - BufferedImage playground = - new BufferedImage(mbox3, mbox3, BufferedImage.TYPE_INT_RGB); - - Graphics2D g = (Graphics2D) playground.getGraphics(); - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - smooth ? - RenderingHints.VALUE_ANTIALIAS_ON : - RenderingHints.VALUE_ANTIALIAS_OFF); - - g.setFont(font); - FontMetrics metrics = g.getFontMetrics(); - - int index = 0; - for (int i = 0; i < charCount; i++) { - char c = charset[i]; - if (!font.canDisplay(c)) { // skip chars not in the font - continue; - } - - g.setColor(Color.white); - g.fillRect(0, 0, mbox3, mbox3); - g.setColor(Color.black); - g.drawString(String.valueOf(c), mbox, mbox * 2); - - // grabs copy of the current data.. so no updates (do each time) - Raster raster = playground.getData(); - - //int w = metrics.charWidth(c); - int minX = 1000, maxX = 0; - int minY = 1000, maxY = 0; - boolean pixelFound = false; - - for (int y = 0; y < mbox3; y++) { - for (int x = 0; x < mbox3; x++) { - int sample = raster.getSample(x, y, 0); // maybe? - // or int samples[] = raster.getPixel(x, y, null); - - //if (sample == 0) { // or just not white? hmm - if (sample != 255) { - if (x < minX) minX = x; - if (y < minY) minY = y; - if (x > maxX) maxX = x; - if (y > maxY) maxY = y; - pixelFound = true; - //System.out.println(x + " " + y + " = " + sample); - } - } - } - - if (!pixelFound) { - //System.out.println("no pixels found in char " + ((char)i)); - // this was dumb that it was set to 20 & 30, because for small - // fonts, those guys don't exist - minX = minY = 0; //20; - maxX = maxY = 0; //30; - - // this will create a 1 pixel white (clear) character.. - // maybe better to set one to -1 so nothing is added? - } - - value[index] = c; - height[index] = (maxY - minY) + 1; - width[index] = (maxX - minX) + 1; - setWidth[index] = metrics.charWidth(c); - - // offset from vertical location of baseline - // of where the char was drawn (mbox*2) - topExtent[index] = mbox*2 - minY; - - // offset from left of where coord was drawn - leftExtent[index] = minX - mbox; - - //System.out.println(height[index] + " " + width[index] + " " + - // setWidth[index] + " " + - // topExtent[index] + " " + leftExtent[index]); - - images[index] = new BImage(new int[width[index] * height[index]], - width[index], height[index], ALPHA); - - for (int y = minY; y <= maxY; y++) { - for (int x = minX; x <= maxX; x++) { - //System.out.println("getting pixel " + x + " " + y); - int value = 255 - raster.getSample(x, y, 0); - int pindex = (y - minY) * width[index] + (x - minX); - images[index].pixels[pindex] = value; - //System.out.print(BApplet.nf(value, 3) + " "); - } - //System.out.println(); - } - //System.out.println(); - index++; - } - charCount = index; - } - -#endif - - public void write(OutputStream output) throws IOException { DataOutputStream os = new DataOutputStream(output); @@ -383,12 +230,12 @@ public class BFont implements BConstants { is.readInt(); } - images = new BImage[charCount]; + images = new PImage[charCount]; for (int i = 0; i < charCount; i++) { //int pixels[] = new int[64 * 64]; int pixels[] = new int[iwidth * iheight]; - //images[i] = new BImage(pixels, 64, 64, ALPHA); - images[i] = new BImage(pixels, iwidth, iheight, ALPHA); + //images[i] = new PImage(pixels, 64, 64, ALPHA); + images[i] = new PImage(pixels, iwidth, iheight, ALPHA); int bitmapSize = height[i] * width[i]; byte temp[] = new byte[bitmapSize]; @@ -411,21 +258,13 @@ public class BFont implements BConstants { } //System.out.println(); } - //kind = VLW; } - //boolean exists(char c) { - //return ((c >= firstChar) && (c - firstChar < charCount)); - //} - - /** * Get index for the char (convert from unicode to bagel charset). * @return index into arrays or -1 if not found */ - //static private int index(char c) { - //static public int index(char c) { public int index(char c) { // these chars required in all fonts if ((c >= 33) && (c <= 126)) { @@ -440,7 +279,6 @@ public class BFont implements BConstants { // whups, this used the p5 charset rather than what was inside the font // meaning that old fonts would crash.. fixed for 0069 - //static private int index_hunt(int c, int start, int stop) { private int index_hunt(int c, int start, int stop) { //System.out.println("checking between " + start + " and " + stop); int pivot = (start + stop) / 2; @@ -526,12 +364,12 @@ public class BFont implements BConstants { } - public void text(char c, float x, float y, BGraphics parent) { + public void text(char c, float x, float y, PGraphics parent) { text(c, x, y, 0, parent); } - public void text(char c, float x, float y, float z, BGraphics parent) { + public void text(char c, float x, float y, float z, PGraphics parent) { //if (!valid) return; //if (!exists(c)) return; @@ -676,11 +514,11 @@ public class BFont implements BConstants { private char c[] = new char[8192]; - public void text(String str, float x, float y, BGraphics parent) { + public void text(String str, float x, float y, PGraphics parent) { text(str, x, y, 0, parent); } - public void text(String str, float x, float y, float z, BGraphics parent) { + public void text(String str, float x, float y, float z, PGraphics parent) { float startX = x; int index = 0; char previous = 0; diff --git a/core/PFont2.java b/core/PFont2.java new file mode 100644 index 000000000..a23c70058 --- /dev/null +++ b/core/PFont2.java @@ -0,0 +1,151 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +package processing.core; + +import java.io.*; +import java.util.*; +import java.awt.*; +import java.awt.image.*; + + +public class PFont2 extends PFont { + + public PFont2(String name, int size) throws IOException { + this(new Font(name, Font.PLAIN, size), true); + } + + public PFont2(String name, int size, boolean smooth) throws IOException { + this(new Font(name, Font.PLAIN, size), smooth); + } + + public PFont2(Font font, boolean smooth) throws IOException { + //int firstChar = 33; + //int lastChar = 126; + + //this.charCount = lastChar - firstChar + 1; + this.charCount = charset.length; + this.mbox = font.getSize(); + + // size for image/texture is next power of 2 over font size + iwidth = iheight = (int) + Math.pow(2, Math.ceil(Math.log(mbox) / Math.log(2))); + + iwidthf = iheightf = (float) iwidth; + + /* + iwidth = (int) + Math.pow(2, Math.ceil(Math.log(mbox) / Math.log(2))); + iheight = (int) + Math.pow(2, Math.ceil(Math.log(mbox) / Math.log(2))); + + iwidthf = (float) iwidth; + iheightf = (float) iheight; + */ + + images = new PImage[charCount]; + + // allocate enough space for the character info + value = new int[charCount]; + height = new int[charCount]; + width = new int[charCount]; + setWidth = new int[charCount]; + topExtent = new int[charCount]; + leftExtent = new int[charCount]; + + int mbox3 = mbox * 3; + + BufferedImage playground = + new BufferedImage(mbox3, mbox3, BufferedImage.TYPE_INT_RGB); + + Graphics2D g = (Graphics2D) playground.getGraphics(); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + smooth ? + RenderingHints.VALUE_ANTIALIAS_ON : + RenderingHints.VALUE_ANTIALIAS_OFF); + + g.setFont(font); + FontMetrics metrics = g.getFontMetrics(); + + int index = 0; + for (int i = 0; i < charCount; i++) { + char c = charset[i]; + if (!font.canDisplay(c)) { // skip chars not in the font + continue; + } + + g.setColor(Color.white); + g.fillRect(0, 0, mbox3, mbox3); + g.setColor(Color.black); + g.drawString(String.valueOf(c), mbox, mbox * 2); + + // grabs copy of the current data.. so no updates (do each time) + Raster raster = playground.getData(); + + //int w = metrics.charWidth(c); + int minX = 1000, maxX = 0; + int minY = 1000, maxY = 0; + boolean pixelFound = false; + + for (int y = 0; y < mbox3; y++) { + for (int x = 0; x < mbox3; x++) { + int sample = raster.getSample(x, y, 0); // maybe? + // or int samples[] = raster.getPixel(x, y, null); + + //if (sample == 0) { // or just not white? hmm + if (sample != 255) { + if (x < minX) minX = x; + if (y < minY) minY = y; + if (x > maxX) maxX = x; + if (y > maxY) maxY = y; + pixelFound = true; + //System.out.println(x + " " + y + " = " + sample); + } + } + } + + if (!pixelFound) { + //System.out.println("no pixels found in char " + ((char)i)); + // this was dumb that it was set to 20 & 30, because for small + // fonts, those guys don't exist + minX = minY = 0; //20; + maxX = maxY = 0; //30; + + // this will create a 1 pixel white (clear) character.. + // maybe better to set one to -1 so nothing is added? + } + + value[index] = c; + height[index] = (maxY - minY) + 1; + width[index] = (maxX - minX) + 1; + setWidth[index] = metrics.charWidth(c); + + // offset from vertical location of baseline + // of where the char was drawn (mbox*2) + topExtent[index] = mbox*2 - minY; + + // offset from left of where coord was drawn + leftExtent[index] = minX - mbox; + + //System.out.println(height[index] + " " + width[index] + " " + + // setWidth[index] + " " + + // topExtent[index] + " " + leftExtent[index]); + + images[index] = new PImage(new int[width[index] * height[index]], + width[index], height[index], ALPHA); + + for (int y = minY; y <= maxY; y++) { + for (int x = minX; x <= maxX; x++) { + //System.out.println("getting pixel " + x + " " + y); + int value = 255 - raster.getSample(x, y, 0); + int pindex = (y - minY) * width[index] + (x - minX); + images[index].pixels[pindex] = value; + //System.out.print(BApplet.nf(value, 3) + " "); + } + //System.out.println(); + } + //System.out.println(); + index++; + } + charCount = index; + } +} \ No newline at end of file diff --git a/core/PGraphics.java b/core/PGraphics.java index 146484cf2..5d0372568 100644 --- a/core/PGraphics.java +++ b/core/PGraphics.java @@ -1,10 +1,10 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BGraphics - main graphics and rendering context for bagel + PGraphics - main graphics and rendering context for bagel Part of the Processing project - http://processing.org - Copyright (c) 2001-03 Massachusetts Institute of Technology + Copyright (c) 2001-04 Massachusetts Institute of Technology (Except where noted that the author is not Ben Fry) This library is free software; you can redistribute it and/or @@ -23,6 +23,8 @@ Boston, MA 02111-1307 USA */ +package processing.core; + import java.applet.*; import java.awt.*; import java.awt.event.*; @@ -32,7 +34,7 @@ import java.net.*; import java.util.zip.*; -public class BGraphics extends BImage implements BConstants { +public class PGraphics extends PImage implements PConstants { public Applet applet; // not comfortable with this being static // ........................................................ @@ -131,7 +133,7 @@ public class BGraphics extends BImage implements BConstants { float lightX[], lightY[], lightZ[]; int lightKind[]; - // inherited from BImage + // inherited from PImage //boolean smooth = false; // antialiasing // projection @@ -183,12 +185,12 @@ public class BGraphics extends BImage implements BConstants { // OLD_GRAPHICS - BPolygon polygon; // general polygon to use for shape - BPolygon fpolygon; // used to fill polys for tri or quad strips - BPolygon spolygon; // stroke/line polygon + PPolygon polygon; // general polygon to use for shape + PPolygon fpolygon; // used to fill polys for tri or quad strips + PPolygon spolygon; // stroke/line polygon float svertices[][]; // temp vertices used for stroking end of poly - BPolygon tpolygon; // for calculating concave/convex + PPolygon tpolygon; // for calculating concave/convex int TPOLYGON_MAX_VERTICES = 512; int tpolygon_vertex_order[]; // = new int[MAX_VERTICES]; @@ -210,13 +212,13 @@ public class BGraphics extends BImage implements BConstants { // lines static final int DEFAULT_LINES = 512; - BLine line; // used for drawing + PLine line; // used for drawing int lines[][] = new int[DEFAULT_LINES][LINE_FIELD_COUNT]; int lines_count; // triangles static final int DEFAULT_TRIANGLES = 256; - BTriangle triangle; // used for rendering + PTriangle triangle; // used for rendering int triangles[][] = new int[DEFAULT_TRIANGLES][TRIANGLE_FIELD_COUNT]; int triangles_count; // total number of triangles @@ -242,9 +244,9 @@ public class BGraphics extends BImage implements BConstants { boolean texture; // NEW_GRAPHICS - private BImage textureImage; + private PImage textureImage; static final int DEFAULT_TEXTURES = 3; - BImage textures[] = new BImage[DEFAULT_TEXTURES]; + PImage textures[] = new PImage[DEFAULT_TEXTURES]; int texture_index; @@ -305,7 +307,7 @@ public class BGraphics extends BImage implements BConstants { int text_mode = ALIGN_LEFT; int text_space = OBJECT_SPACE; - BFont text_font; + PFont text_font; boolean drawing_text = false; @@ -315,20 +317,20 @@ public class BGraphics extends BImage implements BConstants { /** - * Constructor for the BGraphics object. + * Constructor for the PGraphics object. * This prototype only exists because of annoying * java compilers, and should not be used. */ - public BGraphics() { } + public PGraphics() { } /** - * Constructor for the BGraphics object + * Constructor for the PGraphics object * * @param iwidth viewport width * @param iheight viewport height */ - public BGraphics(int iwidth, int iheight) { + public PGraphics(int iwidth, int iheight) { resize(iwidth, iheight); // init color/stroke/fill @@ -400,12 +402,12 @@ public class BGraphics extends BImage implements BConstants { image = Toolkit.getDefaultToolkit().createImage(mis); // use now in the old engine too - line = new BLine(this); + line = new PLine(this); - // moved from BGraphics constructor since not needed by opengl + // moved from PGraphics constructor since not needed by opengl if (hints[NEW_GRAPHICS]) { - //line = new BLine(this); - triangle = new BTriangle(this); + //line = new PLine(this); + triangle = new PTriangle(this); /* // these are all done on beginFrame(), so not doing them here [fry] @@ -419,11 +421,11 @@ public class BGraphics extends BImage implements BConstants { vertex_end = 0; // init lines - line = new BLine(this); + line = new PLine(this); lines_count = 0; // init triangles - triangle = new BTriangle(this); + triangle = new PTriangle(this); triangles_count = 0; // textures @@ -450,9 +452,9 @@ public class BGraphics extends BImage implements BConstants { shapeKind = 0; if (!hints[NEW_GRAPHICS]) { - polygon = new BPolygon(this); - fpolygon = new BPolygon(this); - spolygon = new BPolygon(this); + polygon = new PPolygon(this); + fpolygon = new PPolygon(this); + spolygon = new PPolygon(this); spolygon.vertexCount = 4; svertices = new float[2][]; } @@ -506,7 +508,7 @@ public class BGraphics extends BImage implements BConstants { * initializes engine before drawing a new frame */ public void beginFrame() { - if (BApplet.THREAD_DEBUG) System.out.println(" 1 beginFrame"); + if (PApplet.THREAD_DEBUG) System.out.println(" 1 beginFrame"); /* if (camera_mode == -1) { //System.out.println("setting up camera"); @@ -558,7 +560,7 @@ public class BGraphics extends BImage implements BConstants { * indicates a completed frame */ public void endFrame() { - if (BApplet.THREAD_DEBUG) System.out.println(" 2 endFrame"); + if (PApplet.THREAD_DEBUG) System.out.println(" 2 endFrame"); if (hints[NEW_GRAPHICS]) { @@ -654,10 +656,10 @@ public class BGraphics extends BImage implements BConstants { } - public final void addTexture(BImage image) { + public final void addTexture(PImage image) { if (texture_index == textures.length - 1) { - BImage temp[] = new BImage[texture_index<<1]; + PImage temp[] = new PImage[texture_index<<1]; System.arraycopy(textures, 0, temp, 0, texture_index); textures = temp; System.out.println("allocating more textures " + textures.length); @@ -977,10 +979,10 @@ public class BGraphics extends BImage implements BConstants { * set texture image for current shape * needs to be called between @see beginShape and @see endShape * - * @param image reference to a BImage object + * @param image reference to a PImage object */ - //public void textureImage(BImage image) { - public void texture(BImage image) { + //public void textureImage(PImage image) { + public void texture(PImage image) { if (hints[NEW_GRAPHICS]) { if (z_order == true) { addTexture(image); @@ -2281,7 +2283,7 @@ public class BGraphics extends BImage implements BConstants { if (tpolygon == null) { // allocate on first use, rather than slowing // the startup of the class. - tpolygon = new BPolygon(this); + tpolygon = new PPolygon(this); tpolygon_vertex_order = new int[TPOLYGON_MAX_VERTICES]; } tpolygon.reset(3); @@ -3187,7 +3189,7 @@ public class BGraphics extends BImage implements BConstants { * @param sx1 x coordinate of upper-lefthand corner in screen space * @param sy1 y coordinate of upper-lefthand corner in screen space */ - public void flat_image(BImage image, int sx1, int sy1) { + public void flat_image(PImage image, int sx1, int sy1) { int ix1 = 0; int iy1 = 0; int ix2 = image.width; @@ -4069,7 +4071,7 @@ public class BGraphics extends BImage implements BConstants { } } - public BImage loadImage(String filename) { + public PImage loadImage(String filename) { if (filename.toLowerCase().endsWith(".tga")) { return loadTargaImage(filename); } @@ -4077,7 +4079,7 @@ public class BGraphics extends BImage implements BConstants { } // returns null if no image of that name is found - public BImage loadImage(String filename, boolean force) { + public PImage loadImage(String filename, boolean force) { Image awtimage = null; //String randomizer = "?" + nf((int) (random()*10000), 4); @@ -4165,18 +4167,18 @@ public class BGraphics extends BImage implements BConstants { // since transparency is often at corners, hopefully this // will find a non-transparent pixel quickly and exit if ((jpixels[i] & 0xff000000) != 0xff000000) { - return new BImage(jpixels, jwidth, jheight, RGBA); + return new PImage(jpixels, jwidth, jheight, RGBA); //format = RGBA; //break; } } } - return new BImage(jpixels, jwidth, jheight, RGB); + return new PImage(jpixels, jwidth, jheight, RGB); } /* - public BImage loadImage(String file) { + public PImage loadImage(String file) { try { byte[] imgarray=loadBytes(file); java.awt.Image awtimage = @@ -4201,7 +4203,7 @@ public class BGraphics extends BImage implements BConstants { e.printStackTrace(); } - BImage img=new BImage(pix,w,h,RGB); + PImage img=new PImage(pix,w,h,RGB); if (file.toLowerCase().endsWith(".gif")) { for (int i = 0; i < pix.length; i++) { @@ -4225,7 +4227,7 @@ public class BGraphics extends BImage implements BConstants { // [fry] this could be optimized to not use loadBytes // which would help out memory situations with large images - protected BImage loadTargaImage(String filename) { + protected PImage loadTargaImage(String filename) { // load image file as byte array byte[] buffer = loadBytes(filename); @@ -4240,7 +4242,7 @@ public class BGraphics extends BImage implements BConstants { boolean hasAlpha=(buffer[16] == 32); // setup new image object - BImage img = new BImage(w,h); + PImage img = new PImage(w,h); img.format = (hasAlpha ? RGBA : RGB); // targa's are written upside down, so we need to parse it in reverse @@ -4275,7 +4277,7 @@ public class BGraphics extends BImage implements BConstants { image_mode = mode; } - public void image(BImage image, float x1, float y1) { + public void image(PImage image, float x1, float y1) { if ((dimensions == 0) && !lighting && !_tint && (image_mode != CENTER_RADIUS)) { // if drawing a flat image with no warping, @@ -4289,13 +4291,13 @@ public class BGraphics extends BImage implements BConstants { } - public void image(BImage image, + public void image(PImage image, float x1, float y1, float x2, float y2) { image(image, x1, y1, x2, y2, 0, 0, image.width, image.height); } - public void image(BImage image, + public void image(PImage image, float x1, float y1, float x2, float y2, float u1, float v1, float u2, float v2) { switch (image_mode) { @@ -4361,13 +4363,13 @@ public class BGraphics extends BImage implements BConstants { } - public void cache(BImage image) { + public void cache(PImage image) { } - public void cache(BImage images[]) { + public void cache(PImage images[]) { } - protected void cache(BImage image, int index) { + protected void cache(PImage image, int index) { } @@ -4376,9 +4378,9 @@ public class BGraphics extends BImage implements BConstants { // TEXT/FONTS - public BFont loadFont(String name) { + public PFont loadFont(String name) { try { - BFont font = new BFont(name, this); + PFont font = new PFont(name, this); return font; } catch (IOException e) { @@ -4391,7 +4393,7 @@ public class BGraphics extends BImage implements BConstants { } - public void textFont(BFont which) { + public void textFont(PFont which) { if (which == null) { System.err.println("Ignoring improperly loaded font in textFont()"); return; @@ -4405,7 +4407,7 @@ public class BGraphics extends BImage implements BConstants { text_font.leading(); } - public void textFont(BFont which, float size) { + public void textFont(PFont which, float size) { if (which == null) { System.err.println("Ignoring improperly loaded font in textFont()"); return; @@ -4502,7 +4504,7 @@ public class BGraphics extends BImage implements BConstants { public void text(float num, float x, float y) { - text(BApplet.nfs(num, 0, 3), x, y, 0); + text(PApplet.nfs(num, 0, 3), x, y, 0); } /** @@ -4511,7 +4513,7 @@ public class BGraphics extends BImage implements BConstants { * Users who want more control should use their own nfs() cmmand. */ public void text(float num, float x, float y, float z) { - text(BApplet.nfs(num, 0, 3), x, y, z); + text(PApplet.nfs(num, 0, 3), x, y, z); } @@ -4606,19 +4608,19 @@ public class BGraphics extends BImage implements BConstants { int d = 1; while ((big /= 10) != 0) d++; - BApplet.println(BApplet.nfs(m00, d, 4) + " " + BApplet.nfs(m01, d, 4) + " " + - BApplet.nfs(m02, d, 4) + " " + BApplet.nfs(m03, d, 4)); + PApplet.println(PApplet.nfs(m00, d, 4) + " " + PApplet.nfs(m01, d, 4) + " " + + PApplet.nfs(m02, d, 4) + " " + PApplet.nfs(m03, d, 4)); - BApplet.println(BApplet.nfs(m10, d, 4) + " " + BApplet.nfs(m11, d, 4) + " " + - BApplet.nfs(m12, d, 4) + " " + BApplet.nfs(m13, d, 4)); + PApplet.println(PApplet.nfs(m10, d, 4) + " " + PApplet.nfs(m11, d, 4) + " " + + PApplet.nfs(m12, d, 4) + " " + PApplet.nfs(m13, d, 4)); - BApplet.println(BApplet.nfs(m20, d, 4) + " " + BApplet.nfs(m21, d, 4) + " " + - BApplet.nfs(m22, d, 4) + " " + BApplet.nfs(m23, d, 4)); + PApplet.println(PApplet.nfs(m20, d, 4) + " " + PApplet.nfs(m21, d, 4) + " " + + PApplet.nfs(m22, d, 4) + " " + PApplet.nfs(m23, d, 4)); - BApplet.println(BApplet.nfs(m30, d, 4) + " " + BApplet.nfs(m31, d, 4) + " " + - BApplet.nfs(m32, d, 4) + " " + BApplet.nfs(m33, d, 4)); + PApplet.println(PApplet.nfs(m30, d, 4) + " " + PApplet.nfs(m31, d, 4) + " " + + PApplet.nfs(m32, d, 4) + " " + PApplet.nfs(m33, d, 4)); - BApplet.println(); + PApplet.println(); } @@ -4662,19 +4664,19 @@ public class BGraphics extends BImage implements BConstants { int d = 1; while ((big /= 10) != 0) d++; - BApplet.println(BApplet.nfs(p00, d, 4) + " " + BApplet.nfs(p01, d, 4) + " " + - BApplet.nfs(p02, d, 4) + " " + BApplet.nfs(p03, d, 4)); + PApplet.println(PApplet.nfs(p00, d, 4) + " " + PApplet.nfs(p01, d, 4) + " " + + PApplet.nfs(p02, d, 4) + " " + PApplet.nfs(p03, d, 4)); - BApplet.println(BApplet.nfs(p10, d, 4) + " " + BApplet.nfs(p11, d, 4) + " " + - BApplet.nfs(p12, d, 4) + " " + BApplet.nfs(p13, d, 4)); + PApplet.println(PApplet.nfs(p10, d, 4) + " " + PApplet.nfs(p11, d, 4) + " " + + PApplet.nfs(p12, d, 4) + " " + PApplet.nfs(p13, d, 4)); - BApplet.println(BApplet.nfs(p20, d, 4) + " " + BApplet.nfs(p21, d, 4) + " " + - BApplet.nfs(p22, d, 4) + " " + BApplet.nfs(p23, d, 4)); + PApplet.println(PApplet.nfs(p20, d, 4) + " " + PApplet.nfs(p21, d, 4) + " " + + PApplet.nfs(p22, d, 4) + " " + PApplet.nfs(p23, d, 4)); - BApplet.println(BApplet.nfs(p30, d, 4) + " " + BApplet.nfs(p31, d, 4) + " " + - BApplet.nfs(p32, d, 4) + " " + BApplet.nfs(p33, d, 4)); + PApplet.println(PApplet.nfs(p30, d, 4) + " " + PApplet.nfs(p31, d, 4) + " " + + PApplet.nfs(p32, d, 4) + " " + PApplet.nfs(p33, d, 4)); - BApplet.println(); + PApplet.println(); } @@ -5375,7 +5377,7 @@ public class BGraphics extends BImage implements BConstants { } - public void background(BImage image) { + public void background(PImage image) { if ((image.width != width) || (image.height != height)) { System.err.println("background image must be the same size " + "as your application"); @@ -5969,10 +5971,10 @@ public class BGraphics extends BImage implements BConstants { // MATH // these are *only* the functions used internally - // the real math functions are inside BApplet + // the real math functions are inside PApplet // these have been made private so as not to conflict - // with the versions found in BApplet when fxn importing happens + // with the versions found in PApplet when fxn importing happens // also might be faster that way. hmm. diff --git a/core/PImage.java b/core/PImage.java index 2b0748034..d914a0bf9 100644 --- a/core/PImage.java +++ b/core/PImage.java @@ -1,7 +1,7 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BImage - storage class for pixel data + PImage - storage class for pixel data Part of the Processing project - http://Proce55ing.net Copyright (c) 2001-03 @@ -24,6 +24,8 @@ Boston, MA 02111-1307 USA */ +package processing.core; + import java.awt.*; import java.awt.image.*; import java.io.*; @@ -56,9 +58,9 @@ import java.io.*; * image object as parameter. this is to provide an easy syntax for cases * where the main pixel buffer is the destination. as those methods are * overloaded in BApplet, users can call those functions directly without - * explicitly giving a reference to BGraphics. + * explicitly giving a reference to PGraphics. */ -public class BImage implements BConstants, Cloneable { +public class PImage implements PConstants, Cloneable { // note that RGB images still require 0xff in the high byte // because of how they'll be manipulated by other functions @@ -66,20 +68,15 @@ public class BImage implements BConstants, Cloneable { int pixels[]; int width, height; - // maybe also scan line, etc? + // would scan line be useful? maybe for pow of 2 gl textures - // note! inherited by BGraphics + // note! inherited by PGraphics boolean smooth = false; //true; // for now.. how to fix? // for gl subclass / hardware accel int cacheIndex; - // blend mode used for copy et al. - //int blend_mode = REPLACE; - - // private fields - private int fracU, ifU, fracV, ifV, u1, u2, v1, v2, sX, sY, iw, iw1, ih1; private int ul, ll, ur, lr, cUL, cLL, cUR, cLR; private int srcXOffset, srcYOffset; @@ -97,7 +94,7 @@ public class BImage implements BConstants, Cloneable { /** * Constructor required by java compiler for subclasses. */ - public BImage() { } + public PImage() { } /** @@ -105,7 +102,7 @@ public class BImage implements BConstants, Cloneable { * All pixels are set to zero, meaning black, but since the * alpha is zero, it will be transparent. */ - public BImage(int width, int height) { + public PImage(int width, int height) { this(new int[width * height], width, height, RGBA); // toxi: is it maybe better to init the image with max alpha enabled? //for(int i=0; i>16&0xff) + 151*(col>>8&0xff) + 28*(col&0xff) )>>8; - pixels[i] = (col & ALPHA_MASK) | lum<<16 | lum<<8 | lum; - } + /** + * [toxi040115] + * Converts RGB image data into grayscale using + * weighted RGB components, and keeps alpha channel intact. + */ + public void toGrayscale() { + for (int i = 0; i>16&0xff) + 151*(col>>8&0xff) + 28*(col&0xff))>>8; + pixels[i] = (col & ALPHA_MASK) | lum<<16 | lum<<8 | lum; } + } + + ////////////////////////////////////////////////////////////// @@ -223,14 +224,14 @@ public class BImage implements BConstants, Cloneable { } - public BImage get(int x, int y, int w, int h) { + public PImage get(int x, int y, int w, int h) { if (x < 0) x = 0; if (y < 0) y = 0; if (x + w > width) w = width - x; if (y + h > height) h = height - y; - BImage newbie = new BImage(new int[w*h], w, h, format); + PImage newbie = new PImage(new int[w*h], w, h, format); int index = y*width + x; int index2 = 0; @@ -258,7 +259,7 @@ public class BImage implements BConstants, Cloneable { // not all variables here are needed.. fix it up later - public void set(int x, int y, BImage image) { + public void set(int x, int y, PImage image) { // source int sx = 0; int sy = 0; @@ -325,7 +326,7 @@ public class BImage implements BConstants, Cloneable { // this function is excluded from using any blend modes // it always replaces/overwrites the target pixel value - public void copy(BImage src, int sx, int sy, int dx, int dy) { + public void copy(PImage src, int sx, int sy, int dx, int dy) { if ((dx >= 0) && (dx < width) && (sx >= 0) && (sx < src.width) && (dy >= 0) && (dy < height) && (sy >= 0) && (sy < src.height)) { pixels[dy * width + dx] = src.pixels[sy * src.width + sx]; @@ -350,9 +351,9 @@ public class BImage implements BConstants, Cloneable { /** - * Copies area of one image into another BImage object + * Copies area of one image into another PImage object */ - public void copy(BImage src, int sx1, int sy1, int sx2, int sy2, + public void copy(PImage src, int sx1, int sy1, int sx2, int sy2, int dx1, int dy1, int dx2, int dy2) { blit_resize(src, sx1, sy1, sx2, sy2, pixels, width, height, dx1, dy1, dx2, dy2, REPLACE); @@ -362,7 +363,7 @@ public class BImage implements BConstants, Cloneable { /** * Copies and blends 1 pixel with MODE to pixel in another image */ - public void blend(BImage src, int sx, int sy, int dx, int dy, int mode) { + public void blend(PImage src, int sx, int sy, int dx, int dy, int mode) { if ((dx >= 0) && (dx < width) && (sx >= 0) && (sx < src.width) && (dy >= 0) && (dy < height) && (sy >= 0) && (sy < src.height)) { pixels[dy * width + dx] = @@ -395,9 +396,9 @@ public class BImage implements BConstants, Cloneable { /** - * Copies area of one image into another BImage object + * Copies area of one image into another PImage object */ - public void blend(BImage src, int sx1, int sy1, int sx2, int sy2, + public void blend(PImage src, int sx1, int sy1, int sx2, int sy2, int dx1, int dy1, int dx2, int dy2, int mode) { blit_resize(src, sx1, sy1, sx2, sy2, pixels, width, height, dx1, dy1, dx2, dy2, mode); @@ -410,7 +411,7 @@ public class BImage implements BConstants, Cloneable { // COPYING IMAGE DATA public Object clone() throws CloneNotSupportedException { - BImage c = (BImage) super.clone(); + PImage c = (PImage) super.clone(); // super.clone() will only copy the reference to the pixels // array, so this will do a proper duplication of it instead. @@ -426,8 +427,8 @@ public class BImage implements BConstants, Cloneable { * Duplicate an image, returns new object */ /* - public BImage duplicate() { - BImage c = new BImage(new int[pixels.length], width, height, format); + public PImage duplicate() { + PImage c = new PImage(new int[pixels.length], width, height, format); System.arraycopy(pixels, 0, c.pixels, 0, pixels.length); return c; } @@ -437,8 +438,8 @@ public class BImage implements BConstants, Cloneable { * Duplicate and resize image */ /* - public BImage duplicate(int newWidth, int newHeight) { - BImage dupe = new BImage(new int[newWidth * newHeight], + public PImage duplicate(int newWidth, int newHeight) { + PImage dupe = new PImage(new int[newWidth * newHeight], newWidth, newHeight, format); dupe.copy(this, 0, 0, width - 1, height - 1, @@ -491,7 +492,7 @@ public class BImage implements BConstants, Cloneable { // uses bilinear filtering if smooth() has been enabled // 'mode' determines the blending mode used in the process - private void blit_resize(BImage img, + private void blit_resize(PImage img, int srcX1, int srcY1, int srcX2, int srcY2, int[] destPixels, int screenW, int screenH, int destX1, int destY1, int destX2, int destY2, diff --git a/core/PLibrary.java b/core/PLibrary.java index 6025bb4f1..3e639db3a 100644 --- a/core/PLibrary.java +++ b/core/PLibrary.java @@ -1,7 +1,7 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BLibrary - interface for classes that plug into bagel + PLibrary - interface for classes that plug into bagel Part of the Processing project - http://Proce55ing.net Copyright (c) 2001-03 @@ -25,7 +25,10 @@ */ -public interface BLibrary { +package processing.core; + + +public interface PLibrary { // called when the applet is stopped public void stop(); @@ -34,7 +37,7 @@ public interface BLibrary { /* -public void libraryEvent(BLibrary who, Object data) { +public void libraryEvent(PLibrary who, Object data) { //if (who instanceof BVideo) { if (who.signature() == Sonia.SIGNATURE) { BImage frame = (BImage)data; diff --git a/core/PLine.java b/core/PLine.java index 27097503a..b425c6299 100644 --- a/core/PLine.java +++ b/core/PLine.java @@ -1,8 +1,8 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BFont - font object for text rendering - Part of the Processing project - http://Proce55ing.net + PLine - code for rendering lines + Part of the Processing project - http://processing.org Copyright (c) 2001-03 Ben Fry, Massachusetts Institute of Technology and @@ -24,8 +24,9 @@ Boston, MA 02111-1307 USA */ +package processing.core; -public class BLine implements BConstants +public class PLine implements PConstants { private int[] m_pixels; @@ -97,10 +98,10 @@ public class BLine implements BConstants //int x[] = new int[2]; //int y[] = new int[2]; - private BGraphics parent; + private PGraphics parent; - public BLine(BGraphics g) + public PLine(PGraphics g) { //SCREEN_WIDTH = g.width; //SCREEN_HEIGHT = g.height; @@ -126,7 +127,7 @@ public class BLine implements BConstants public void reset() { - // reset these in case BGraphics was resized + // reset these in case PGraphics was resized SCREEN_WIDTH = parent.width; SCREEN_HEIGHT = parent.height; diff --git a/core/PPolygon.java b/core/PPolygon.java index 0583906b5..65b03dcf8 100644 --- a/core/PPolygon.java +++ b/core/PPolygon.java @@ -1,7 +1,7 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BPolygon - zbuffer polygon rendering object for BGraphics + PPolygon - zbuffer polygon rendering object for BGraphics Part of the Processing project - http://Proce55ing.net Copyright (c) 2001-03 @@ -24,8 +24,10 @@ Boston, MA 02111-1307 USA */ +package processing.core; -public class BPolygon implements BConstants { + +public class PPolygon implements PConstants { static final int DEFAULT_SIZE = 64; // this is needed for spheres float vertices[][] = new float[DEFAULT_SIZE][VERTEX_FIELD_COUNT]; int vertexCount; @@ -55,7 +57,7 @@ public class BPolygon implements BConstants { float zbuffer[]; boolean noDepthTest; - BGraphics parent; + PGraphics parent; int pixels[]; // the parent's width/height, @@ -64,7 +66,7 @@ public class BPolygon implements BConstants { int width, height; int width1, height1; - BImage timage; + PImage timage; int tpixels[]; int theight, twidth; int theight1, twidth1; @@ -95,7 +97,7 @@ public class BPolygon implements BConstants { } - public BPolygon(BGraphics iparent) { + public PPolygon(PGraphics iparent) { parent = iparent; reset(0); } @@ -130,7 +132,7 @@ public class BPolygon implements BConstants { } - public void texture(BImage image) { + public void texture(PImage image) { this.timage = image; this.tpixels = image.pixels; this.twidth = image.width; diff --git a/core/PTriangle.java b/core/PTriangle.java index 99af5b6e4..0c3924fde 100644 --- a/core/PTriangle.java +++ b/core/PTriangle.java @@ -1,8 +1,8 @@ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* - BFont - font object for text rendering - Part of the Processing project - http://Proce55ing.net + PTriangle - handler for tesselated triangle rendering + Part of the Processing project - http://processing.org Copyright (c) 2001-03 Ben Fry, Massachusetts Institute of Technology and @@ -24,9 +24,12 @@ Boston, MA 02111-1307 USA */ -// written by sami www.sumea.com +package processing.core; -public class BTriangle implements BConstants +/** + * written by sami www.sumea.com + */ +public class PTriangle implements PConstants { static final int R_GOURAUD = 0x1; static final int R_TEXTURE8 = 0x2; @@ -41,7 +44,7 @@ public class BTriangle implements BConstants private float[] m_zbuffer; // texture image - private BImage m_tImage; + private PImage m_tImage; // private int SCREEN_WIDTH; @@ -182,7 +185,7 @@ public class BTriangle implements BConstants private int m_index; /** */ - private BGraphics parent; + private PGraphics parent; /** */ private boolean m_culling; @@ -193,7 +196,7 @@ public class BTriangle implements BConstants /** */ private boolean m_bilinear; - public BTriangle(BGraphics g) { + public PTriangle(PGraphics g) { //SCREEN_WIDTH = g.width; //SCREEN_HEIGHT = g.height; //SCREEN_WIDTH1 = SCREEN_WIDTH-1; @@ -221,7 +224,7 @@ public class BTriangle implements BConstants * Resets polygon attributes */ public void reset() { - // reset these in case BGraphics was resized + // reset these in case PGraphics was resized SCREEN_WIDTH = parent.width; SCREEN_HEIGHT = parent.height; @@ -338,7 +341,7 @@ public class BTriangle implements BConstants /** * Sets texture image used for the polygon */ - public void setTexture(BImage image) { + public void setTexture(PImage image) { m_tImage = image; m_texture = image.pixels; TEX_WIDTH = image.width; @@ -394,7 +397,7 @@ public class BTriangle implements BConstants * Renders the polygon */ public void render() { - // removed. done in BGraphics [rocha] + // removed. done in PGraphics [rocha] // increase polygon offset //m_index = (m_index + 1) & 0xFFFFFFF;