Created
November 23, 2010 04:30
-
-
Save osima/711252 to your computer and use it in GitHub Desktop.
codes to draw honeycombs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.* | |
class Utils { | |
def getGoodRenderingHints() { | |
def hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON) | |
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY) | |
hints | |
} | |
def outputAsPNG( def bimg, def outfile){ | |
def out = new FileOutputStream(outfile) | |
new javax.imageio.ImageIO().write(bimg,"png",out) | |
out.close() | |
} | |
def createImage( Dimension size ){ | |
new java.awt.image.BufferedImage((int)size.width,(int)size.height,java.awt.image.BufferedImage.TYPE_4BYTE_ABGR) | |
} | |
} | |
abstract class AbstractHoneycombDiagram { | |
def size | |
def outfile | |
def AbstractHoneycombDiagram(Dimension size, def outfile){ | |
this.size = size | |
this.outfile = outfile | |
} | |
def toRectangle( def cell ){ | |
def cu = new org.notehub.ntwo.painter.TextBoxLocationCalcUtil2(cell) | |
def padding = new Insets(-2,5,-2,5) | |
def rect = org.notehub.ntwo.painter.TextBoxLocationCalcUtil2.padding( cu.rectangle, padding ) | |
rect | |
} | |
def paintTextArea( def g2, def cell ){ | |
g2.fill( toRectangle(cell) ) | |
} | |
def paintText( def g2, def cell, def text ){ | |
def tp = new org.notehub.awt.painter.TextPainter(text) | |
tp.verticalAlignment = org.notehub.awt.painter.TextPainter.MIDDLE | |
tp.horizontalAlignment = org.notehub.awt.painter.TextPainter.CENTER | |
def rect = toRectangle(cell) | |
tp.location = new Point( (int)rect.x, (int)rect.y ) | |
tp.size = new Dimension( (int)rect.width, (int)rect.height ) | |
tp.paint(g2) | |
} | |
abstract def createHoneycomb() | |
def generate(){ | |
def u = new Utils() | |
// 1) | |
def bimg = u.createImage( size ) | |
def g2 = bimg.graphics | |
g2.renderingHints = u.goodRenderingHints | |
// 2) | |
g2.color = Color.WHITE | |
g2.fill( new Rectangle(0,0,(int)size.width,(int)size.height) ) | |
// 3) | |
//def h = new org.notehub.ntwo.painter.HoneycombOne() | |
def h = createHoneycomb() | |
h.size = org.notehub.ntwo.painter.DrawingUtils.toXDimension( this.size ) | |
h.getHexagonShapes().eachWithIndex{ shape,index-> | |
// 3-1) border | |
g2.color = Color.BLACK | |
g2.draw( shape ) | |
// 3-2) paint text area | |
g2.color = new Color(0xEE,0xEE,0xEE) | |
paintTextArea( g2, shape.hexagonCell ) | |
// 3-3) paint text | |
g2.color = Color.BLACK | |
paintText( g2, shape.hexagonCell, "${index}) Hello" ) | |
} | |
g2.dispose() | |
u.outputAsPNG(bimg,outfile) | |
} | |
} | |
class HoneycombDiagramOne extends AbstractHoneycombDiagram{ | |
def HoneycombDiagramOne(Dimension size, def outfile){ | |
super(size,outfile) | |
} | |
def createHoneycomb(){ | |
new org.notehub.ntwo.painter.HoneycombOne() | |
} | |
} | |
class HoneycombDiagramTwo extends AbstractHoneycombDiagram{ | |
def HoneycombDiagramTwo(Dimension size, def outfile){ | |
super(size,outfile) | |
} | |
def createHoneycomb(){ | |
new org.notehub.ntwo.painter.HoneycombTwo() | |
} | |
} | |
x = new HoneycombDiagramOne( new Dimension(300,300),new File('one.png') ) | |
x.generate() | |
x = new HoneycombDiagramTwo( new Dimension(720,600),new File('two.png') ) | |
x.generate() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment