JRubyでFilthyRichClients その3
Filthy Rich Clientsの第3章。
paintComponentやpaintをオーバーライドするときや、画像のgetGraphicsメソッドなどで
取得できるGraphicsオブジェクトの話。
Graphicsオブジェクトの状態を変化させて、レンダリングを操作できる。
以下の属性が、Filthy Rich Clientsについて、最も役にたつと書いてある。
属性を変えるためのセッター、ゲッターなど関連するメソッドも一緒に書くと
- Foreground Color
- setColor
- getColor
- Background Color
- setBackground
- getBackground
- Font
- setFont
- getFont
- Stroke
- setStroke
- getStroke
- RenderingHint
- setRenderingHint
- getRenderingHint
- addRenderingHint
- Clip
- clipRect
- clip
- setClip
- getClip
- getClipBounds
- Composite
- setComposite
- getComposite
- Paint
- setPaint
- getPaint
- Transform
- rotate
- scale
- translate
- transform
- setTransform
- getTransform
以下はTransformを色々と変更して、図形や文字を描画している。
include Java include_class %w( JFrame JComponent SwingUtilities ).map{|s| 'javax.swing.' + s} include_class %w( Color ).map{|s| 'java.awt.' + s} class RotateComponent < JComponent def paintComponent(g) g2d = g.create() # 背景の描画 g2d.setColor(Color::WHITE) g2d.fillRect(0, 0, getWidth, getHeight) # 四角形の描画 g2d.setColor(Color::GRAY.brighter()) g2d.fillRect(50, 50, 50, 50) g2d.rotate(degree_to_radian(45)) g2d.setColor(Color::GRAY.darker()) g2d.fillRect(50, 50, 50, 50) g2d = g.create() g2d.rotate(degree_to_radian(45), 75, 75) g2d.setColor(Color::BLACK) g2d.fillRect(50, 50, 50, 50) g2d.dispose() # 文字列の描画 g2d = g.create() g2d.translate(150, 150) total_degree = 0 4.times do g2d.drawString("rotate: " + total_degree.to_s + "-degree", 10, 0) g2d.rotate(degree_to_radian(90)) total_degree += 90 end g2d.dispose() end private def degree_to_radian(deg) Math::PI / 180.0 * deg end end SwingUtilities.invokeLater do f = JFrame.new f.add(RotateComponent.new) f.setSize(300, 300) f.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE) f.setLocationRelativeTo(nil) f.setVisible(true) end