root/lang/scala/sandbox/src/jp/ryugate/image/Graphics.scala @ 5723

Revision 5723, 2.1 kB (checked in by ryugate, 6 years ago)

/scala/sandbox/src/jp/ryugate/excel/*.scala: modules for Excel
/scala/sandbox/src/jp/ryugate/image/*.scala: modules for Java2D
/scala/sandbox/src/jp/ryugate/video/Timecode*.scala: modules for Video Timecode

  • Property svn:executable set to *
Line 
1package jp.ryugate.image
2
3import java.awt._
4import java.awt.image._
5import java.awt.geom._
6import java.awt.font._
7
8class Graphics(grp:Graphics2D) {
9  def setAntialias(f:boolean):Unit = f match {
10    case true => {
11      setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
12      setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
13    }       
14    case false => {
15      setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF)
16      setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)
17    }       
18  }
19
20  def antialias(f:(Graphics => Unit)) = {
21    val now_rh = grp.getRenderingHint(RenderingHints.KEY_ANTIALIASING)
22    val now_trh = grp.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING)
23    setAntialias(true)
24    f(this)
25    setRenderingHint(RenderingHints.KEY_ANTIALIASING, now_rh)
26    setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, now_trh)
27  }   
28   
29  def layer(x:int, y:int, w:int, h:int)(f:(Graphics => Unit)) = {
30    val layer = new BufferedImage(w,h, BufferedImage.TYPE_INT_ARGB_PRE)
31    val g = layer.createGraphics
32    g.setBackground(Color.BLACK)
33    g.setColor(Color.WHITE)
34    f(new Graphics(g))
35    val op = new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_BICUBIC)
36    grp.drawImage(layer, op, x, y)
37  }
38 
39  def line(x1:int, y1:int, x2:int, y2:int) = grp.draw(new Line2D.Double(x1,y1,x2,y2)) 
40  def arc_chord(x:int, y:int, w:int, h:int, start:int, extent:int) = grp.draw(new Arc2D.Double(x,y,w,h,start,extent,Arc2D.CHORD))
41
42  //------------------------------------------------
43  def setBackground(color:Color):Unit = grp.setBackground(color)
44  def clearRect(x:int, y:int, w:int, h:int) = grp.clearRect(x,y,w,h)
45  def setColor(c:Color) = grp.setColor(c)
46  def draw(s:Shape) = grp.draw(s)
47  def setRenderingHint(hk:RenderingHints.Key, hv:Object) = grp.setRenderingHint(hk,hv)
48  def drawString(str:String, x:int,y:int) = grp.drawString(str, x,y)
49  def setFont(name:String, style:int, size:int) = grp.setFont(new Font(name, style, size))
50}
Note: See TracBrowser for help on using the browser.