Groovyではてブカウンター風はてなスターカウンターを作成する 8)APIで取得したスター数をはてなスターカウンターに描く
7)はてなスターAPIを使用してはてなスター数を取得する処理と、6)カウンターに数字を描く処理とを合わせる。
これで、カウンターとしての機能を果たせる。
このブログで4桁になることはないだろうが、桁数にあわせて数字を描く位置を補正するようにした。
Dropboxのpublicに保存するようにした。
ソース
import groovy.swing.j2d.* import net.arnx.jsonic.JSON; class HatenaStar { String title; String uri; String star_count; } myURI = new URL("http://s.hatena.ne.jp/blog.json/http://d.hatena.ne.jp/orangeclover/") hatenastar = JSON.decode(new InputStreamReader( myURI.openStream() ,"UTF-8"), HatenaStar.class) gr = new GraphicsRenderer() gr.renderToFile("D:\\MyDropbox\\My Dropbox\\Public\\hatenastar\\HatenaStarCount.png", 75, 14){ antialias("on") rect(x:0, y:0, width:75, height:14, fill: color('#ff9429'), borderColor: color('#ffd583'), borderWidth:1 ) star(cx:7, cy:7, count:5, ir:2, or:4, fill: color('#ffdea4'), ) /* 3D-ish */ /* right side of of star */ line(x1:14, y1:0, x2:14, y2:14, borderColor:color('#ff7b18'), borderWidth:1) /* left side of the rectangle */ line(x1:15, y1:0, x2:15, y2:14, borderColor:color('#ffd583'), borderWidth:1) /* right side of the rectangle */ line(x1:74, y1:0, x2:74, y2:14, borderColor:color('#ff7b18'), borderWidth:1) /* base of the rectangle */ line(x1:0, y1:13, x2:75, y2:13, borderColor:color('#ff7b18'), borderWidth:1) font(new java.awt.Font('Tahoma', java.awt.Font.BOLD, 10)) text( x:70 - (hatenastar.star_count.length() -1) * 10, y:4, text: hatenastar.star_count, fill: 'white', borderColor: no, ) }
実行結果
groovysh -classpath c:\usr\lib\jsonic\jsonic-1.1.3\jsonic-1.1.3.jar
groovy:000> load GraphicsHatenaStarCount.groovy ===> [import groovy.swing.j2d.*] ===> [import groovy.swing.j2d.*, import net.arnx.jsonic.JSON;] ===> true ===> http://s.hatena.ne.jp/blog.json/http://d.hatena.ne.jp/orangeclover/ ===> HatenaStar@18f9b75 ===> groovy.swing.j2d.GraphicsRenderer@91e321 ===> D:\MyDropbox\My Dropbox\Public\hatenastar\HatenaStarCount.png