<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: draw code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 00:07:47 GMT</pubDate>
    <description>DZone Snippets: draw code</description>
    <item>
      <title>Drawing a single Polyline using SVG</title>
      <link>http://snippets.dzone.com/posts/show/5270</link>
      <description>This code is very similar to &lt;a href="http://snippets.dzone.com/posts/show/5269"&gt;Draw a series of lines using SVG&lt;/a&gt; [dzone.com] but now we can draw a series of points at the same time the mouse button is pressed and is moving. Here's an &lt;a href="http://twitxr.com/image/18451/"&gt;example of the SVG squiggle output&lt;/a&gt; [twitxr.com].&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;svg	xmlns="http://www.w3.org/2000/svg" width="100%"&lt;br /&gt;		xmlns:xlink="http://www.w3.org/1999/xlink" &gt;&lt;br /&gt;  &lt;g id="sketch" class="sketch"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;rect x="0"&lt;br /&gt;y="0" width="100%" height="100%" fill="yellow" /&gt;&lt;br /&gt;  &lt;/g&gt;&lt;br /&gt;&lt;br /&gt;  &lt;script&gt;&lt;br /&gt;  &lt;![CDATA[&lt;br /&gt;  var	xmlns="http://www.w3.org/2000/svg"&lt;br /&gt;  Root=document.documentElement&lt;br /&gt;  on_start(Root)&lt;br /&gt;&lt;br /&gt;  function mouseUp(evt) {&lt;br /&gt;	  on_pen_up(Root);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_start(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"mouseMove",&lt;br /&gt;      "onmousedown":"add_line(evt)",	&lt;br /&gt;      "onmouseup":"mouseUp"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_pen_down(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"addPoint(evt)",&lt;br /&gt;	    "onmouseup":"on_pen_up(Root)"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_pen_up(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"null"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function add_line(evt){&lt;br /&gt;	  //if (evt.target.nodeName!="rect") return&lt;br /&gt;	  var C=document.createElementNS(xmlns,"polyline") &lt;br /&gt;	&lt;br /&gt;	  var Attr={&lt;br /&gt;		  "x":30,&lt;br /&gt;		  "y":180,&lt;br /&gt;		  "fill":'none',&lt;br /&gt;		  "stroke": '#44a',&lt;br /&gt;		  "id":'pl1',&lt;br /&gt;		  "stroke-width":2&lt;br /&gt;	  }&lt;br /&gt;    &lt;br /&gt;	  assignAttr(C,Attr)&lt;br /&gt;	  Root.appendChild(C)&lt;br /&gt;    &lt;br /&gt;	  on_pen_down(Root)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function addPoint (evt) {&lt;br /&gt;    element = document.getElementById('pl1') 	&lt;br /&gt;    var point = element.ownerDocument.documentElement.createSVGPoint(); &lt;br /&gt;    point.x = evt.clientX; &lt;br /&gt;    point.y = evt.clientY;&lt;br /&gt;    &lt;br /&gt;    element.points.appendItem(point);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function assignAttr(O,A){&lt;br /&gt;	  for (i in A) O.setAttributeNS(null,i, A[i])&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  ]]&gt;&lt;br /&gt;  &lt;/script&gt;&lt;br /&gt;&lt;br /&gt;  &lt;text font-size="12pt" x="50" y="20" id="t1"&gt;Click something to move it&lt;/text&gt;&lt;br /&gt;  &lt;text font-size="12pt" x="80" y="40" id="t2"&gt;Click nothing to add something&lt;/text&gt;&lt;br /&gt;&lt;/svg&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;*update 7:44pm*&lt;br /&gt;By simply making the id unique using a global variable count it was possible to draw &lt;a href="http://www.twitxr.com/image/18474/"&gt;multiple polylines&lt;/a&gt; [twitxr.com].</description>
      <pubDate>Sat, 22 Mar 2008 19:15:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5270</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Actionscript Draw Box</title>
      <link>http://snippets.dzone.com/posts/show/3377</link>
      <description>Function to draw a box with a specified width, height, and color, and return the box MovieClip&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function draw_box(w,h,c){&lt;br /&gt;	var new_box = this.createEmptyMovieClip("new_box", this.getNextDepth());&lt;br /&gt;	new_box.beginFill(c);&lt;br /&gt;	new_box.lineStyle(0,0x000000,0);&lt;br /&gt;	new_box.moveTo(0, 0);&lt;br /&gt;	new_box.lineTo(w, 0);&lt;br /&gt;	new_box.lineTo(w, h);&lt;br /&gt;	new_box.lineTo(0, h);&lt;br /&gt;	new_box.lineTo(0, 0);&lt;br /&gt;	new_box.endFill();&lt;br /&gt;	return new_box;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample function call draws a 200w x 25h pixel, black box:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;draw_box(200,25,0x000000);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 27 Jan 2007 03:00:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3377</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
    <item>
      <title>Allegro Dibujos de posicion relativa</title>
      <link>http://snippets.dzone.com/posts/show/2658</link>
      <description>Dibujos en allegro dependiendo de la posicion del mouse&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;allegro.h&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void  dibujo(int x,int y){&lt;br /&gt;      circle (screen, x+100,y+100,100,makecol(128,0,0));&lt;br /&gt;      circlefill (screen,x+50,y+50,20,makecol(0,255,0));&lt;br /&gt;      circlefill (screen,x+150,y+50,20,makecol(0,255,0));&lt;br /&gt;      rect (screen,x+80,y+100,x+120,y+150,makecol(0,0,128));&lt;br /&gt;      line (screen,x+50,y+180,x+150,y+180,makecol(128,128,0));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void init();&lt;br /&gt;void deinit();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;	init();&lt;br /&gt;	show_mouse(screen);&lt;br /&gt;	while (!key[KEY_ESC]) {  &lt;br /&gt;          clear_to_color(screen,0);&lt;br /&gt;          dibujo(mouse_x,mouse_y);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	deinit();&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;END_OF_MAIN();&lt;br /&gt;&lt;br /&gt;void init() {&lt;br /&gt;	int depth, res;&lt;br /&gt;	allegro_init();&lt;br /&gt;	depth = desktop_color_depth();&lt;br /&gt;	if (depth == 0) depth = 32;&lt;br /&gt;	set_color_depth(depth);&lt;br /&gt;	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);&lt;br /&gt;	if (res != 0) {&lt;br /&gt;		allegro_message(allegro_error);&lt;br /&gt;		exit(-1);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	install_timer();&lt;br /&gt;	install_keyboard();&lt;br /&gt;	install_mouse();&lt;br /&gt;	/* add other initializations here */&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void deinit() {&lt;br /&gt;	clear_keybuf();&lt;br /&gt;	/* add other deinitializations here */&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Sep 2006 20:09:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2658</guid>
      <author>jcongote (John Edgar Congote Calle)</author>
    </item>
    <item>
      <title>Sparkline: from PIL to S60</title>
      <link>http://snippets.dzone.com/posts/show/413</link>
      <description>py_s60 1.1.3 has its drawing API very similar to PIL.&lt;br /&gt;Porting from PIL to S60 is very easy. Here's an example&lt;br /&gt;that I port a sparkline drawing function from &lt;br /&gt;Joe Gregorio's article at xml.com&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# modified from  Joe Gregorio's article at&lt;br /&gt;# http://www.xml.com/pub/a/2005/06/22/sparklines.html&lt;br /&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;import e32&lt;br /&gt;&lt;br /&gt;lock = e32.Ao_lock()&lt;br /&gt;c = Canvas()&lt;br /&gt;app.body = c&lt;br /&gt;draw = c._draw&lt;br /&gt;&lt;br /&gt;red, green, blue, gray = 0xff0000, 0x00ff00, 0x0000ff, 0x777777&lt;br /&gt;&lt;br /&gt;def plot_sparkline(results, step=2, height=20, \&lt;br /&gt;    min_m=None, max_m=None, last_m=None, \&lt;br /&gt;    min_color=blue, max_color=green, last_color=red):&lt;br /&gt;    coords = zip(range(1,len(results)*step+1, step), \&lt;br /&gt;       [height - 3  - y/(101.0/(height-4)) for y in results])&lt;br /&gt;    draw.line(coords, gray)&lt;br /&gt;    if min_m:&lt;br /&gt;        min_pt = coords[results.index(min(results))]&lt;br /&gt;        draw.rectangle([min_pt[0]-1, min_pt[1]-1, min_pt[0]+1, min_pt[1]+1], fill=min_color)&lt;br /&gt;    if max_m:&lt;br /&gt;        max_pt = coords[results.index(max(results))]&lt;br /&gt;        draw.rectangle([max_pt[0]-1, max_pt[1]-1, max_pt[0]+1, max_pt[1]+1], fill=max_color)&lt;br /&gt;    if last_m:&lt;br /&gt;        end = coords[-1]&lt;br /&gt;        draw.rectangle([end[0]-1, end[1]-1, end[0]+1, end[1]+1], fill=last_color)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;results = [88,84,82,92,82,86,66,82,44,64,66,88,96,80,24,26, \&lt;br /&gt;        14,0,0,26,8,6,6,24,52,66,36,6,10,14,30]&lt;br /&gt;plot_sparkline(results, 3, 30, min_m=1, max_m=1, last_m=1)&lt;br /&gt;&lt;br /&gt;app.exit_key_handler = lock.signal&lt;br /&gt;lock.wait()&lt;/code&gt;</description>
      <pubDate>Sun, 26 Jun 2005 13:50:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/413</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
