Home About
Java2D , icon

JythonとJava2Dを使って、CD-ROMアイコンを作成

Jythonを使って、今回CD-ROMアイコンを作成してみました。

cdrom16 cdrom16 cdrom32 cdrom32 cdrom48 cdrom48

cdrom.py

import sys

import java.lang as lang
import java.io as io
import java.awt as awt
import java.awt.image as awtimage
import javax.imageio as imageio


false=0
true=1


def createImage(size):
	type=awtimage.BufferedImage.TYPE_4BYTE_ABGR
	bimg=awtimage.BufferedImage(size.width,size.height,type)
	return bimg

def paint(g,mysize):

	ptCenter=awt.Point( mysize.width/2,mysize.height/2 )



	g.setStroke(awt.BasicStroke(0.2))

	ell=awt.geom.Ellipse2D.Float(0,0,mysize.width-1,mysize.height-1)

	# paint background cdrom
	g.setColor(awt.Color.WHITE)
	g.fill(ell)


	# paint light on disk
	keepClip=g.getClip()
	g.setClip(ell)

	#
	# light-1
	#

	c1=awt.Color.YELLOW
	pol=awt.Polygon()
	pol.addPoint( 0, lang.Float(mysize.height*0.2).intValue() )
	pol.addPoint( lang.Float(mysize.width*0.2).intValue(), 0 )
	pol.addPoint( ptCenter.x,ptCenter.y )

	g.setColor(c1)
	g.fill(pol)


	pol=awt.Polygon()
	pol.addPoint( mysize.width, mysize.height-lang.Float(mysize.height*0.2).intValue() )
	pol.addPoint( mysize.width-lang.Float(mysize.width*0.2).intValue(), mysize.height )
	pol.addPoint( ptCenter.x,ptCenter.y )

	g.setColor(c1)
	g.fill(pol)



	#
	# light-2
	#

	c2=awt.Color.GREEN;

	pol=awt.Polygon()
	pol.addPoint( 0, lang.Float(mysize.height*0.2).intValue() )
	pol.addPoint( 0, lang.Float(mysize.height*0.5).intValue() )
	pol.addPoint( ptCenter.x,ptCenter.y )

	g.setColor(c2)
	g.fill(pol)


	pol=awt.Polygon()
	pol.addPoint( mysize.width, mysize.height-lang.Float(mysize.height*0.2).intValue() )
	pol.addPoint( mysize.width, lang.Float(mysize.height*0.5).intValue() )
	pol.addPoint( ptCenter.x,ptCenter.y )

	g.setColor(c2)
	g.fill(pol)


	g.setClip(keepClip)




	# paint cdrom
	g.setColor(awt.Color(102,102,128,100)) # paint as transparent
	g.fill(ell)

	# paint cdrom border
	g.setColor(awt.Color(128,128,102,100))
	g.draw(ell)


	gap=mysize.width*(3.0/4.0)

	ell=awt.geom.Ellipse2D.Float(gap/2,gap/2,mysize.width-gap,mysize.height-gap)
	g.setColor(awt.Color.WHITE)
	g.fill(ell)


outf=None
widthAndHeight=None

try:
	outf=sys.argv[1]
	widthAndHeight=sys.argv[2]
except :
	print "Usage: jython paintcdrom.py outputfile.png size"

if outf==None or widthAndHeight==None :
	sys.exit()


w=lang.Integer(widthAndHeight).intValue()
h=w
mysize=awt.Dimension(w,h)

bimg=createImage(mysize)
g=bimg.getGraphics()
g.setRenderingHint(awt.RenderingHints.KEY_ANTIALIASING,awt.RenderingHints.VALUE_ANTIALIAS_ON)
paint(g,mysize)
g.dispose()

fos=io.FileOutputStream( outf )
imageio.ImageIO.write(bimg,"PNG",fos)
fos.close()

生成方法

出力するPNG画像ファイル名とそのサイズを指定します。

jython cdrom.py cdrom48.png 48

この例では、48x48サイズのCD-ROMアイコンを生成します。