■ 事例

 ↑ TOP

    
tips369_Graphics2D/scala/src/AppWindow.scala
1: /* 2: * Copyright (c) 2010-2013, KOTSUBU-chan and/or its affiliates. 3: * Copyright (c) 1998, Atelier-AYA. 4: * All rights reserved. 5: */ 6: package cherry.pie 7: 8: // ---------------------------------------- 9: import swing._ 10: object AppWindow extends MainFrame { 11: val version = AppWindow 12: .getClass.getName+": #1.0.01" 13: // ---------------------------------------- 14: title = "hexTetris" 15: contents = new View 16: peer.setLocationRelativeTo(null) 17: } 18: 19: // ---------------------------------------- 20: class View extends FlowPanel { 21: preferredSize = new Dimension(220,200) 22: override def paintComponent(g: Graphics2D) { 23: new Stone(new Point(14,4)).paint(g) 24: } 25: } 26: 27: // ---------------------------------------- 28: object Stone { 29: val vertices = List((1,0),(2,1),(2,3),(1,4),(0,3),(0,1)) 30: val (dx,dy) = (7,4) 31: 32: def xPoints(translate: Point, scale: Int) = { 33: points(translate, scale) map { case (x,y) => x } 34: } 35: def yPoints(translate: Point, scale: Int) = { 36: points(translate, scale) map { case (x,y) => y } 37: } 38: def points(translate: Point, scale: Int) = { 39: import scala.collection.mutable.ListBuffer 40: val buf = new ListBuffer[(Int,Int)] 41: vertices foreach { case (x,y) => 42: val px = translate.x + dx*scale*x 43: val py = translate.y + dy*scale*y 44: buf += ((px, py)) 45: } 46: buf.toArray 47: } 48: } 49: 50: class Stone(origin: Point) { 51: def paint(g: Graphics2D) { 52: val translate = new Point( 53: Stone.dx*origin.x, Stone.dy*origin.y) 54: val scale = 2 55: val xPoints = Stone.xPoints(translate, scale) 56: val yPoints = Stone.yPoints(translate, scale) 57: val nPoints = 6 58: 59: import java.awt.Color 60: g.setColor(Color.yellow) 61: g.fillPolygon(xPoints, yPoints, nPoints) 62: 63: import java.awt.BasicStroke 64: g.setStroke(new BasicStroke(3)) 65: g.setColor(Color.red) 66: g.drawPolygon(xPoints, yPoints, nPoints) 67: } 68: }

 ↑ TOP