Revision: 4745 http://sourceforge.net/p/vexi/code/4745 Author: mkpg2 Date: 2014-11-12 13:16:54 +0000 (Wed, 12 Nov 2014) Log Message: ----------- ui:Canvas. Drawable area. - accidentally saved in gen folder so not committed.
Modified Paths: -------------- branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp Added Paths: ----------- branches/vexi3/org.vexi-core.main/src/main/java/org/vexi/core/Canvas.java Added: branches/vexi3/org.vexi-core.main/src/main/java/org/vexi/core/Canvas.java =================================================================== --- branches/vexi3/org.vexi-core.main/src/main/java/org/vexi/core/Canvas.java (rev 0) +++ branches/vexi3/org.vexi-core.main/src/main/java/org/vexi/core/Canvas.java 2014-11-12 13:16:54 UTC (rev 4745) @@ -0,0 +1,133 @@ +package org.vexi.core; + +import java.awt.BasicStroke; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectOutputStream; + +import javax.imageio.ImageIO; + +import org.ibex.js.Fountain; +import org.ibex.js.JS; +import org.ibex.js.JSExn; +import org.ibex.js.JSU; +import org.vexi.graphics.Color; +import org.vexi.graphics.Texture; + +public class Canvas extends Box implements Texture{ + static final public JS.Constructor Constructor = new JS.Constructor("Canvas") { + public JS new_(JS[] args) { return new Canvas(); } + }; + + + // SHOULD theoretically use pixel buffer to be platform independent ... + private BufferedImage buffer; + private float strokeWidth = 1; + private int drawColor = 0xffffffff; + + public Canvas(){ + visual = new BoxVisual(); + visual.texture = this; + } + public Object getImage() { return getBuffer(); } + public int getWidth() { return width; } + public int getHeight() { return height; } + + @Override public JS get(JS name) throws JSExn { + String nameStr = JSU.toString(name); + if("drawLine".equals(nameStr)) return METHOD; + if("drawColor".equals(nameStr)) return JSU.S(Color.colorToString(drawColor)); + if("drawWidth".equals(nameStr)) return JSU.N(strokeWidth); + return super.get(name); + } + + @Override public void put(JS name, JS value) throws JSExn { + String nameStr = JSU.toString(name); + if("drawColor".equals(nameStr)) { + drawColor = Color.stringToColor(JSU.toString(value)); + return; + } + if("drawWidth".equals(nameStr)) { + strokeWidth = (float)JSU.toDouble(value); + return; + } + super.put(name, value); + } + + @Override public JS callMethod(JS this_, JS method, JS[] args) throws JSExn { + String nameStr = JSU.toString(method); + if("drawLine".equals(nameStr)) { + int x1 = JSU.expectArg_int(args, 0); + int y1 = JSU.expectArg_int(args, 1); + int x2 = JSU.expectArg_int(args, 2); + int y2 = JSU.expectArg_int(args, 3); + Graphics2D graphics2D = (Graphics2D)getBuffer().getGraphics(); + graphics2D.setColor(Color.intToColor(drawColor)); + graphics2D.setStroke(new BasicStroke(strokeWidth)); + graphics2D.drawLine(x1, y1, x2, y2); + dirty(); +// dirty(this, x1-1,y1-1,x2+1,y2+1); + return null; + } + return super.callMethod(this_, method, args); + } + + private BufferedImage getBuffer(){ + if(width==0 || height==0){ + buffer = null; + }else if(buffer==null){ + buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + }else{ + if(buffer.getHeight()!=height || buffer.getWidth()!=width){ + BufferedImage buffer1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + Graphics2D g0 = (Graphics2D)buffer.getGraphics(); + Graphics2D g1 = (Graphics2D)buffer1.getGraphics(); + g1.drawImage(buffer, 0, 0, null); + g1.setColor(g0.getColor()); + g1.setStroke(g0.getStroke()); + buffer = buffer1; + } + // SHOULD resize the buffer if necessary + } + return buffer; + } + + public void init() { + getBuffer(); + } + + public JS getStream() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try{ + ObjectOutputStream oos = new ObjectOutputStream(baos); + ImageIO.write(getBuffer(),"png",ImageIO.createImageOutputStream(oos)); + oos.flush(); + oos.close(); + return new Fountain.ByteArray(baos.toByteArray()); + }catch(Exception e){ + throw new Error(e); + } + } + + synchronized public void setStream(JS f) throws IOException{ + InputStream is = JSU.getInputStream(f); + Image img= ImageIO.read(ImageIO.createImageInputStream(is)); + + BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); + + // Draw the image on to the buffered image + Graphics2D bGr = bimage.createGraphics(); + bGr.drawImage(img, 0, 0, null); + bGr.dispose(); + + // Return the buffered image + buffer = bimage; + } + + public boolean isLoaded() { return true; } + public JSExn getLoadFailed() { return null; } +} Property changes on: branches/vexi3/org.vexi-core.main/src/main/java/org/vexi/core/Canvas.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Modified: branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp =================================================================== --- branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp 2014-11-10 21:07:42 UTC (rev 4744) +++ branches/vexi3/org.vexi-core.main/src/main/jpp/org/vexi/core/Vexi.jpp 2014-11-12 13:16:54 UTC (rev 4745) @@ -15,8 +15,8 @@ import org.ibex.util.Encode; import org.vexi.graphics.Font; import org.vexi.graphics.Picture; +import org.vexi.graphics.Color; import org.vexi.plat.Platform; -import org.vexi.graphics.Color; import org.vexi.util.Log; import java.util.List; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Comprehensive Server Monitoring with Site24x7. Monitor 10 servers for $9/Month. Get alerted through email, SMS, voice calls or mobile push notifications. Take corrective actions from your mobile device. http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn