Saturday, August 04, 2012

groovyでドーナツ模様を描画する

groovyでドーナツ模様を描画するには、以下のコードを実行します。
@Grab(group='org.codehaus.griffon', module='jsilhouette-geom', version='0.4')
import java.io.*
import java.awt.*
import java.awt.geom.*
import java.awt.image.*
import javax.imageio.*
import org.codehaus.griffon.jsilhouette.geom.*

// ドーナツ模様を描画する 
def img = new BufferedImage(
    200, 200, BufferedImage.TYPE_INT_RGB
)
def gr = img.createGraphics()
gr.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON
)

def radius1 = 18
def radius2 = 12
def padding = 2
def interval = radius1*2 + padding

gr.setColor(new Color(0xffffff))
gr.fillRect(0, 0, img.getWidth(), img.getHeight())

gr.setColor(new Color(0x8C6F5E))
def rc = 0
for(int ly=0;ly<img.getHeight();ly+=interval){
  for(int lx=0;lx<img.getWidth()+radius1;lx+=interval){
    def donut = new Donut(lx+radius1, ly+radius1,
      radius1, radius2, 2)
    if( rc%2 == 0 ){
      donut = new Donut(lx+radius1-interval/2, ly+radius1,
        radius1, radius2, 2)
    }
    gr.fill(donut)
  }
  rc++;
}
gr.dispose()
ImageIO.write(img, "png", new File("drawdonutspattern.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4

groovyで大小水玉模様を描画する

groovyで大小水玉模様を描画するには、以下のコードを実行します。
import java.io.*
import java.awt.*
import java.awt.geom.*
import java.awt.image.*
import javax.imageio.*

// 大小水玉模様を描画する 
def img = new BufferedImage(
    200, 200, BufferedImage.TYPE_INT_RGB
)
def gr = img.createGraphics()
gr.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON
)

def radius1 = 9
def radius2 = 3
def dr = radius1 - radius2
def padding = 2
def interval = radius1*2 + padding

gr.setColor(new Color(0xffffff))
gr.fillRect(0, 0, img.getWidth(), img.getHeight())

gr.setColor(new Color(0x99bbff))
def rc = 0
for(int ly=0;ly<img.getHeight();ly+=interval){
  def dc = (rc%2==0)?1:0
  for(int lx=0;lx<img.getWidth()+radius1;lx+=interval){
    def rr = new Ellipse2D.Double(
      lx+dr, ly+dr, radius2*2, radius2*2)
    if( dc%2 == 0 ){
      rr = new Ellipse2D.Double(
        lx, ly, radius1*2, radius1*2)
    }
    gr.fill(rr)
    dc++;
  }
  rc++;
}
gr.dispose()
ImageIO.write(img, "png", new File("draw2dots.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4

groovyでひし形模様を描画する

groovyでひし形模様を描画するには、以下のコードを実行します。
import java.io.*
import java.awt.*
import java.awt.geom.*
import java.awt.image.*
import javax.imageio.*

// ひし形模様を描画する 
def img = new BufferedImage(
    200, 200, BufferedImage.TYPE_INT_RGB
)
def gr = img.createGraphics()
gr.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON
)

def size = 20
def color1 = new Color(0x7799dd)
def color2 = new Color(0xFFFFFF)
def xpoints = new int[4]
def ypoints = new int[4]

gr.setColor(color2)
gr.fillRect(0, 0, img.getWidth(), img.getHeight())
gr.setColor(color1)
int rc = 1;
for(int ly=0;ly<img.getHeight()+size;ly+=size){
  for(int lx=0;lx<img.getWidth()+size;lx+=size){
    xpoints[0] = (rc%2==0)?(int)(lx+size/2):lx;
    ypoints[0] = ly
    xpoints[1] = (rc%2==0)?(int)(lx+size*3/4):(int)(lx+size*1/4);
    ypoints[1] = (int)(ly+size/2)
    xpoints[2] = (rc%2==0)?(int)(lx+size/2):lx;
    ypoints[2] = ly+size
    xpoints[3] = (rc%2==0)?(int)(lx+size*1/4):(int)(lx-size*1/4);
    ypoints[3] = (int)(ly+size/2)
    gr.fillPolygon(xpoints, ypoints, 4)
  }
  rc++;
}
gr.dispose()
ImageIO.write(img, "png", new File("drawrhombus.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4

groovyでひねった放射ストライプを描画する

groovyでひねった放射ストライプを描画するには、以下のコードを実行します。
@Grab(group='com.jhlabs', module='filters', version='2.0.235')
@Grab(group='org.codehaus.griffon', module='jsilhouette-geom', version='0.4')
import java.awt.*
import java.awt.image.*
import java.io.*
import javax.imageio.*
import com.jhlabs.composite.*
import com.jhlabs.image.*
import org.codehaus.griffon.jsilhouette.geom.*

// ひねった放射ストライプを描画する
def img = new BufferedImage(
  200, 200, BufferedImage.TYPE_INT_ARGB
)
def gr = img.createGraphics()
gr.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON
)
def radius = 200
def nrays = 10
def rays = new Rays(img.getWidth()/2, img.getHeight()/2,
  radius, nrays
)
gr.setColor(new Color((int)0xccddff))
gr.fill(rays)
gr.dispose()

def tf = new TwirlFilter()
// 角度 
tf.setAngle((float)60/180*ImageMath.PI)
// 中央座標X
tf.setCentreX(0.5f)
// 中央座標Y
tf.setCentreY(0.5f)
// 半径
tf.setRadius((float)(img.getWidth()>img.getHeight()?img.getWidth():img.getHeight()))
img = tf.filter(img, null)

ImageIO.write(img, "png", new File("twistedrays.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4

groovyで三角模様を描画する - その2

groovyで三角模様を描画するには、以下のコードを実行します。
import java.io.*
import java.awt.*
import java.awt.geom.*
import java.awt.image.*
import javax.imageio.*

// 三角模様を描画する - その2
def img = new BufferedImage(
    200, 200, BufferedImage.TYPE_INT_RGB
)
def gr = img.createGraphics()
gr.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON
)

def size = 15
def color1 = new Color(0xCCDDFF)
def color2 = new Color(0xFFFFFF)
def xpoints = new int[3]
def ypoints = new int[3]

gr.setColor(color2)
gr.fillRect(0, 0, img.getWidth(), img.getHeight())
gr.setColor(color1)
int rc = 1;
for(int ly=0;ly<img.getHeight()+size;ly+=size){
  for(int lx=0;lx<img.getWidth()+size;lx+=size){
    xpoints[0] = (rc%2==0)?(int)(lx+size/2):lx;
    ypoints[0] = ly
    xpoints[1] = (rc%2==0)?lx+size:(int)(lx+size/2);
    ypoints[1] = ly+size
    xpoints[2] = (rc%2==0)?lx:(int)(lx-size/2);
    ypoints[2] = ly+size
    gr.fillPolygon(xpoints, ypoints, 3)
  }
  rc++;
}
gr.dispose()
ImageIO.write(img, "png", new File("drawtriangles2.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4

Thursday, August 02, 2012

groovyで三角模様を描画する

groovyで三角模様を描画するには、以下のコードを実行します。
import java.io.*
import java.awt.*
import java.awt.geom.*
import java.awt.image.*
import javax.imageio.*

// 三角模様を描画する 
def img = new BufferedImage(
    200, 200, BufferedImage.TYPE_INT_RGB
)
def gr = img.createGraphics()
gr.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON
)

def size = 15
def color1 = new Color(0xF27405)
def color2 = new Color(0xFFFFFF)
def xpoints = new int[3]
def ypoints = new int[3]

gr.setColor(color2)
gr.fillRect(0, 0, img.getWidth(), img.getHeight())
gr.setColor(color1)
for(int ly=0;ly<img.getHeight()+size;ly+=size){
  for(int lx=0;lx<img.getWidth()+size;lx+=size){
    xpoints[0] = lx
    ypoints[0] = ly
    xpoints[1] = lx+size
    ypoints[1] = ly
    xpoints[2] = lx
    ypoints[2] = ly+size
    gr.fillPolygon(xpoints, ypoints, 3)
  }
}
gr.dispose()
ImageIO.write(img, "png", new File("drawtriangles.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4

Tuesday, July 31, 2012

groovyでピラミッド模様を描画する

groovyでピラミッド模様を描画するには、以下のコードを実行します。
import java.awt.*
import java.awt.image.*
import java.awt.geom.*
import java.io.*
import javax.imageio.*

// ピラミッド模様を描画する
def img = new BufferedImage(
  200, 200, BufferedImage.TYPE_INT_ARGB
)
def gr = img.createGraphics()

def size = 20

def base = new Color(0x5577bb)
def hsb = new float[3]
Color.RGBtoHSB(base.getRed(), base.getGreen(), base.getBlue(), hsb)
def brighter = new Color(Color.HSBtoRGB(hsb[0], hsb[1], (float)hsb[2]*1.2>1?1:hsb[2]*1.1))
def brighter2 = new Color(Color.HSBtoRGB(hsb[0], hsb[1], (float)hsb[2]*1.2>1?1:hsb[2]*1.2))
def darker = new Color(Color.HSBtoRGB(hsb[0], hsb[1], (float)hsb[2]*0.9))
def darker2 = new Color(Color.HSBtoRGB(hsb[0], hsb[1], (float)hsb[2]*0.8))

def xpoints = new int[3]
def ypoints = new int[3]
for(int ly=0;ly<img.getHeight()+size;ly+=size){
  for(int lx=0;lx<img.getWidth()+size;lx+=size){
    gr.setColor(brighter2)
    xpoints[0] = lx
    ypoints[0] = ly
    xpoints[1] = lx+size
    ypoints[1] = ly
    xpoints[2] = (int)lx+size/2
    ypoints[2] = (int)ly+size/2
    gr.fillPolygon(xpoints, ypoints, 3)

    gr.setColor(brighter)
    xpoints[0] = lx
    ypoints[0] = ly
    xpoints[1] = lx
    ypoints[1] = ly+size
    xpoints[2] = (int)lx+size/2
    ypoints[2] = (int)ly+size/2
    gr.fillPolygon(xpoints, ypoints, 3)

    gr.setColor(darker)
    xpoints[0] = lx+size
    ypoints[0] = ly
    xpoints[1] = lx+size
    ypoints[1] = ly+size
    xpoints[2] = (int)lx+size/2
    ypoints[2] = (int)ly+size/2
    gr.fillPolygon(xpoints, ypoints, 3)

    gr.setColor(darker2)
    xpoints[0] = lx
    ypoints[0] = ly+size
    xpoints[1] = lx+size
    ypoints[1] = ly+size
    xpoints[2] = (int)lx+size/2
    ypoints[2] = (int)ly+size/2
    gr.fillPolygon(xpoints, ypoints, 3)
  }
}
gr.dispose()

ImageIO.write(img, "png", new File("drawpyramid.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4

Monday, July 30, 2012

groovyで渦巻き模様を生成する

groovyで渦巻き模様を生成するには、以下のコードを実行します。
@Grab(group='com.jhlabs', module='filters', version='2.0.235')
import java.awt.*
import java.awt.image.*
import java.io.*
import javax.imageio.*
import com.jhlabs.composite.*
import com.jhlabs.image.*

// 渦巻き模様を生成する
def img = new BufferedImage(
    200, 200, BufferedImage.TYPE_INT_ARGB
)
def gr = img.createGraphics()
gr.setColor(new Color((int)0xccddff))
gr.fillRect(0, 0, (int)img.getWidth(), (int)img.getHeight()/2)
gr.setColor(new Color((int)0xffffff))
gr.fillRect(0, (int)img.getHeight()/2, (int)img.getWidth(), (int)img.getHeight()/2)
gr.dispose()

def tf = new TwirlFilter()
// 角度 
tf.setAngle((float)ImageMath.PI*7200/360)
// 中央座標X
tf.setCentreX(0.5f)
// 中央座標Y
tf.setCentreY(0.5f)
// 半径
tf.setRadius(
  (float)img.getWidth()>img.getHeight()?img.getWidth():img.getHeight()
)
img = tf.filter(img, null)

ImageIO.write(img, "png", new File("spiral.png"))


出力画像


動作環境
groovy 1.8.6, JDK7 update4