あかすくぱるふぇ

同人サークル「あかすくぱるふぇ」のブログです。


regedit 起動して,HKEY_LOCAL_MACHINE\Software\Classes.jsContent Typeapplication/javascript に変更

package demos.basic

import com.jogamp.opengl.*
import com.jogamp.opengl.GL2.GL_POLYGON
import com.jogamp.opengl.awt.GLCanvas
import com.jogamp.opengl.util.texture.*
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.SwingUtilities

class FirstStepSwing : GLEventListener {
override fun init(drawable: GLAutoDrawable?) {
drawable?.let {
val gl = it.gl.gL2
gl.glClearColor(0.0f, 0.0f, 1.0f, 1.0f)
}

try {
val resource = this::class.java.classLoader.getResource("nakayoshi.jpg")
texture = TextureIO.newTexture(resource, false, TextureIO.PNG)
} catch (e: Exception) {
e.printStackTrace()
}
}

override fun reshape(drawable: GLAutoDrawable?, x: Int, y: Int, width: Int, height: Int) {}
override fun display(drawable: GLAutoDrawable?) {
drawable?.let {
val gl = it.gl.gL2
gl.glClear(GL.GL_COLOR_BUFFER_BIT)

texture?.enable(gl)
gl.glBegin(GL_POLYGON)
gl.glTexCoord2f(0.0f, 0.0f)
gl.glVertex2f(-0.9f, -0.9f)
gl.glTexCoord2f(1.0f, 0.0f)
gl.glVertex2f(0.9f, -0.9f)
gl.glTexCoord2f(1.0f, 1.0f)
gl.glVertex2f(0.9f, 0.9f)
gl.glTexCoord2f(0.0f, 1.0f)
gl.glVertex2f(-0.9f, 0.9f)
gl.glEnd()
}
}
override fun dispose(drawable: GLAutoDrawable?) {
drawable?.let {
val gl = drawable.gl.gL2
texture?.let {
it.destroy(gl)
}
}
}

var texture : Texture? = null

companion object {
//(1)
@JvmStatic
fun main(args: Array<String>) {
SwingUtilities.invokeLater { FirstStepSwing() }
}
}

init {
val caps = GLCapabilities(GLProfile.get(GLProfile.GL2)) //(2)
val frame = JFrame() //(3)
frame.title = "First demo (Swing)" //(4)
frame.addWindowListener(object : WindowAdapter() {
//(6)
override fun windowClosing(e: WindowEvent) {
System.exit(0)
}
})
val canvas = GLCanvas(caps)
canvas.setPreferredSize(Dimension(300, 300)) //(5)
canvas.addGLEventListener(this) //(7)
frame.add(canvas, BorderLayout.CENTER)
frame.setLocation(300, 300) //(9)
frame.pack()
frame.isVisible = true //(10)
}
}

New ProjectでKotlin→JVMを選択
Project nameを適当につけてFinish
srcフォルダにktファイルを作成
下記サイトのFirstStepSwing.javaをコピペし、kotlinコードに変換

File→Project Settings→Librariesでgluegen-rt.jarとjogl-all.jarを追加
init~dispose関数にoverrideキーワードを追加
dispose関数の中身を削除

package demos.basic

import com.jogamp.opengl.GLAutoDrawable
import com.jogamp.opengl.GLCapabilities
import com.jogamp.opengl.GLEventListener
import com.jogamp.opengl.GLProfile
import com.jogamp.opengl.awt.GLCanvas
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.SwingUtilities

class FirstStepSwing : GLEventListener {
override fun init(drawable: GLAutoDrawable?) {}
override fun reshape(drawable: GLAutoDrawable?, x: Int, y: Int, width: Int, height: Int) {}
override fun display(drawable: GLAutoDrawable?) {}
override fun dispose(drawable: GLAutoDrawable?) {}

companion object {
//(1)
@JvmStatic
fun main(args: Array<String>) {
SwingUtilities.invokeLater { FirstStepSwing() }
}
}

init {
val caps = GLCapabilities(GLProfile.get(GLProfile.GL2)) //(2)
val frame = JFrame() //(3)
frame.title = "First demo (Swing)" //(4)
frame.addWindowListener(object : WindowAdapter() {
//(6)
override fun windowClosing(e: WindowEvent) {
System.exit(0)
}
})
val canvas = GLCanvas(caps)
canvas.setPreferredSize(Dimension(300, 300)) //(5)
canvas.addGLEventListener(this) //(7)
frame.add(canvas, BorderLayout.CENTER)
frame.setLocation(300, 300) //(9)
frame.pack()
frame.isVisible = true //(10)
}
}

package demos.basic

import com.jogamp.opengl.*
import com.jogamp.opengl.GL2.GL_POLYGON
import com.jogamp.opengl.awt.GLCanvas
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.SwingUtilities

class FirstStepSwing : GLEventListener {
override fun init(drawable: GLAutoDrawable?) {
drawable?.let {
val gl = it.gl.gL2
gl.glClearColor(0.0f, 0.0f, 1.0f, 1.0f)
}
}
override fun reshape(drawable: GLAutoDrawable?, x: Int, y: Int, width: Int, height: Int) {}
override fun display(drawable: GLAutoDrawable?) {
drawable?.let {
val gl = it.gl.gL2
gl.glClear(GL.GL_COLOR_BUFFER_BIT)
gl.glBegin(GL_POLYGON)
gl.glColor3f(1.0f, 0.0f, 0.0f)
gl.glVertex2f(-0.9f, -0.9f)
gl.glColor3f(0.0f, 1.0f, 0.0f)
gl.glVertex2f(0.9f, -0.9f)
gl.glColor3f(0.0f, 0.0f, 1.0f)
gl.glVertex2f(0.9f, 0.9f)
gl.glColor3f(1.0f, 1.0f, 0.0f)
gl.glVertex2f(-0.9f, 0.9f)
gl.glEnd()
}
}
override fun dispose(drawable: GLAutoDrawable?) {}

companion object {
//(1)
@JvmStatic
fun main(args: Array<String>) {
SwingUtilities.invokeLater { FirstStepSwing() }
}
}

init {
val caps = GLCapabilities(GLProfile.get(GLProfile.GL2)) //(2)
val frame = JFrame() //(3)
frame.title = "First demo (Swing)" //(4)
frame.addWindowListener(object : WindowAdapter() {
//(6)
override fun windowClosing(e: WindowEvent) {
System.exit(0)
}
})
val canvas = GLCanvas(caps)
canvas.setPreferredSize(Dimension(300, 300)) //(5)
canvas.addGLEventListener(this) //(7)
frame.add(canvas, BorderLayout.CENTER)
frame.setLocation(300, 300) //(9)
frame.pack()
frame.isVisible = true //(10)
}
}

↑このページのトップヘ