Created
November 27, 2011 11:38
-
-
Save alphaneet/1397449 to your computer and use it in GitHub Desktop.
processing のキャンバス上に swing コンポネーションを配置してみた。つhttp://d.hatena.ne.jp/alpha_neet/20111127/1322395351
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main { | |
def createGUI(width: Int, height: Int) { | |
val frame = new javax.swing.JFrame("hoge") | |
val swing = new Swing | |
val dim = new java.awt.Dimension(width, height) | |
val applet = new PApplet(swing, width, height) | |
swing.setOpaque(false) | |
swing.setPreferredSize(dim) | |
swing.setLayout(null) | |
applet.init() | |
applet.setPreferredSize(dim) | |
applet.add(swing) | |
frame.getContentPane.add(applet) | |
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE) | |
frame.setResizable(false) | |
frame.pack() | |
frame.setVisible(true) | |
} | |
def main(args: Array[String]) = | |
javax.swing.SwingUtilities.invokeLater( | |
new Runnable() { def run() = createGUI(400, 300) } | |
) | |
} | |
class Swing extends javax.swing.JPanel { | |
swing => | |
import java.awt.Graphics | |
import javax.swing.{ | |
JTextField, | |
JComponent | |
} | |
val components = scala.collection.mutable.ArrayBuffer[Component]() | |
trait Component extends JComponent { | |
swing.components += this | |
swing.add(this) | |
override def paintComponent(g: Graphics) = super.paintComponent(g) | |
override def paint(g: Graphics) = paintComponent(g) | |
} | |
class TextField extends JTextField with Component | |
} | |
class PApplet(val swing: Swing, width: Int, height: Int) extends processing.core.PApplet { | |
import processing.core.PConstants | |
val text = new swing.TextField { | |
import java.awt.event._ | |
setBounds(50, 150, 250, 20) | |
// Enter を押したらテキスト表示する | |
addActionListener(new ActionListener() { | |
override def actionPerformed(e: ActionEvent) { | |
println(getText) | |
} | |
}) | |
} | |
override def setup() { | |
size(width, height, PConstants.P2D) | |
frameRate(24) | |
} | |
override def draw() { | |
background(255, 0, 0) | |
} | |
override def paint(screen: java.awt.Graphics) { | |
super.paint(screen) | |
swing.paint(screen) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment