Tuesday, July 10, 2012

groovyで盛り上がった枠を描画する

groovyで盛り上がった枠を描画するには、以下のコードを実行します。
import java.awt.*
import java.awt.image.*
import java.awt.geom.*
import java.io.*
import javax.imageio.*

// 盛り上がった枠を描画する
def img = ImageIO.read(new File("sf2.jpg"))

def bwidth = 10

def img2 = new BufferedImage(
  img.getWidth()+bwidth*2, img.getHeight()+bwidth*2, 
  BufferedImage.TYPE_INT_ARGB
)
def gr = img2.createGraphics()
def base = new Color(0x808080)
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))
def brighter2 = new Color(Color.HSBtoRGB(hsb[0], hsb[1], (float)hsb[2]*1.4))
def darker = new Color(Color.HSBtoRGB(hsb[0], hsb[1], (float)hsb[2]*0.8))
def darker2 = new Color(Color.HSBtoRGB(hsb[0], hsb[1], (float)hsb[2]*0.6))

for(lw=0;lw<bwidth;lw++){
  gr.setColor(brighter2)
  gr.drawLine(lw, lw, img2.getWidth()-lw-1, lw)
  gr.setColor(brighter)
  gr.drawLine(lw, lw, lw,img2.getHeight()-lw-1)
  gr.setColor(darker)
  gr.drawLine(img2.getWidth()-lw-1, lw, img2.getWidth()-lw-1, img2.getHeight()-lw-1)
  gr.setColor(darker2)
  gr.drawLine(lw, img2.getHeight()-lw-1, img2.getWidth()-lw-1, img2.getHeight()-lw-1)
}
gr.drawImage(img, bwidth, bwidth, null)
gr.dispose()

ImageIO.write(img2, "png", new File("raisedborder.png"))


元画像

出力画像


動作環境
groovy 1.8.6, JDK7 update4

No comments: