From e0875266f6cdf7fdb006b5822349515b1ea4d155 Mon Sep 17 00:00:00 2001 From: benfry Date: Mon, 14 Feb 2005 04:50:28 +0000 Subject: [PATCH] working on java2d graphics --- processing/core/PConstants.java | 18 ++- processing/core/PGraphics2.java | 237 +++++++++++++++++++++++++++++--- processing/core/PImage.java | 104 +------------- 3 files changed, 230 insertions(+), 129 deletions(-) diff --git a/processing/core/PConstants.java b/processing/core/PConstants.java index 97f2b766f..1465300a0 100644 --- a/processing/core/PConstants.java +++ b/processing/core/PConstants.java @@ -189,17 +189,15 @@ public interface PConstants { // stroke modes - static final int SQUARE_ENDCAP = 1 << 0; - static final int ROUND_ENDCAP = 1 << 1; - static final int PROJECTED_ENDCAP = 1 << 2; - static final int STROKE_CAP_MASK = - SQUARE_ENDCAP | ROUND_ENDCAP | PROJECTED_ENDCAP; + static final int SQUARE = 1 << 0; + static final int ROUND = 1 << 1; + static final int PROJECTED = 1 << 2; + static final int CAP_MASK = SQUARE | ROUND | PROJECTED; - static final int MITERED_JOIN = 1 << 3; - static final int ROUND_JOIN = 1 << 4; - static final int BEVELED_JOIN = 1 << 5; - static final int STROKE_JOIN_MASK = - MITERED_JOIN | ROUND_JOIN | BEVELED_JOIN; + static final int MITERED = 1 << 3; + static final int ROUND = 1 << 4; + static final int BEVELED = 1 << 5; + static final int JOIN_MASK = MITERED | ROUND | BEVELED; // lighting diff --git a/processing/core/PGraphics2.java b/processing/core/PGraphics2.java index 42395d7fb..9c3fa18c7 100644 --- a/processing/core/PGraphics2.java +++ b/processing/core/PGraphics2.java @@ -357,12 +357,6 @@ public class PGraphics2 extends PGraphics { return; } - // set smoothing mode - graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - smooth ? - RenderingHints.VALUE_ANTIALIAS_ON : - RenderingHints.VALUE_ANTIALIAS_OFF); - if (fill) render_triangles(); if (stroke) render_lines(); @@ -373,17 +367,20 @@ public class PGraphics2 extends PGraphics { ////////////////////////////////////////////////////////////// - // POINT + // SHAPES public void point(float x, float y) { + graphics.drawLine(x1, y1, x2, y2); } public void line(float x1, float y1, float x2, float y2) { + graphics.drawLine(x1, y1, x2, y2); } public void triangle(float x1, float y1, float x2, float y2, float x3, float y3) { + } public void rect(float x1, float y1, float x2, float y2) { @@ -422,21 +419,12 @@ public class PGraphics2 extends PGraphics { float x, float y, float hr, float vr) { } - //public void ellipseMode(int mode) { - //} - public void ellipse(float x, float y, float hradius, float vradius) { } public void circle(float x, float y, float radius) { } - //public float bezierPoint(float a, float b, float c, float d, - // float t); - - //public float bezierTangent(float a, float b, float c, float d, - // float t); - public void bezier(float x1, float y1, float x2, float y2, float x3, float y3, @@ -455,10 +443,6 @@ public class PGraphics2 extends PGraphics { // TODO } - //public float curvePoint(float a, float b, float c, float d, float t); - - //public float curveTangent(float a, float b, float c, float d, float t); - public void curve(float x1, float y1, float x2, float y2, float x3, float y3, @@ -466,6 +450,7 @@ public class PGraphics2 extends PGraphics { // TODO } + /* public void textFont(PFont which); @@ -501,6 +486,12 @@ public class PGraphics2 extends PGraphics { */ + + ////////////////////////////////////////////////////////////// + + // MATRIX + + public void translate(float tx, float ty) { graphics.translate(tx, ty); } @@ -580,4 +571,210 @@ public class PGraphics2 extends PGraphics { graphics.getTransform().getMatrix(transform); return (float)transform[1]*x + (float)transform[3]*y + (float)transform[5]; } + + + + ////////////////////////////////////////////////////////////// + + // STROKE + + + public void strokeWeight(float weight) { + super.strokeWeight(weight); + setStroke(); + } + + + public void strokeJoin(int join) { + super.strokeJoin(join); + setStroke(); + } + + + public void strokeCap(int cap) { + super.strokeCap(cap); + setStroke(); + } + + + protected void setStroke() { + int cap = BasicStroke.CAP_BUTT; + if (strokeCap == ROUND) { + cap = BasicStroke.CAP_ROUND; + } else if (strokeCap == PROJECTED) { + cap = BasicStroke.CAP_SQUARE; + } + + int join = BasicStroke.JOIN_BEVEL; + if (strokeJoin == MITERED) { + join = BasicStroke.JOIN_MITER; + } else if (strokeJoin == ROUND) { + join = BasicStroke.JOIN_ROUND; + } + + graphics.setStroke(new BasicStroke(strokeWeight, cap, join)); + } + + + + ////////////////////////////////////////////////////////////// + + // STROKE/FILL/BACKGROUND + + + protected void calc_tint() { + super.calc_tint(); + // ??? how to do this + } + + protected void calc_fill() { + super.calc_fill(); + graphics.setPaint(new Color(fillColor)); + } + + protected void calc_stroke() { + super.calc_stroke(); + ///graphics.setStroke(new Color(fillColor)); + } + + public void noTint() { + } + + public void noFill() { + } + + public void noStroke() { + } + + + public void background(PImage image) { + if ((image.width != width) || (image.height != height)) { + die("background image must be the same size " + + "as your application"); + } + if ((image.format != RGB) && (image.format != ARGB)) { + die("background images should be RGB or ARGB"); + } + + // blit image to the screen + //System.arraycopy(image.pixels, 0, pixels, 0, pixels.length); + graphics.drawImage((BufferedImage) image.cache, 0, 0); + } + + + /** + * Clears pixel buffer. Also clears the stencil and zbuffer + * if they exist. Their existence is more accurate than using 'depth' + * to test whether to clear them, because if they're non-null, + * it means that depth() has been called somewhere in the program, + * even if noDepth() was called before draw() exited. + */ + public void clear() { + graphics.setColor(new Color(backgroundColor)); + graphics.fillRect(0, 0, width, height); + } + + + + ////////////////////////////////////////////////////////////// + + // FROM PIMAGE + + + public void alpha(int alpha[]) { + // TODO + } + + public void alpha(PImage alpha) { + // TODO + } + + public void filter(int kind) { + // TODO + } + + public void filter(int kind, float param) { + // TODO + } + + + public int get(int x, int y) { + ((BufferedImage) image).getRGB(x, y); + } + + + public PImage get(int x, int y, int w, int h) { + PImage output = new PImage(w, h); + ((BufferedImage) image).getRGB(x, y, w, h, output.pixels, 0, width); + } + + + public void set(int x, int y, int c) { + ((BufferedImage) image).setRGB(x, y, c); + } + + + public void copy(PImage src, int dx, int dy) { + // TODO if this image is not RGB, needs to behave differently + // (if it's gray, need to copy gray pixels) + // for alpha, just leave it be.. copy() doesn't composite + ((BufferedImage) image).setRGB(dx, dy, src.width, src.height, + src.pixels, 0, src.width); + } + + + public void copy(int sx1, int sy1, int sx2, int sy2, + int dx1, int dy1, int dx2, int dy2) { + // TODO + } + + + public void copy(PImage src, int sx1, int sy1, int sx2, int sy2, + int dx1, int dy1, int dx2, int dy2) { + // TODO + } + + + public void blend(PImage src, int sx, int sy, int dx, int dy, int mode) { + // TODO + } + + + public void blend(int sx, int sy, int dx, int dy, int mode) { + // TODO + } + + + public void blend(int sx1, int sy1, int sx2, int sy2, + int dx1, int dy1, int dx2, int dy2, int mode) { + // TODO + } + + + public void blend(PImage src, int sx1, int sy1, int sx2, int sy2, + int dx1, int dy1, int dx2, int dy2, int mode) { + // TODO + } + + + public PImage get() { + // TODO + } + + + public void save(String filename) { + // TODO + } + + + public void smooth() { + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + } + + + public void noSmooth() { + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + } } \ No newline at end of file diff --git a/processing/core/PImage.java b/processing/core/PImage.java index 44013e800..bf65f6c35 100644 --- a/processing/core/PImage.java +++ b/processing/core/PImage.java @@ -85,9 +85,7 @@ public class PImage implements PConstants, Cloneable { public boolean smooth = false; /** for gl subclass / hardware accel */ - public int tindex; - public int tpixels[]; - public int twidth, theight; + public Object cache; // private fields private int fracU, ifU, fracV, ifV, u1, u2, v1, v2, sX, sY, iw, iw1, ih1; @@ -110,7 +108,7 @@ public class PImage implements PConstants, Cloneable { */ public PImage() { format = RGB; // makes sure that this guy is useful - tindex = -1; + cache = null; } @@ -138,7 +136,7 @@ public class PImage implements PConstants, Cloneable { this.width = width; this.height = height; this.format = format; - this.tindex = -1; + this.cache = null; } @@ -150,99 +148,7 @@ public class PImage implements PConstants, Cloneable { this.height = height; this.pixels = new int[width*height]; this.format = format; - this.tindex = -1; - } - - - public void modified() { - tindex = -1; - - // bit shifting this might be more efficient - int width2 = (int) Math.pow(2, Math.ceil(Math.log(width) / Math.log(2))); - int height2 = (int) Math.pow(2, Math.ceil(Math.log(height) / Math.log(2))); - - if ((width2 > twidth) || (height2 > theight)) { - // either twidth/theight are zero, or size has changed - tpixels = null; - } - if (tpixels == null) { - twidth = width2; - theight = height2; - tpixels = new int[twidth * theight]; - } - - // copy image data into the texture - int p = 0; - int t = 0; - - if (System.getProperty("sun.cpu.endian").equals("big")) { - switch (format) { - case ALPHA: - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - tpixels[t++] = 0xFFFFFF00 | pixels[p++]; - } - t += twidth - width; - } - break; - - case RGB: - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int pixel = pixels[p++]; - tpixels[t++] = (pixel << 8) | 0xff; - } - t += twidth - width; - } - break; - - case ARGB: - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int pixel = pixels[p++]; - tpixels[t++] = (pixel << 8) | ((pixel >> 24) & 0xff); - } - t += twidth - width; - } - break; - } - - } else { - switch (format) { - case ALPHA: - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - tpixels[t++] = (pixels[p++] << 24) | 0x00FFFFFF; - } - t += twidth - width; - } - break; - - case RGB: - for (int y = 0; y < height; y++) { - System.arraycopy(pixels, p, tpixels, t, width); - p += width; - t += twidth; - } - break; - - case ARGB: - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int pixel = pixels[p++]; - // needs to be ABGR - // stored in memory ARGB - // so R and B must be swapped - tpixels[t++] = - ((pixel & 0xFF) << 16) | - ((pixel & 0xFF0000) >> 16) | - (pixel & 0xFF00FF00); - } - t += twidth - width; - } - break; - } - } + this.cache = null; } @@ -265,7 +171,7 @@ public class PImage implements PConstants, Cloneable { } catch (InterruptedException e) { } format = RGB; - tindex = -1; + cache = null; }