本文整理汇总了Scala中java.awt.Graphics2D类的典型用法代码示例。如果您正苦于以下问题:Scala Graphics2D类的具体用法?Scala Graphics2D怎么用?Scala Graphics2D使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Graphics2D类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: GraphicMatrix
//设置package包名称以及导入依赖的类
package swayvil.langtonant.gui
import java.awt.{ Graphics2D, BasicStroke }
import java.awt.geom._ // Rectangle2D
import swayvil.langtonant.Matrix
import swayvil.langtonant.Square
import scala.swing.Panel
import java.awt.Color
import java.awt.Dimension
class GraphicMatrix(private var m: Matrix) extends Panel with GUI {
preferredSize = new Dimension(m.size * squareSize, m.size * squareSize)
override def paintComponent(g: Graphics2D) {
m.matrix.foreach { (row: Array[Square]) => row.foreach { (square: Square) => drawSquare(g, square.x, square.y) } }
}
override def repaint() {
super.repaint()
}
private def drawSquare(g: Graphics2D, x: Int, y: Int) {
var rec = new Rectangle2D.Double(squareSize * x, squareSize * y, squareSize, squareSize)
if (m.matrix(x)(y).isWhite)
g.setColor(white)
else
g.setColor(black)
if (m.matrix(x)(y).isAnt)
g.setColor(red)
g.fill(new Rectangle2D.Double(squareSize * x, squareSize * y, squareSize, squareSize))
g.setColor(black)
g.draw(new Rectangle2D.Double(squareSize * x, squareSize * y, squareSize, squareSize))
}
}
示例2: GraphicTurn
//设置package包名称以及导入依赖的类
package swayvil.langtonant.gui
import java.awt.{ Graphics2D }
import main.scala.swayvil.langtonant.Game
import scala.swing.Panel
import java.awt.Dimension
class GraphicTurn(var game: Game) extends Panel with GUI {
preferredSize = new Dimension(10, 10)
override def paintComponent(g: Graphics2D) {
// draw some text
g.setColor(black)
g.setFont(font)
g.drawString(game.turn.toString(), 10, 10)
}
override def repaint() {
super.repaint()
}
}
示例3: Bubble
//设置package包名称以及导入依赖的类
package com.alvinalexander.bubbles
import java.awt.Color
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.awt.GraphicsConfiguration
import java.awt.Transparency
case class Bubble (
var x: Int,
var y: Int,
var lastX: Int,
var lastY: Int,
circleDiameter: Int,
fgColor: Color,
bgColor: Color,
char: Char
)
{
private var image: BufferedImage = null
def drawBubbleFast(g: Graphics, gc: GraphicsConfiguration) {
val g2 = g.asInstanceOf[Graphics2D]
if (image == null) {
println("image was null")
// build the image on the first call
image = gc.createCompatibleImage(circleDiameter+1, circleDiameter+1, Transparency.BITMASK)
val gImg = image.getGraphics.asInstanceOf[Graphics2D]
renderBubble(gImg)
gImg.dispose
}
g2.drawImage(image, x, y, null)
}
// given a Graphics object, render the bubble
def renderBubble(g: Graphics) {
val g2 = g.create.asInstanceOf[Graphics2D]
// draw the circle
g2.setColor(bgColor)
g2.fillOval(0, 0, circleDiameter, circleDiameter)
// draw the character
g2.setFont(CIRCLE_FONT)
g2.setColor(fgColor)
g2.drawString(char.toString, CIRCLE_FONT_PADDING_X, CIRCLE_FONT_PADDING_Y)
g2.dispose
}
}
示例4: Desenho
//设置package包名称以及导入依赖的类
package jerimum
import java.awt.{ Graphics2D, RenderingHints }
import scala.collection.SortedMap
import br.edu.ifrn.potigol.Potigolutil.Inteiro
object Desenho {
private[this] val vazia = SortedMap[Inteiro, List[Graphics2D => Unit]]()
private[this] var camadas = vazia
private[this] def todos = camadas.values.flatten
private[this] val rh = new RenderingHints(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB)
def desenhe(g: Graphics2D): Unit = {
g match {
case g: Graphics2D =>
g.setRenderingHints(rh)
}
todos.foreach(f => f(g))
camadas = vazia
}
def incluir(z: Inteiro, funcao: Graphics2D => Unit) = {
camadas += z -> (funcao :: camadas.getOrElse(z, Nil))
}
}
示例5: PointPanel
//设置package包名称以及导入依赖的类
package com.ganchurin.view
import java.awt.geom.Rectangle2D
import java.awt.{Color, Graphics2D}
import javax.swing.JPanel
class PointPanel(width: Int, height: Int, cellSize: Int, gridSize: Int) extends JPanel {
def initRectangles(): Unit = {
val rectangles = for {
i <- 0 until width by (cellSize + gridSize)
j <- 0 until height by (cellSize + gridSize)
} yield new Rectangle2D.Double(i, j, cellSize, cellSize)
clearRectangles(rectangles)
}
def paintRectangles(rectangles: Seq[Rectangle2D.Double]): Unit = {
fillRectangles(rectangles, Color.BLACK)
}
def clearRectangles(rectangles: Seq[Rectangle2D.Double]): Unit = {
fillRectangles(rectangles, Color.WHITE)
}
private def fillRectangles(rectangles: Seq[Rectangle2D.Double], color: Color) {
val g = getGraphics
val g2d = g.asInstanceOf[Graphics2D]
g2d setColor color
rectangles foreach g2d.fill
}
}
示例6: SnowAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.Graphics2D
import java.awt.AlphaComposite
import collection.mutable.ListBuffer
import util.Random
import game_mechanics.path.{Waypoint,CellPos}
import gui._
object SnowAnimation
{
val rng = new Random()
}
class SnowAnimation( pos : CellPos, radius : Double ) extends Animatable
{
import SnowAnimation._
val size = MapPanel.cellsize.toDouble
val origin = (pos.toDouble + new Waypoint(0.5,0.5)) * size
val duration = 7.0
timer = duration
val particle_amount = 30
val particles = new ListBuffer[Waypoint]()
val fall_distance = 20
for( i <- 0 until particle_amount )
{
// Uniform disk distribution
val r = rng.nextDouble
val theta = rng.nextDouble * 2 * Math.PI
val x = Math.sqrt( r ) * Math.cos( theta ) * radius * size
val y = Math.sqrt( r ) * Math.sin( theta ) * radius * size
particles += new Waypoint( x + origin.x, y + origin.y - fall_distance )
}
override def draw(g: Graphics2D): Unit = {
g.setColor( Colors.white )
val alpha = (timer / duration).toFloat
g.setComposite(
AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) )
val movement = (1.0 - timer / duration) * fall_distance
for( particle <- particles ) {
g.drawRect( particle.x.toInt, particle.y.toInt + movement.toInt,
1, 1 )
}
}
}
示例7: ThunderflashAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.Graphics2D
import java.awt.AlphaComposite
import runtime.TowerDefense
import gui._
class ThunderflashAnimation extends Animatable
{
val duration = 0.5
timer = duration
override def draw(g : Graphics2D): Unit = {
g.setColor( Colors.white )
val alpha = Math.pow( timer / duration, 4 ).toFloat
g.setComposite(
AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) )
g.fillRect( 0, 0,
TowerDefense.map_panel.map.width * MapPanel.cellsize,
TowerDefense.map_panel.map.height * MapPanel.cellsize )
}
}
示例8: MuzzleflashAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.Graphics2D
import game_mechanics.path.Waypoint
import gui._
class MuzzleflashAnimation(origin : Waypoint) extends Animatable
{
val duration = 0.1
timer = duration
val cellsize = MapPanel.cellsize
val size = cellsize / 2
val pos = origin * cellsize
override def draw(g: Graphics2D): Unit = {
g.setColor( Colors.white )
g.fillOval(
pos.x.toInt + cellsize / 2 - size / 2,
pos.y.toInt + cellsize / 2 - size / 2,
size, size )
}
}
示例9: DamageAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.AlphaComposite
import java.awt.Graphics2D
import game_mechanics.path.Waypoint
import gui._
import runtime.{TowerDefense,Controller}
import utils.Continuable
class DamageAnimation(amount: Double, origin: Waypoint) extends Animatable
{
var pos = origin
val target = origin + new Waypoint(0,-1)
override def draw(g: Graphics2D): Unit = {
pos = origin * timer + target * (1 - timer)
g.setColor( Colors.red )
g.drawString( amount.toString + " dmg",
pos.x.toFloat * MapPanel.cellsize,
pos.y.toFloat * MapPanel.cellsize )
}
}
示例10: SmokeAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import collection.mutable.ListBuffer
import util.Random
import java.awt.Graphics2D
import game_mechanics.path.Waypoint
import gui._
class SmokeAnimation(origin: Waypoint) extends Animatable
{
val duration = 2.0
val rng = new Random
timer = duration
val particles = new ListBuffer[Waypoint]
for( i <- 0 until 30 )
{
val theta = rng.nextDouble * 2 * Math.PI
particles += new Waypoint( Math.cos( theta ), Math.sin( theta ) )
}
override def draw(g: Graphics2D) : Unit = {
val interp = 1 - timer / duration
g.setColor( Colors.lightGrey )
for( particle <- particles )
{
val pos = (origin + particle * interp * 1.5) * MapPanel.cellsize
g.fillOval(
pos.x.toInt, pos.y.toInt,
(MapPanel.cellsize * (1-interp)).toInt,
(MapPanel.cellsize * (1-interp)).toInt )
}
}
}
示例11: BuffAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.Graphics2D
import java.awt.AlphaComposite
import collection.mutable.ListBuffer
import gui._
import util.Random
import game_mechanics.path.{Waypoint,CellPos}
object BuffAnimation
{
val rng = new Random()
}
class BuffAnimation( pos : CellPos, radius : Double ) extends Animatable
{
import BuffAnimation._
val size = MapPanel.cellsize.toDouble
val origin = (pos.toDouble + new Waypoint(0.5,0.5)) * size
val duration = 7.0
timer = duration
val particle_amount = 30
val particles = new ListBuffer[Waypoint]()
val fall_distance = 20
for( i <- 0 until particle_amount )
{
// Uniform disk distribution
val r = rng.nextDouble
val theta = rng.nextDouble * 2 * Math.PI
val x = Math.sqrt( r ) * Math.cos( theta ) * radius * size
val y = Math.sqrt( r ) * Math.sin( theta ) * radius * size
particles += new Waypoint( x + origin.x, y + origin.y - fall_distance )
}
override def draw(g: Graphics2D): Unit = {
g.setColor( Colors.blue )
val alpha = (timer / duration).toFloat
g.setComposite(
AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) )
val movement = timer / duration * fall_distance
for( particle <- particles ) {
g.drawString( "+",
particle.x.toInt, particle.y.toInt + movement.toInt )
}
}
}
示例12: RaygunShootAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import collection.mutable.ListBuffer
import util.Random
import java.awt.Graphics2D
import java.awt.AlphaComposite
import runtime.TowerDefense
import gui._
import game_mechanics.path.{Waypoint,CellPos}
object RaygunShootAnimation
{
val rng = new Random
val duration = 10.0
}
class RaygunShootAnimation(tower_pos: CellPos, direction : Waypoint) extends Animatable
{
import RaygunShootAnimation._
val origin = tower_pos.toDouble
timer = duration
val size = MapPanel.cellsize.toDouble
val laser_length = (TowerDefense.map_panel.map.width * size).toInt
override def draw(g: Graphics2D): Unit = {
val prev_transform = g.getTransform()
g.rotate( Math.atan2( direction.y, direction.x ), tower_pos.x, tower_pos.y )
val alpha = Math.min( timer, 1.0 ).toFloat
g.setComposite(
AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) )
g.setColor( Colors.white )
g.fillRect(
tower_pos.x * MapPanel.cellsize + MapPanel.cellsize / 2,
tower_pos.y * MapPanel.cellsize - MapPanel.cellsize / 2,
laser_length,
MapPanel.cellsize * 2 )
g.setColor( Colors.lightblue )
g.fillRect(
tower_pos.x * MapPanel.cellsize + MapPanel.cellsize / 2,
tower_pos.y * MapPanel.cellsize,
laser_length,
MapPanel.cellsize )
TowerDefense.map_panel.darkness = (alpha * RaygunAnimation.max_darkness).toFloat
g.setTransform( prev_transform )
}
}
示例13: GoldAnimation
//设置package包名称以及导入依赖的类
package gui.animations
import java.awt.AlphaComposite
import java.awt.Graphics2D
import game_mechanics.path.Waypoint
import gui._
import utils.Continuable
class GoldAnimation(amount: Int,origin: Waypoint) extends Animatable
{
timer = 2.0
var pos = origin
val target = origin + new Waypoint(0,-1)
override def draw(g: Graphics2D): Unit = {
pos = origin * timer + target * (1 - timer)
val string = "+" + amount.toString + " Gold"
val alpha = if(timer < 1.0) { Math.max(0,timer).toFloat } else { 1.0f }
g.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) )
g.setColor( Colors.black )
g.drawString( string,
pos.x.toFloat * MapPanel.cellsize + 1,
pos.y.toFloat * MapPanel.cellsize + 1 )
g.setColor( Colors.yellow )
g.drawString( string,
pos.x.toFloat * MapPanel.cellsize,
pos.y.toFloat * MapPanel.cellsize )
}
}
示例14: TransparentTest
//设置package包名称以及导入依赖的类
package de.sciss.schwaermen
import java.awt.{BasicStroke, Color, Graphics, Graphics2D}
import scala.swing.Swing
object TransparentTest {
def main(args: Array[String]): Unit = {
Swing.onEDT(run())
}
def run(): Unit = {
val frame = new javax.swing.JFrame("Test")
frame.setUndecorated(true)
frame.setBackground (new Color(0,0,0,0))
frame.setContentPane(new TestComponent())
frame.pack()
frame.setSize(500, 500)
frame.setLocationRelativeTo(null)
frame.setVisible(true)
}
final class TestComponent extends javax.swing.JComponent {
override def paintComponent(g: Graphics): Unit = {
super.paintComponent(g)
g.setColor(Color.red)
val g2 = g.asInstanceOf[Graphics2D]
g2.setStroke(new BasicStroke(3f))
g.drawLine(0, 0, getWidth, getHeight)
g.drawLine(0, getHeight, getWidth, 0)
}
}
}
示例15: timeTo
//设置package包名称以及导入依赖的类
package things
import java.awt.{BasicStroke, Color, Graphics2D}
trait Wall extends Thing {
val p: Double
val dir: Int
def timeTo(mov: Movable) = {
val v = mov.vel(dir)
val dist = if (p < 0) p - mov.pos(dir) + mov.r else p - mov.pos(dir) - mov.r
val timeToHit = if (v == 0) Double.PositiveInfinity else dist / v
if (timeToHit <= 0) Double.PositiveInfinity else timeToHit
}
}
case class VWall(p: Double) extends Wall {
val id = Int.MaxValue
val dir = 0
def draw(g: Graphics2D) {
g.setColor(new Color(255, 0, 255))
g.setStroke(new BasicStroke(3f))
line(g)(p, -3000, p, 3000)
}
}
case class HWall(p: Double) extends Wall {
val id = Int.MaxValue
val dir = 1
def draw(g:Graphics2D) {
g.setColor(new Color(255, 0, 255))
g.setStroke(new BasicStroke(3f))
line(g)(-3000, p, 3000, p)
}
}