JOGLによるOpenGL入門
|
|
|
- なぎさ あさま
- 8 years ago
- Views:
Transcription
1 OpenGL と GLUT を組み合わせれば UNIX 系 OS(Linux FreeBSD 等を含む ) と Windows と Mac のいずれでも動く リアルタイムに三次元表示を行うプログラムが とっても簡単に書けてしまう という三拍子そろったメリットが得られます
2 org.jogamp.jogl jogl-all-main 2.*.* grep -lr 'javax\.media\.opengl' --include="*.java" * xargs sed -i ".bak" -e 's/javax\.media\.opengl/com\.jogamp\.opengl/g' package demos.basic; import com.jogamp.opengl.gl; import com.jogamp.opengl.glautodrawable;
3 import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.animator; public class FirstStepNewt implements GLEventListener { //(1) public static void main(string[] args) { new FirstStepNewt(); public FirstStepNewt() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2));//(2) GLWindow glwindow = GLWindow.create(caps); //(3) glwindow.settitle("first demo (Newt)"); //(4) glwindow.setsize(300, 300); //(5) glwindow.addwindowlistener(new WindowAdapter() { //(6) public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(this); //(7) Animator animator = new Animator(); //(8) animator.add(glwindow); animator.start(); glwindow.setvisible(true); //(10) public void reshape(glautodrawable drawable, int x, int y, int width, int height) { public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); package demos.basic; import java.awt.borderlayout; import java.awt.dimension; import java.awt.event.windowadapter; import java.awt.event.windowevent; import com.jogamp.opengl.gl; 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 javax.swing.jframe; import javax.swing.swingutilities; public class FirstStepSwing implements GLEventListener { //(1) public static void main(string[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new FirstStepSwing(); ); public FirstStepSwing() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); //(2) JFrame frame = new JFrame(); //(3) frame.settitle("first demo (Swing)"); //(4) frame.addwindowlistener(new WindowAdapter() { //(6) public void windowclosing(windowevent e) { System.exit(0); ); GLCanvas canvas = new GLCanvas(caps); canvas.setpreferredsize(new Dimension(300, 300)); //(5) canvas.addgleventlistener(this); //(7) frame.add(canvas, BorderLayout.CENTER);
4 frame.setlocation(300, 300); //(9) frame.pack(); frame.setvisible(true); //(10) public void reshape(glautodrawable drawable, int x, int y, int width, int height) { public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); import com.jogamp.opengl.awt.gljpanel; //GLCanvas canvas = new GLCanvas(caps); コメントアウト GLJPanel canvas = new GLJPanel(caps); int main(int argc, char *argv[]) { glutinit(&argc, argv); glutcreatewindow(argv[0]); glutdisplayfunc(display); glutmainloop(); return 0; public void init(glautodrawable drawable); public void reshape(glautodrawable drawable int x,int y, int width, int height); public void display(glautodrawable drawable); public void dispose(glautodrawable drawable); GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); //(2) GLWindow glwindow = GLWindow.create(caps); //(3) glwindow.settitle("first demo (Newt)"); //(4) glwindow.setsize( ); //(5) glwindow.addwindowlistener(new WindowAdapter() { //(6) public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(this); //(7) Animator animator = new Animator(); //(8)
5 animator.add(glwindow); animator.start(); glwindow.setposition(500, 500); //(9) glwindow.setvisible(true); //(10) JFrame frame = new JFrame(); //(3) frame.settitle("first demo (Swing)"); //(4) frame.addwindowlistener(new WindowAdapter() { //(6) public void windowclosing(windowevent e) { System.exit(0); ); GLCanvas canvas = new GLCanvas(caps); canvas.setpreferredsize(new Dimension(300, 300)); //(5) canvas.addgleventlistener(this); //(7) frame.add(canvas BorderLayout.CENTER); frame.setlocation(500, 500); //(9) frame.pack(); frame.setvisible(true);//(10) Animator animator = new Animator(); //(8) animator.add(glwindow); animator.start(); import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; //( 省略 ) public class FirstStepNewt implements GLEventListener { //(1) public static void main(string[] args) { new FirstStepNewt(); public FirstStepNewt() { // 変更なし G L 2 g l = d r a w a b l e. g e t G L ( ). g e t G L 2 ( ) ; / / 追加 / / ウィンドウを青く塗りつぶす g l. g l C l e a r C o l o r ( 0. 0 f, 0. 0 f, 1. 0 f, 1. 0 f ) ; / / 追加 public void reshape(glautodrawable drawable, int x, int y, int width, int height) { G L 2 g l = d r a w a b l e. g e t G L ( ). g e t G L 2 ( ) ; / / 追加 g l. g l C l e a r ( G L. G L _ C O L O R _ B U F F E R _ B I T ) ; / / 追加 public void dispose(glautodrawable drawable) { G L 2 g l = d r a w a b l e. g e t G L ( ). g e t G L 2 ( ) ;
6 g l. g l C l e a r C o l o r ( 0. 0 f, 0. 0 f, 1. 0 f, 1. 0 f ) ; v o i d g l C l e a r C o l o r ( f l o a t r, f l o a t g, f l o a t b, f l o a t a ) g l. g l C l e a r ( G L. G L _ C O L O R _ B U F F E R _ B I T ) ; i m p o r t s t a t i c c o m. j o g a m p. o p e n g l. G L. * ; / / " * " とすると G L クラスで定義されている全ての s t a t i c 定数がインポートされる g l. g l C l e a r B u f f e r ( G L _ C O L O R _ B U F F E R _ B I T G L _ D E P T H _ B U F F E R _ B I T G L _ S T E N C I L _ B U F F E R _ B I T ) ; //GL を省略可能 g l. g l C l e a r B u f f e r ( G L. G L _ C O L O R _ B U F F E R _ B I T G L _ D E P T H _ B U F F E R _ B I T G L _ S T E N C I L _ B U F F E R _ B I T ) ; g l F l u s h ( ) i m p o r t s t a t i c c o m. j o g a m p. o p e n g l. G L 2. * ;// 追加 public class FirstStepNewt implements GLEventListener { //(1) public static void main(string[] args) { new FirstStepNewt(); public FirstStepNewt() { // 変更なし // 変更なし public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glclear(gl.gl_color_buffer_bit); // 以下を追加 g l. g l B e g i n ( G L _ L I N E _ L O O P ) ; g l. g l V e r t e x 2 f ( f, f ) ; g l. g l V e r t e x 2 f ( 0. 9 f, f ) ; g l. g l V e r t e x 2 f ( 0. 9 f, 0. 9 f ) ; g l. g l V e r t e x 2 f ( f, 0. 9 f ) ; g l. g l E n d ( ) ; // ここまでを追加 public void dispose(glautodrawable drawable) {
7 v o i d g l B e g i n ( i n t m o d e ) v o i d g l E n d ( ) v o i d g l V e r t e x 2 f ( f l o a t x, f l o a t y ) v o i d g l V e r t e x 2 d ( d o u b l e x, d o u b l e y ) G L _ P O I N T S G L _ L I N E S G L _ L I N E _ S T R I P G L _ L I N E _ L O O P G L _ T R I A N G L E S / G L _ Q U A D S G L _ T R I A N G L E _ S T R I P / G L _ Q U A D _ S T R I P G L _ T R I A N G L E _ F A N G L _ P O L Y G O N
8 package demos.basic; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; import static com.jogamp.opengl.gl2.*; public class LineAndPointNewt1 implements GLEventListener { public static void main(string[] args){ new LineAndPointNewt1(); private float[] colors; private final short linepattern = 0b ; // 破線のパターンを定義 (1) public LineAndPointNewt1() { initcolors(); GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("line and point (Newt)"); glwindow.setsize(400, 300); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(this); FPSAnimator animator = new FPSAnimator(60); animator.add(glwindow); animator.start(); glwindow.setvisible(true); private void initcolors() { colors = new float[8]; for(int i = 0; i < 8; i++) { colors[i] = 0.3f + (0.1f * i); final GL2 gl2 = drawable.getgl().getgl2(); gl2.glclear(gl_color_buffer_bit); // 大きさを変えて 点を描く for(int i = 0; i < 8; i++) { gl2.glpointsize((i + 1) * 0.5f); //(2) gl2.glcolor3f(1.0f, 1.0f, 1.0f); gl2.glbegin(gl_points); //(3) gl2.glvertex2f(-0.9f, (i-7)*(1.6f/7f) + 0.8f); gl2.glend(); // 灰色の濃度を変えて 点を描く for(int i = 0; i < 8; i++) { gl2.glpointsize(2f); gl2.glbegin(gl_points); gl2.glcolor3f(colors[i], colors[i], colors[i]); //-0.8 から +0.8 の範囲になるよう計算 gl2.glvertex2f(-0.8f, (i-7)*(1.6f/7f) + 0.8f); gl2.glend(); // 太さを変えて 線を描く for(int i = 1; i < 9; i++) { gl2.gllinewidth(i * 0.5f); //(4) gl2.glcolor3f(1.0f, 1.0f, 1.0f); gl2.glbegin(gl_lines); gl2.glvertex2f(-0.6f + i*0.05f, -0.8f); gl2.glvertex2f(-0.6f + i*0.05f, +0.8f); gl2.glend(); // 灰色の濃度を変えて 線を描く gl2.gllinewidth(1f); for(int i = 0; i < 8; i++) { gl2.glcolor3f(colors[i], colors[i], colors[i]); gl2.glbegin(gl_lines); gl2.glvertex2f(-0.1f + i*0.05f, -0.8f); gl2.glvertex2f(-0.1f + i*0.05f, +0.8f); gl2.glend(); // 破線の色と 破線のスケールを変えて 線を描く gl2.glenable(gl_line_stipple); // 破線を描くことを設定 (5) for(int i = 0; i < 8; i++) { gl2.gllinestipple(i+1, linepattern); //(6) gl2.glcolor3f(colors[i], colors[i], colors[i]);
9 gl2.glbegin(gl_lines); gl2.glvertex2f(+0.4f + i*0.05f, -0.8f); gl2.glvertex2f(+0.4f + i*0.05f, +0.8f); gl2.glend(); gl2.gldisable(gl_line_stipple);// 破線を描く設定を解除 (7) GL2 gl2 = drawable.getgl().getgl2(); gl2.glclearcolor(0.0f, 0.0f, 0.0f, 0.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); v o i d g l P o i n t S i z e ( f l o a t s i z e ) java.nio.doublebuffer glgetbuf = com.jogamp.common.nio.buffers.newdirectfloatbuffer(bufsize); gl2.glgetfloatv(paramtype, glgetbuf); glgetbuf.rewind(); System.out.print(paramName + ":"); for(int i = 0; i < bufsize; i++) { System.out.print(glGetBuf.get(i) + ", "); System.out.println(); void glgetbooleanv(int pname, java.nio.booleanbuffer buf); void glgetdoublev(int pname, java.nio.doublebuffer buf); void glgetfloatv(int pname, java.nio.floatbuffer params); void glgetintegerv(int pname, java.nio.intbuffer buf); v o i d g l L i n e W i d t h ( f l o a t w i d t h ) v o i d g l E n a b l e ( i n t m o d e ) v o i d g l D i s a b l e ( i n t m o d e ) v o i d g l L i n e S t i p p l e ( i n t f a c t o r s h o r t p a t t e r n ) public class FirstStepNewt implements GLEventListener { //(1) public static void main(string[] args) { new FirstStepNewt(); public FirstStepNewt() { // 変更なし // 変更なし public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glclear(gl.gl_color_buffer_bit); g l. g l C o l o r 3 f ( 1. 0 f, 0. 0 f, 0. 0 f ) ; // この行を追加 1.0f, 0.0f のように 'f' を付けていることに注意 gl.glbegin(gl_line_loop); gl.glvertex2f(-0.9f,-0.9f); gl.glvertex2f(0.9f, -0.9f); gl.glvertex2f(0.9f, 0.9f); gl.glvertex2f(-0.9f, 0.9f); gl.glend(); public void dispose(glautodrawable drawable) {
10 v o i d g l C o l o r 3 f ( f l o a t r, f l o a t g, f l o a t b ) i m p o r t s t a t i c c o m. j o g a m p. o p e n g l. G L 2. G L _ P O L Y G O N ; / / 追加 //( 省略 ) public class FirstStepNewt implements GLEventListener { //(1) public static void main(string[] args) { new FirstStepNewt(); public FirstStepNewt() { // 変更なし // ウィンドウを青く塗りつぶす gl.glclearcolor(0f, 0f, 1.0f, 1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glclear(gl.gl_color_buffer_bit); gl.glcolor3f(1.0f, 0.0f, 0.0f); gl.glbegin( G L _ P O L Y G O N ) ;// 変更 gl.glvertex2f(-0.9f,-0.9f); gl.glvertex2f(0.9f, -0.9f); gl.glvertex2f(0.9f, 0.9f); gl.glvertex2f(-0.9f, 0.9f); gl.glend(); public void dispose(glautodrawable drawable) {
11 gl.glclear(gl.gl_color_buffer_bit); //gl.glcolor3f(1.0f, 0.0f, 0.0f); // ここは削除 gl.glbegin(gl_polygon); g l. g l C o l o r 3 f ( 1. 0 f, 0. 0 f, 0. 0 f ) ; // 赤 gl.glvertex2f(-0.9f,-0.9f); g l. g l C o l o r 3 f ( 0. 0 f, 1. 0 f, 0. 0 f ) ; // 緑 gl.glvertex2f(0.9f, -0.9f); g l. g l C o l o r 3 f ( 0. 0 f, 0. 0 f, 1. 0 f ) ; // 青 gl.glvertex2f(0.9f, 0.9f); g l. g l C o l o r 3 f ( 1. 0 f, 1. 0 f, 0. 0 f ) ; // 黄 gl.glvertex2f(-0.9f, 0.9f); gl.glend(); package demos.basic; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; import static com.jogamp.opengl.gl2.*; public class FirstStepNewt implements GLEventListener { public static void main(string[] args){ new FirstStepNewt(); public FirstStepNewt() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("first demo (Newt)"); glwindow.setsize(300, 300); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(new FirstStepNewt()); FPSAnimator animator = new FPSAnimator(10); //(2) animator.add(glwindow); animator.start(); glwindow.setvisible(true); // ウィンドウを青く塗りつぶす gl.glclearcolor(0f, 0f, 1f, 1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) {
12 gl.glclear(gl.gl_color_buffer_bit); //gl.glcolor3f(1.0f, 0.0f, 0.0f); // ここは削除 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(); public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); package demos.basic; import java.awt.geom.point2d; import java.awt.geom.point2d.float; import java.util.arraylist; import java.util.list; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.mouseevent; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; import static com.jogamp.opengl.gl2.*; public class NewtMouseHandleSample implements GLEventListener, com.jogamp.newt.event.mouselistener { //(1) public static void main(string[] args) { new NewtMouseHandleSample(); private final List points; public NewtMouseHandleSample() { points = new ArrayList<>(); GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); final GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("mouse Handle Sample (Newt)"); glwindow.setsize(300, 300); //(2) glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(this); glwindow.addmouselistener(this); //(3) FPSAnimator animator = new FPSAnimator(30); //(4) animator.add(glwindow); animator.start(); glwindow.setposition(500, 500); glwindow.setvisible(true); GL gl = drawable.getgl(); // 背景を白く塗りつぶす gl.glclearcolor(1f, 1f, 1f, 1.0f); public void reshape(glautodrawable drawable, int x,int y, int width, int height) { //gl.glviewport(x, y, width, height); //(5)Jogl 内部で実行済みなので不要 gl.glmatrixmode(gl_projection); //(6) 透視変換行列を指定 gl.glloadidentity(); //(7) 透視変換行列を単位行列にする System.out.printf("x:%d, y:%d, w:%d, h:%d, %n", x, y, width, height); // これによりウィンドウをリサイズしても中の図形は大きさが維持される // また 第 3 第 4 引数を入れ替えることによりGLWindowの座標系 ( 左上隅が原点 ) とデバイス座標系 ( 左下隅が原点 ) の違いを吸収している gl.glorthof(x, x + width, y + height, y, -1.0f, 1.0f); //(8) gl.glmatrixmode(gl_modelview); //(9) モデルビュー変換行列を指定 gl.glloadidentity(); //(10) モデルビュー変換行列を単位行列にする gl.glclear(gl_color_buffer_bit); gl.glcolor3f(1.0f, 0.0f,0.0f); // 赤
13 gl.glbegin(gl_lines); //p1 のところで +1 しているので i が範囲を超えないようループ回数を一つ減らしている for(int i = 0; i < points.size() - 1; i++) { //(11) Point2D.Float p0 = (Float) points.get(i); Point2D.Float p1 = (Float) points.get(i + 1); gl.glvertex2d(p0.getx(), p0.gety()); // 今の位置 gl.glvertex2d(p1.getx(), p1.gety()); // 次の位置 gl.glend(); public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); // ここから下で com.jogamp.newt.event.mouselistener インターフェースのメソッドを実装 (12) public void mouseclicked(com.jogamp.newt.event.mouseevent e) { System.out.printf("%d, %d%n", e.getx(), e.gety()); points.add(new Point2D.Float(e.getX(), e.gety())); //(13) public void mouseentered(com.jogamp.newt.event.mouseevent e) { public void mouseexited(com.jogamp.newt.event.mouseevent e) { public void mousepressed(com.jogamp.newt.event.mouseevent e) { public void mousereleased(com.jogamp.newt.event.mouseevent e) { public void mousemoved(com.jogamp.newt.event.mouseevent e) { public void mousedragged(com.jogamp.newt.event.mouseevent e) { public void mousewheelmoved(com.jogamp.newt.event.mouseevent e) { public void mouseclicked(com.jogamp.newt.event.mouseevent e); public void mouseentered(com.jogamp.newt.event.mouseevent e); public void mouseexited(com.jogamp.newt.event.mouseevent e); public void mousepressed(com.jogamp.newt.event.mouseevent e); public void mousereleased(com.jogamp.newt.event.mouseevent e); public void mousemoved(com.jogamp.newt.event.mouseevent e); public void mousedragged(com.jogamp.newt.event.mouseevent e); public void mousewheelmoved(com.jogamp.newt.event.mouseevent e); public class NewtMouseHandleSample implements GLEventListener {// com.jogamp.newt.event.mouselistener を削除 //glwindow.addmouselistener(this);//(3) 削除 glwindow.addmouselistener(new com.jogamp.newt.event.mouseadapter() { public void mouseclicked(com.jogamp.newt.event.mouseevent e) { System.out.printf("%d, %d%n", e.getx(), e.gety()); points.add(new Point2D.Float(e.getX(), e.gety()));
14 ); //( 省略 ) // ここから下で com.jogamp.newt.event.mouselistenerインターフェースのメソッドを実装 (12) // これ以降のMouseListenerインターフェースを実装していた部分を削除 // // public void mouseclicked(com.jogamp.newt.event.mouseevent e) { //(13) // System.out.printf("%d, %d%n", e.getx(), e.gety()); // points.add(new Point2D.Float(e.getX(), e.gety())); // // // // public void mousereleased(com.jogamp.newt.event.mouseevent e) { // System.out.println("mouse released"); // // // public void mouseentered(com.jogamp.newt.event.mouseevent e) { // // // public void mouseexited(com.jogamp.newt.event.mouseevent e) { // // // public void mousepressed(com.jogamp.newt.event.mouseevent e) { // // // public void mousemoved(com.jogamp.newt.event.mouseevent e) { // // // public void mousedragged(com.jogamp.newt.event.mouseevent e) { // // // public void mousewheelmoved(com.jogamp.newt.event.mouseevent e) { int getx(); int gety(); short getclickcount(); short getbutton(); int getmodifiers(); float[] getrotation(); float getrotationscale(); float[] getrotationxyz(float rotationxory, int mods); float getmaxpressure(); float[] getallpressures(); float getpressure(boolean normalized); float getpressure(int index, boolean normalized); public final boolean isaltdown(); public final boolean isaltgraphdown(); public final boolean iscontroldown(); public final boolean ismetadown(); public final boolean isshiftdown(); glwindow.addmouselistener(new com.jogamp.newt.event.mouseadapter() { public void mouseclicked(com.jogamp.newt.event.mouseevent e) { //(4) System.out.printf("%d, %d%n", e.getx(), e.gety()); points.add(new Point2D.Float(e.getX(), e.gety())); System.out.println("mouse clicked count:" + e.getclickcount()); System.out.println("mouse source :" + e.getbutton()); System.out.println("mouse button 1:" + MouseEvent.BUTTON1); System.out.println("mouse button 2 :" + MouseEvent.BUTTON2); System.out.println("mouse button 3 :" + MouseEvent.BUTTON3); ); public void mousereleased(com.jogamp.newt.event.mouseevent e) { System.out.println("mouse released"); public void reshape(glautodrawable drawable, int x,int y, int width, int height) { //gl.glviewport(x, y, width, height); //(5)Jogl 内部で実行済みなので不要 gl.glmatrixmode(gl_projection); //(6) 透視変換行列を指定 gl.glloadidentity(); //(7) 透視変換行列を単位行列にする System.out.printf("x:%d, y:%d, w:%d, h:%d, %n", x, y, width, height);
15 // これによりウィンドウをリサイズしても中の図形は大きさが維持される // また 第 3 第 4 引数を入れ替えることにより GLWindow の座標系 ( 左上隅が原点 ) とデバイス座標系 ( 左下隅が原点 ) の違いを吸収している gl.glorthof(x, x + width, y + height, y, -1.0f, 1.0f); //(8) gl.glmatrixmode(gl_modelview); //(9) モデルビュー変換行列を指定 gl.glloadidentity(); //(10) モデルビュー変換行列を単位行列にする v o i d g l V i e w p o r t ( i n t x, i n t y, i n t w i d t h, i n t h e i g h t ) For efficiency the GL viewport has already been updated via glviewport(x, y, width, height) when this method is called. v o i d g l M a t r i x M o d e ( i n t m o d e ) FPSAnimator animator = new FPSAnimator(30); //(4) v o i d g l L o a d I d e n t i t y ( ) v o i d g l O r t h o f ( f l o a t l e f t, f l o a t r i g h t, f l o a t b o t t o m, f l o a t t o p, f l o a t n e a r, f l o a t f a r )
16 x' a e i m x y' b f j n y = z' c g k o z w' d h l p w gl.gltranslatef(δx, ΔY, ΔZ); x' ΔX x y' ΔY y = z' ΔZ z w' w gl.glrotatef(φ, 0, 0, 1); //z 軸による回転
17 x' y' z' w' = cosφ -sinφ 0 0 sinφ cosφ x y z w gl.glrotatef(θ, 1, 0, 0); //X 軸による回転 x' y' z' w' = cosθ -sinθ 0 0 sinθ cosθ x y z w gl.glrotatef(ψ, 0, 1, 0); //Y 軸による回転 x' y' z' w' = cosψ 0 sinψ sinψ 0 cosψ x y z w x' y' z' w' = cosγ sinγ 0 0 -sinγ cosγ cosβ sinβ 0 0 -sinβ cosβ cosα sinα 0 0 -sinα cosα x y z w = cosγ sinγcosβ sinγsinβ 1 -sinγ cosγcosβ cosγsinβ 0 0 -sinβ cosβ cosα -sinα 0 0 sinα cosα x y z w = cosγcosβ-cosβsinαsinγ cosγsinα+cosβcosαsinγ sinγsinβ 0 -sinγcosα-cosβsinαcosγ -sinγsinα+cosβcosαcosγ cosγsinβ 0 sinβsinα -sinβcosα cosβ x y z w x' y' z' w' = cosφ -sinφ 0 0 sinφ cosφ cosθ 0 sinθ sinθ 0 cosθ cosψ -sinψ 0 0 sinψ cosψ x y z w = cosθ 0 sinθ 0 sinφsinθ cosφ -sinφcosθ 0 -cosφsinθ sinφ cosφcosθ cosψ -sinψ 0 0 sinψ cosψ x y z w = cosθcosψ -cosθsinψ sinθ 0 sinφsinθcosψ+cosφsinψ -sinφsinθsinψ+cosφcosψ -sinφcosθ 0 -cosφsinθcosψ+sinφsinψ cosφsinθsinψ+-sinφcosψ cosφcosθ x y z w
18 gl.glscalef(0.5f, 1.0f, 1.0f); //X 方向を1/2 gl.glscalef(2.0f, 1.0f, 1.0f); //X 方向を2 倍 gl.glscalef(1.0f, 0.5f, 1.0f); //Y 方向を1/2 gl.glscalef(1.0f, 2.0f, 1.0f); //Y 方向を2 倍 gl.glscalef(1.0f, -1.0f, 1.0f); //Y 方向を反転 x' scalex x y' 0 scaley 0 0 y = z' 0 0 scalez 0 z w' w gl.gltranslatef(x, y, z); // 平行移動 gl.glrotatef(rot, ax, ay, zz); // 回転 gl.glscalef(sx, sy, sz); // 拡大 縮小 package demos.basic; import java.awt.geom.point2d; import java.awt.geom.point2d.float; import java.util.arraylist; import java.util.list; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; import static com.jogamp.opengl.gl2.*; public class NewtMouseDraggingSample implements GLEventListener, com.jogamp.newt.event.mouselistener { //(1) public static void main(string[] args) { new NewtMouseDraggingSample(); private final List> pointslist; private final List points; public NewtMouseDraggingSample() { pointslist = new ArrayList<>(); points = new ArrayList<>(); GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); final GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("mouse Drag Sample (Newt)");
19 glwindow.setsize(300, 300); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(this); glwindow.addmouselistener(this); //(2) FPSAnimator animator = new FPSAnimator(60); animator.add(glwindow); animator.start(); glwindow.setposition(500, 500); glwindow.setvisible(true); //showglinfo(drawable); GL gl = drawable.getgl(); // 背景を白く塗りつぶす gl.glclearcolor(1f, 1f, 1f, 1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glmatrixmode(gl_projection);// 透視変換行列を指定 gl.glloadidentity();// 透視変換行列を単位行列にする System.out.printf("x:%d, y:%d, w:%d, h:%d, %n", x, y, width, height); gl.glorthof(x, x + width, y + height, y, -1.0f, 1.0f); gl.glmatrixmode(gl_modelview);// モデルビュー変換行列を指定 gl.glloadidentity();// モデルビュー変換行列を単位行列にする gl.glclear(gl_color_buffer_bit); gl.glcolor3f(1.0f, 0.0f,0.0f); // 赤 // 現在ドラッグ中の図形を描画 (3) render(gl, points); // 前の図形を描画 (4) for(list p : pointslist) { render(gl, p); private void render(gl2 gl, List p) { gl.glbegin(gl_lines); //p1 のところで +1 しているので i が範囲を超えないようループ回数を一つ減らしている for(int i = 0; i < p.size() - 1; i++) { Point2D.Float p0 = (Float) p.get(i); Point2D.Float p1 = (Float) p.get(i + 1); gl.glvertex2d(p0.getx(), p0.gety()); // 今の位置 gl.glvertex2d(p1.getx(), p1.gety()); // 次の位置 gl.glend(); public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); // ここから下で com.jogamp.newt.event.mouselistener インターフェースのメソッドを実装 (5) public void mousedragged(com.jogamp.newt.event.mouseevent e) { points.add(new Point2D.Float(e.getX(), e.gety())); //(6) public void mousereleased(com.jogamp.newt.event.mouseevent e) { pointslist.add(new ArrayList(points)); //(7) points.clear(); public void mouseclicked(com.jogamp.newt.event.mouseevent e) { public void mouseentered(com.jogamp.newt.event.mouseevent e) { public void mouseexited(com.jogamp.newt.event.mouseevent e) { public void mousepressed(com.jogamp.newt.event.mouseevent e) { public void mousemoved(com.jogamp.newt.event.mouseevent e) { public void mousewheelmoved(com.jogamp.newt.event.mouseevent e) {
20 package demos.basic; import static com.jogamp.opengl.gl.*; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.keyevent; import com.jogamp.newt.event.keylistener; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; public class NewtMouseWheelSample implements GLEventListener, KeyListener { private static final char KEY_ESC = 0x1b; private static final float INIT_SCALE = 20f; private static final int HEIGHT = 300; private static final int WIDTH = 300; public static void main(string[] args) { new NewtMouseWheelSample(); private float scale = INIT_SCALE; public NewtMouseWheelSample() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); final GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("mouse wheel sample (Newt)"); glwindow.setsize(width, HEIGHT); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent arg0) { System.exit(0); ); glwindow.addgleventlistener(this); glwindow.addmouselistener(new com.jogamp.newt.event.mouseadapter() { public void mousewheelmoved(com.jogamp.newt.event.mouseevent e) { //(1) float[] rot = e.getrotation(); //(2) scale *= (rot[1] > 0? 1.005f : 0.995f); //(3) System.out.println("scale:" + scale); ); glwindow.addkeylistener(this); FPSAnimator animator = new FPSAnimator(10); animator.add(glwindow); animator.start(); glwindow.setposition(500, 500); glwindow.setvisible(true); //showglinfo(drawable); GL gl = drawable.getgl(); // 背景を白く塗りつぶす gl.glclearcolor(1f, 1f, 1f, 1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glorthof(x, x + width, y, y + height, -1.0f, 1.0f); //(4) gl.glclear(gl_color_buffer_bit); gl.glcolor3f(1.0f, 0.0f,0.0f); // 赤 gl.glbegin(gl_line_loop); gl.glvertex2f(width/2 - scale, HEIGHT/2 - scale); //(5) gl.glvertex2f(width/2 + scale, HEIGHT/2 - scale); gl.glvertex2f(width/2 + scale, HEIGHT/2 + scale); gl.glvertex2f(width/2 - scale, HEIGHT/2 + scale); gl.glend(); public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); public void keypressed(keyevent e) { public void keyreleased(keyevent e) { char keychar = e.getkeychar(); if( keychar == ' ' ) { scale = INIT_SCALE; System.out.println("scale:" + scale); if(keychar == 'q' keychar == KEY_ESC) System.exit(0);
21 package demos.basic; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.keyevent; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; public class NewtKeyboardInputSample implements GLEventListener, com.jogamp.newt.event.keylistener { //(1) private static final char KEY_ESC = 0x1b; public static void main(string[] args) { new NewtKeyboardInputSample(); private final GLWindow glwindow; public NewtKeyboardInputSample() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); glwindow = GLWindow.create(caps); glwindow.settitle("keyboard input demo (Newt)"); glwindow.setsize(300, 300); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(this); glwindow.addkeylistener(this); //(2) FPSAnimator animator = new FPSAnimator(10); animator.add(glwindow); animator.start(); glwindow.setvisible(true); // 背景を白く塗りつぶす gl.glclearcolor(1f, 1f, 1f, 1.0f); showglinfo(drawable); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { public void dispose(glautodrawable drawable) { System.out.println("dispose()"); if(animator!= null) animator.stop(); // ここから下で com.jogamp.newt.event.mouselistener インターフェースのメソッドを実装 (3) public void keypressed(keyevent e) { char keychar = e.getkeychar(); printkeychar(keychar, "pressed"); if(e.isaltdown()) { System.out.println("ALT key pressed"); if(e.isshiftdown()) { System.out.println("Shift key pressed"); if(e.iscontroldown()) {
22 System.out.println("Ctrl key pressed"); if(e.isaltgraphdown()) { System.out.println("AltGraph key pressed"); if(e.ismetadown()) { System.out.println("Meta key pressed"); public void keyreleased(keyevent e) { char keychar = e.getkeychar(); //(4) printkeychar(keychar, "released"); //(5) if(character.isisocontrol(keychar)) { System.out.println(Integer.valueOf(keyChar) + " released"); else { System.out.println(keyChar + " released"); if(e.isaltdown()) { System.out.println("ALT key released"); if(e.isshiftdown()) { System.out.println("Shift key released"); if(e.iscontroldown()) { System.out.println("Ctrl key released"); if(e.isaltgraphdown()) { System.out.println("AltGraph key released"); if(e.ismetadown()) { System.out.println("Meta key released"); if(keychar == KEY_ESC keychar == 'q' keychar == 'Q' ) { //(6) glwindow.destroy(); private void printkeychar(char keychar, String type) { if(character.isisocontrol(keychar)) { System.out.println(Integer.valueOf(keyChar) + type); else { System.out.println(keyChar + type); glwindow.addkeylistener(new com.jogamp.newt.event.keyadapter() { public void keypressed(keyevent e) { public void keyreleased(keyevent e) { gl.glclear(gl.gl_color_buffer_bit); //gl.glcolor3f(1.0f, 0.0f, 0.0f); // ここは削除 g l. g l R o t a t e f ( f, 0 f, 1 f, 0 f ) ; // 追加 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();
23 g l R o t a t e f ( f l o a t a n g l e, f l o a t x, f l o a t y, f l o a t z ) float[][] vertex = { { 0.0f, 0.0f, 0.0f, /* A */ { 1.0f, 0.0f, 0.0f, /* B */ { 1.0f, 1.0f, 0.0f, /* C */ { 0.0f, 1.0f, 0.0f, /* D */ { 0.0f, 0.0f, 1.0f, /* E */ { 1.0f, 0.0f, 1.0f, /* F */ { 1.0f, 1.0f, 1.0f, /* G */ { 0.0f, 1.0f, 1.0f /* H */ ; int[][] edge = { { 0, 1, /* ア (A-B) */ { 1, 2, /* イ (B-C) */ { 2, 3, /* ウ (C-D) */ { 3, 0, /* エ (D-A) */ { 4, 5, /* オ (E-) */ { 5, 6, /* カ (-G) */ { 6, 7, /* キ (G-H) */ { 7, 4, /* ク (H-E) */ { 0, 4, /* ケ (A-E) */ { 1, 5, /* コ (B-) */ { 2, 6, /* サ (C-G) */ { 3, 7 /* シ (D-H) */ ; gl.glbegin(gl_lines); gl.glvertex3dv(vertex[edge[2][0]], 0); // 線分 " ウ " の一つ目の端点 "C" gl.glvertex3dv(vertex[edge[2][1]], 0); // 線分 " ウ " の二つ目の端点 "D" gl.glend(); package demos.basic; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; import static com.jogamp.opengl.gl2.*; public class CubeSample implements GLEventListener { public static void main(string[] args){ new NewtWireCube(); float[][] vertex = { { 0.0f, 0.0f, 0.0f, /* A */ { 1.0f, 0.0f, 0.0f, /* B */
24 ; { 1.0f, 1.0f, 0.0f, /* C */ { 0.0f, 1.0f, 0.0f, /* D */ { 0.0f, 0.0f, 1.0f, /* E */ { 1.0f, 0.0f, 1.0f, /* F */ { 1.0f, 1.0f, 1.0f, /* G */ { 0.0f, 1.0f, 1.0f /* H */ int[][] edge = { { 0, 1, /* ア (A-B) */ { 1, 2, /* イ (B-C) */ { 2, 3, /* ウ (C-D) */ { 3, 0, /* エ (D-A) */ { 4, 5, /* オ (E-) */ { 5, 6, /* カ (-G) */ { 6, 7, /* キ (G-H) */ { 7, 4, /* ク (H-E) */ { 0, 4, /* ケ (A-E) */ { 1, 5, /* コ (B-) */ { 2, 6, /* サ (C-G) */ { 3, 7 /* シ (D-H) */ ; public CubeSample() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("first demo (Newt)"); glwindow.setsize(300, 300); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addgleventlistener(this); FPSAnimator animator = new FPSAnimator(10); //(2) animator.add(glwindow); animator.start(); glwindow.setvisible(true); // 背景を白く塗りつぶす. gl.glclearcolor(1f, 1f, 1f, 1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { //gl.glviewport(0f, 0f, width, height); // Jogl 内部で実行済みなので不要 gl.glortho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); gl.glclear(gl.gl_color_buffer_bit); // 黒色を指定 gl.glcolor3f(0.0f, 0.0f, 0.0f); // 図形の描画 gl.glbegin(gl_lines); for (int i = 0; i < 12; i++) { gl.glvertex3fv(vertex[edge[i][0]], 0); //(1) gl.glvertex3fv(vertex[edge[i][1]], 0); //(1) gl.glend(); public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); g l V e r t e x 3 f v ( f l o a t [ ] v, i n t o f f s e t ) g l V e r t e x 3 f v ( j a v a. n i o. F l o a t B u f f e r v ) x 座標値 y 座標値 z 座標値の import com.jogamp.opengl.glu.glu;// 追加 p r i v a t e f i n a l G L U g l u ; / / 追加 public NewtWireCube() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); GLWindow glwindow = GLWindow.create(caps); g l u = n e w G L U ( ) ; / / 追加 glwindow.settitle("first demo (Newt)"); // 略 public void reshape(glautodrawable drawable, int x, int y, int width, int height) { //gl.glviewport(0f, 0f, width, height); // Jogl 内部で実行済みなので不要 //gl.glortho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); コメントアウト g l u. g l u P e r s p e c t i v e ( , ( d o u b l e ) w / ( d o u b l e ) h, 1. 0, ) ; g l. g l T r a n s l a t e f ( 0. 0 f, 0. 0 f, f ) ; G L U C/C++ 言語のGLUをJOGLに移植したもので com.jogamp.opengl.glu.gluパッケージとして提供されています 詳細は12 章で説明します G L U T ついでですが GLUTについても移植されていて com.jogamp.opengl.util.gl2.glutパッケージになっています 詳細は12 章で説明します g l u P e r s p e c t i v e ( f l o a t f o v y f l o a t a s p e c t f l o a t z N e a r f l o a t z F a r )
25 g l T r a n s l a t e f ( f l o a t x f l o a t y f l o a t z ) public void reshape(glautodrawable drawable, int x, int y, int width, int height) { //gl.glviewport(0f, 0f, width, height); // Jogl 内部で実行済みなので不要 //gl.glortho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); コメントアウト glu.gluperspective(30.0, (double)w / (double)h, 1.0, 100.0); gl.gltranslatef(0.0f, 0.0f, -5.0f); g l u L o o k A t ( 3. 0 f, 4. 0 f, 5. 0 f, 0. 0 f, 0. 0 f, 0. 0 f, 0. 0 f, 1. 0 f, 0. 0 f ) ; v o i d g l u L o o k A t ( f l o a t e x f l o a t e y f l o a t e z f l o a t c x f l o a t c y f l o a t c z f l o a t u x f l o a t u y f l o a t u z ) package demos.basic; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.opengl.glu.glu; import com.jogamp.newt.event.keyevent; import com.jogamp.newt.event.keylistener; import com.jogamp.newt.event.mouseevent; import com.jogamp.newt.event.mouselistener; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; import static com.jogamp.opengl.gl2.*; public class CubeSample2 implements GLEventListener, MouseListener, KeyListener { public static void main(string[] args){ new CubeSample2(); float[][] vertex = { { 0.0f, 0.0f, 0.0f, /* A */ { 1.0f, 0.0f, 0.0f, /* B */ { 1.0f, 1.0f, 0.0f, /* C */ { 0.0f, 1.0f, 0.0f, /* D */ { 0.0f, 0.0f, 1.0f, /* E */ { 1.0f, 0.0f, 1.0f, /* F */ { 1.0f, 1.0f, 1.0f, /* G */ { 0.0f, 1.0f, 1.0f /* H */ ; int[][] edge = { { 0, 1, /* ア (A-B) */ { 1, 2, /* イ (B-C) */ { 2, 3, /* ウ (C-D) */ { 3, 0, /* エ (D-A) */ { 4, 5, /* オ (E-) */ { 5, 6, /* カ (-G) */ { 6, 7, /* キ (G-H) */ { 7, 4, /* ク (H-E) */ { 0, 4, /* ケ (A-E) */ { 1, 5, /* コ (B-) */ { 2, 6, /* サ (C-G) */ { 3, 7 /* シ (D-H) */ ; private final GLU glu; p r i v a t e f i n a l A n i m a t o r a n i m a t o r ; //(1)
26 private final GLWindow glwindow; private boolean willanimatorpause = false; private static final char KEY_ESC = 0x1b; // 回転角 float r = 0; public CubeSample2() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); glu = new GLU(); glwindow = GLWindow.create(caps); glwindow.settitle("cube demo (Newt)"); glwindow.setsize(300, 300); glwindow.addgleventlistener(this); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { System.exit(0); ); glwindow.addmouselistener(this); glwindow.addkeylistener(this); a n i m a t o r = n e w A n i m a t o r ( ) ; //(2) a n i m a t o r.ad d (gl W i n d o w ) ; //(3) a n i m a t o r. s t a r t ( ) ; //(4) a n i m a t o r. p a u s e ( ) ; //(5) glwindow.setvisible(true); // 背景を白く塗りつぶす. gl.glclearcolor(1f, 1f, 1f, 1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glmatrixmode(gl_projection); gl.glloadidentity(); glu.gluperspective(30.0, (double)width / (double)height, 1.0, 300.0); // 視点位置と視線方向 glu.glulookat(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); gl.glmatrixmode(gl_modelview); gl.glclear(gl.gl_color_buffer_bit); gl.glloadidentity(); // 図形の回転 gl.glrotatef(r, 0.0f, 1.0f, 0.0f); //(6) // 図形の描画 gl.glcolor3f(0.0f, 0.0f, 0.0f); gl.glbegin(gl_lines); for (int i = 0; i < 12; i++) { gl.glvertex3fv(vertex[edge[i][0]], 0); gl.glvertex3fv(vertex[edge[i][1]], 0); gl.glend(); // 一周回ったら回転角を 0 に戻す if (r++ >= 360.0f) r = 0; System.out.println("anim:" + animator.isanimating() + ", r:" + r); if(willanimatorpause) { animator.pause(); //(8) System.out.println("animoator paused:"); willanimatorpause = false; public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); public void keypressed(keyevent e) { public void keyreleased(keyevent e) { char keychar = e.getkeychar(); if(keychar == KEY_ESC keychar == 'q' keychar == 'Q') { glwindow.destroy(); public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { public void mousepressed(mouseevent e) { switch(e.getbutton()) { case MouseEvent.BUTTON1: animator.resume(); //(9) System.out.println("button 1, left click"); break; case MouseEvent.BUTTON2: System.out.println("button 2"); break; case MouseEvent.BUTTON3: System.out.println("button 3, right click"); willanimatorpause = true; //(10) animator.resume(); //(11) break; default: //empty
27 public void mousereleased(mouseevent e) { animator.pause(); public void mousemoved(mouseevent e) { public void mousedragged(mouseevent e) { public void mousewheelmoved(mouseevent e) { F P S A n i m a t o r ( f i n a l G L A u t o D r a w a b l e d r a w a b l e, f i n a l i n t f p s, f i n a l b o o l e a n s c h e d u l e A t F i x e d R a t e ) F P S A n i m a t o r ( f i n a l i n t f p s ) F P S A n i m a t o r ( f i n a l i n t f p s, f i n a l b o o l e a n s c h e d u l e A t F i x e d R a t e ) F P S A n i m a t o r ( f i n a l G L A u t o D r a w a b l e d r a w a b l e, f i n a l i n t f p s ) p r i v a t e f i n a l F P S A n i m a t o r a n i m a t o r ; //( 省略 ) public CubeSample2() { //( 省略 ) glwindow.addmouselistener(this); glwindow.addkeylistener(this); a n i m a t o r = n e w F P S A n i m a t o r ( g l W i n d o w, 3 0, t r u e ) ; // 次の行をコメントアウトしないと "IllegalArgumentException: Drawable already added to animator" が投げられる //animator.add(glwindow); animator.start(); animator.pause(); glwindow.setvisible(true); // 図形の回転 g l. g l T r a n s l a t e f ( 0. 5 f, 0. 5 f, 0. 5 f ) ; / / 追加 gl.glrotatef(r, 0.0f, 1.0f, 0.0f); g l. g l T r a n s l a t e f ( f, f, f ) ; / / 追加 // 背景を白く塗りつぶす gl.glclearcolor(1f, 1f, 1f, 1.0f); S y s t e m. o u t. p r i n t l n ( " a u t o s w a p : " + d r a w a b l e. g e t A u t o S w a p B u f f e r M o d e ( ) ) ; / / 追加 d r a w a b l e. s e t A u t o S w a p B u f f e r M o d e ( f a l s e ) ; / / 追加 drawable.swapbuffers() // 省略 drawable.swapbuffers(); // 追加 glutswapbuffers()
28 d u m p T h r e a d ( " d i s p l a y " ) ; // 他は同じ public void mousedragged(com.jogamp.newt.event.mouseevent e) { d u m p T h r e a d ( " d r a g g i n g " ) ; // 他は同じ p r i v a t e v o i d d u m p T h r e a d ( S t r i n g n a m e ) { T h r e a d t h = T h r e a d. c u r r e n t T h r e a d ( ) ; S y s t e m. o u t. p r i n t l n ( n a m e + " : " + t h. g e t N a m e ( ) + ", " + t h. g e t S t a t e ( ) ) ; display:main-fpsawtanimator#00-timer0, RUNNABLE ( 実際にはこれが何度も表示される ) dragging:main-display-.macosx_nil-1-edt-1, RUNNABLE private final int face[][] = { { 0, 1, 2, 3, // A-B-C-D を結ぶ面 { 1, 5, 6, 2, // B-F-G-C を結ぶ面 { 5, 4, 7, 6, // F-E-H-G を結ぶ面 { 4, 0, 3, 7, // E-A-D-H を結ぶ面 { 4, 5, 1, 0, // E-F-B-A を結ぶ面 { 3, 2, 6, 7 // D-C-G-H を結ぶ面 ; package demos.basic; import com.jogamp.opengl.gl; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.opengl.glu.glu; import com.jogamp.newt.event.mouseevent; import com.jogamp.newt.event.mouselistener; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.fpsanimator; import static com.jogamp.opengl.gl2.*; public class CubeSample3 implements GLEventListener, MouseListener { public static void main(string[] args) { new CubeSample3(); private final float[][] vertex = { { 0.0f, 0.0f, 0.0f, /* A */ { 1.0f, 0.0f, 0.0f, /* B */ { 1.0f, 1.0f, 0.0f, /* C */ { 0.0f, 1.0f, 0.0f, /* D */ { 0.0f, 0.0f, 1.0f, /* E */ { 1.0f, 0.0f, 1.0f, /* F */ { 1.0f, 1.0f, 1.0f, /* G */ { 0.0f, 1.0f, 1.0f /* H */ ; private final int face[][] = { { 0, 1, 2, 3, // A-B-C-D を結ぶ面 { 1, 5, 6, 2, // B-F-G-C を結ぶ面 { 5, 4, 7, 6, // F-E-H-G を結ぶ面 { 4, 0, 3, 7, // E-A-D-H を結ぶ面 { 4, 5, 1, 0, // E-F-B-A を結ぶ面 { 3, 2, 6, 7 // D-C-G-H を結ぶ面 ; private final GLU glu; private final FPSAnimator animator; private boolean willanimatorpause = false; // 回転角 float r = 0; public CubeSample3() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); glu = new GLU(); GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("cube demo (Newt)"); glwindow.setsize(300, 300); glwindow.addwindowlistener(new WindowAdapter() { ); public void windowdestroyed(windowevent evt) { System.exit(0);
29 glwindow.addgleventlistener(this); glwindow.addmouselistener(this); animator = new FPSAnimator(30); animator.add(glwindow); animator.start(); animator.pause(); glwindow.setvisible(true); // 背景を白く塗りつぶす. gl.glclearcolor(1f, 1f, 1f, 1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glmatrixmode(gl_projection); gl.glloadidentity(); glu.gluperspective(30.0, (double)width / (double)height, 1.0, 300.0); glu.glulookat(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); gl.glmatrixmode(gl_modelview); gl.glclear(gl_color_buffer_bit); gl.glloadidentity(); // 図形の回転 gl.gltranslatef(0.5f, 0.5f, 0.5f); gl.glrotatef(r, 0.0f, 1.0f, 0.0f); gl.gltranslatef(-0.5f, -0.5f, -0.5f); // 図形の描画 gl.glcolor3f(0.0f, 0.0f, 0.0f); gl.glbegin(gl_quads); for (int j = 0; j < 6; ++j) { for (int i = 0; i < 4; ++i) { gl.glvertex3fv(vertex[face[j][i]], 0); gl.glend(); // 一周回ったら回転角を 0 に戻す if (r++ >= 360.0f) r = 0; System.out.println("anim:" + animator.isanimating() + ", r:" + r); if(willanimatorpause) { animator.pause(); System.out.println("animoator paused:"); willanimatorpause = false; public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { public void mousepressed(mouseevent e) { switch(e.getbutton()) { case MouseEvent.BUTTON1: animator.resume(); System.out.println("button 1, left click"); break; case MouseEvent.BUTTON2: System.out.println("button 2"); break; case MouseEvent.BUTTON3: System.out.println("button 3, right click"); willanimatorpause = true; animator.resume(); break; public void mousereleased(mouseevent e) { animator.pause(); public void mousemoved(mouseevent e) { public void mousedragged(mouseevent e) { public void mousewheelmoved(mouseevent e) {
30 private final float color[][] = { { 1.0f, 0.0f, 0.0f, // 赤 { 0.0f, 1.0f, 0.0f, // 緑 { 0.0f, 0.0f, 1.0f, // 青 { 1.0f, 1.0f, 0.0f, // 黄 { 1.0f, 0.0f, 1.0f, // マゼンタ { 0.0f, 1.0f, 1.0f // シアン ; // 図形の描画 / / g l. g l C o l o r 3 f ( 0. 0 f, 0. 0 f, 0. 0 f ) ; / / コメントアウト gl.glbegin(gl_quads); for (int j = 0; j < 6; ++j) { g l. g l C o l o r 3 f v ( c o l o r [ j ], 0 ) ; / / 追加 for (int i = 0; i < 4; ++i) { gl.glvertex3fv(vertex[face[j][i]], 0); gl.glend(); v o i d g l C o l o r 3 f v ( f l o a t [ ] v, i n t o f f s e t ) このプログラムでは常にデプスバッファを使うので init() の中でglEnable(GL_DEPTH_TEST) を一度だけ実行し gldisable(gl_depth_test) の実行を省略しています CubeSample3.javaを以下のように修正します // 背景を白く塗りつぶす gl.glclearcolor(1f, 1f, 1f, 1.0f); g l. g l E n a b l e ( G L _ D E P T H _ T E S T ) ; / / 追加 g l. g l C l e a r ( G L _ C O L O R _ B U F F E R _ B I T G L _ D E P T H _ B U F F E R _ B I T ) ; / / G L _ D E P T H _ B U F F E R _ B I T を追加 //( 省略 )
31 // 背景を白く塗りつぶす. gl.glclearcolor(1f, 1f, 1f, 1.0f); gl.glenable(gl_depth_test); gl.glenable(gl_cull_face);// 追加 gl.glcullface(gl_back);// 追加 private final float normal[][] = { { 0.0f, 0.0f, -1.0f, { 1.0f, 0.0f, 0.0f, { 0.0f, 0.0f, 1.0f, {-1.0f, 0.0f, 0.0f, { 0.0f, -1.0f, 0.0f, { 0.0f, 1.0f, 0.0f ; // 背景を白く塗りつぶす. gl.glclearcolor(1f, 1f, 1f, 1.0f); gl.glenable(gl_depth_test); gl.glenable(gl_cull_face); g l. g l C u l l F a c e ( G L _ F R O N T ) ; / / G L _ B A C K から変更 gl.glenable(gl_lighting);// 追加 gl.glenable(gl_light0);// 追加 gl.glenable(gl_light1);// 追加 // 省略 gl.glbegin(gl_quads); for (int j = 0; j < 6; ++j) { // g l. g l C o l o r 3 f v ( c o l o r [ j ], 0 ) ; / / コメントアウト g l. g l N o r m a l 3 f v ( n o r m a l [ j ], 0 ) ; / / 追加 for (int i = 0; i < 4; ++i) { gl.glvertex3fv(vertex[face[j][i]], 0); gl.glend(); // 省略
32 a b = a x b x + a y b y + a z b z a b = a b cosθ a b cosθ = a b = axbx + ayby + azbz (a 2 x + a 2 y + a 2 z )(b 2 x + b 2 y + b 2 z ) // 図形の描画 gl.glbegin(gl_quads); for (int j = 0; j < 6; ++j) { //gl.glnormal3fv(normal[j], 0);// コメントアウト for (int i = 0; i < 4; ++i) { gl.glnormal3fv(vertex[face[j][i]], 0);// 追加 gl.glvertex3fv(vertex[face[j][i]], 0); gl.glend();
33 int[] maxlights = new int[1]; gl.glgetintegerv(gl2.gl_max_lights, maxlights, 0); System.out.println(maxLights[0]); gldisable(gl_lighting) glenable(gl_lighting) glenable(gl_light n) gldisable(gl_lighting) gldisable(gl_light n) private final float[] light0pos = {0.0f, 3.0f, 5.0f, 1.0f; private final float[] light1pos = {5.0f, 3.0f, 0.0f, 1.0f; private final float[] green = {0.0f, 1.0f, 0.0f, 1.0f; private final float[] light0pos = {0.0f, 3.0f, 5.0f, 1.0f;// 追加 private final float[] light1pos = {5.0f, 3.0f, 0.0f, 1.0f;// 追加 private final float[] green = {0.0f, 1.0f, 0.0f, 1.0f;// 追加 // 背景を白く塗りつぶす. gl.glclearcolor(1f, 1f, 1f, 1.0f); gl.glenable(gl_depth_test); gl.glenable(gl_cull_face); gl.glcullface(gl_front); gl.glenable(gl_lighting); gl.glenable(gl_light0); gl.glenable(gl_light1); gl.gllightfv(gl_light1, GL_DIFFUSE, green, 0);// 追加 gl.gllightfv(gl_light1, GL_SPECULAR, green, 0);// 追加 public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl.glmatrixmode(gl_projection); gl.glloadidentity(); glu.gluperspective(30.0, (double)width / (double)height, 1.0, 300.0); // 視点位置と視線方向 //glu.glulookat(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);// コメントアウト gl.glmatrixmode(gl_modelview); gl.glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); //GL_DEPTH_BUFFER_BITを追加 gl.glloadidentity(); glu.glulookat(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);// 追加 // 光源の位置設定 gl.gllightfv(gl_light0, GL_POSITION, light0pos, 0);// 追加 gl.gllightfv(gl_light1, GL_POSITION, light1pos, 0);// 追加 // 以降省略 v o i d g l L i g h t f v ( i n t l i g h t i n t p n a m e f l o a t [ ] p a r a m s ) pname 光源の位置を設定 x,y,z,w の 4 要素 w が 0 なら光源からの指向性のある平行光線 ( 例 : 太陽光 ) で 0 以外なら点光源 ( 例 : スポットライト 街灯 ) 点光源の場合 光源からの距離に応じてどのように減衰するかを設定できる 拡散光 ( 特定の方向からの面光源 ワールド内の物体の 光源に面している側だけが照らされる ) 光 ( 特定の方向からの光束 物体の一部だけを照らす )
34 private final float[] red = {0.8f, 0.2f, 0.2f, 1.0f; p r i v a t e f i n a l f l o a t [ ] r e d = { 0. 8 f, 0. 2 f, 0. 2 f, 1. 0 f ; / / 追加 // ここまで省略 // 図形の回転 gl.gltranslatef(0.5f, 0.5f, 0.5f); gl.glrotatef(r, 0.0f, 1.0f, 0.0f); gl.gltranslatef(-0.5f, -0.5f, -0.5f); // 図形の色 ( 赤 ) g l. g l M a t e r i a l f v ( G L _ F R O N T _ A N D _ B A C K, G L _ A M B I E N T _ A N D _ D I F F U S E, r e d, 0 ) ; / / 追加 // 図形の描画 gl.glbegin(gl_quads); // 以降省略 v o i d g l M a t e r i a l f v ( i n t f a c e i n t p n a m e f l o a t [ ] p a r a m s, i n t o f f s e t ) package demos.fingers; import static com.jogamp.opengl.gl.gl_color_buffer_bit; import java.nio.intbuffer; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.keyadapter; import com.jogamp.newt.event.keyevent; import com.jogamp.newt.event.mouseadapter; import com.jogamp.newt.event.mouseevent; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.animator; public class FingersSampleNewt implements GLEventListener { protected GLCapabilities caps; private final Animator animator; private float viewscale = 0.04f; private int prevmousex = -1; // フィンガー private static final int FINGERS_COUNT = 3*5; private static Finger[] fingers; private Palm palm; private int fingernumber = 1;
35 private final float[] red = {1f, 0f, 0f; private final float[] green = {0f, 1f, 0f; private final float[] blue = {0f, 0f, 1f; private final float[] orange = {1f, 0f, 1f; public static void main(string[] args) { System.out.println(" 指をドラッグして角度を変えられます 第一関節 ( 指先 ) を操作するにはコントロールキーを 第二関節を操作するにはシフトキーを押しながらドラッグしてください "); System.out.println(" 操作する指を変えるには 1( 小指 ) から 5( 親指 ) を押してください "); new FingersSampleNewt(); public FingersSampleNewt() { GLProfile prof = GLProfile.get(GLProfile.GL2); caps = new GLCapabilities(prof); setupfingers(); GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("finger demo (Newt)"); glwindow.setsize(500, 500); glwindow.addgleventlistener(this); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { quit(); ); glwindow.addmouselistener(new MouseAdapter() { public void mousereleased(mouseevent e) { prevmousex = -1; public void mousedragged(mouseevent evt) { int x = evt.getx() ; if(prevmousex!= -1) { float rotdelta = (prevmousex - x); int fingerindex = (fingernumber -1) * 3; if(evt.iscontroldown()) { fingers[2 + fingerindex].updaterotation(rotdelta); else if(evt.isshiftdown()) { fingers[1 + fingerindex].updaterotation(rotdelta); else { fingers[0 + fingerindex].updaterotation(rotdelta); // 現在のマウスの位置を保存 prevmousex = x; ); public void mousewheelmoved(mouseevent e) { float[] rot = e.getrotation(); viewscale *= (rot[1] > 0? 1.005f : 0.995f); glwindow.addkeylistener(new KeyAdapter() { public void keypressed(keyevent key) { switch (key.getkeychar()) { case KeyEvent.VK_ESCAPE: quit(); break; ); case 'q': quit(); break; case '1': fingernumber = 1; System.out.println("1: 小指 "); break; case '2': fingernumber = 2; System.out.println("2: 薬指 "); break; case '3': fingernumber = 3; System.out.println("3: 中指 "); break; case '4': fingernumber = 4; System.out.println("4: 人差し指 "); break; case '5': fingernumber = 5; System.out.println("5: 親指 "); break; default: break; glwindow.setvisible(true); animator = new Animator(glWindow); animator.start(); private void setupfingers() { fingers = new Finger[FINGERS_COUNT]; palm = new Palm(10, 0, -20, orange); //position は指の根元の場合に使う 二番目以降については // 親のアンカーの位置に合わせる // 小指の設定 fingers[0] = new Finger(-9.0f, 10f, 7f, 0f, red); fingers[1] = new Finger(fingers[0], 7f, 0f, blue); fingers[2] = new Finger(fingers[1], 5f, 0f, green); // 薬指の設定 fingers[3] = new Finger(-4.5f, 10f, 8f, 0f, red); fingers[4] = new Finger(fingers[3], 8f, 0f, blue); fingers[5] = new Finger(fingers[4], 7f, 0f, green); // 中指の設定 fingers[6] = new Finger(0.0f, 10f, 9f, 0f, red);
36 fingers[7] = new Finger(fingers[6], 9f, 0f, blue); fingers[8] = new Finger(fingers[7], 8f, 0f, green); // 人差し指の設定 fingers[9] = new Finger(4.5f, 10f, 7f, 0f, red); fingers[10] = new Finger(fingers[9], 7f, 0f, blue); fingers[11] = new Finger(fingers[10], 9f, 0f, green); // 親指の設定 fingers[12] = new Finger(9.0f, 7f, 6f, 0f, red); fingers[13] = new Finger(fingers[12], 6f, 0f, blue); fingers[14] = new Finger(fingers[13], 5f, 0f, green); GL2 gl2 = drawable.getgl().getgl2(); gl2.glclearcolor(1f, 1f, 1f, 1f); gl2.glcleardepth(1.0f); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { GL2 gl2 = drawable.getgl().getgl2(); gl2.glclear(gl_color_buffer_bit); gl2.glloadidentity(); gl2.glscalef(viewscale, viewscale, viewscale); //(1) // 手のひらを描く palm.render(gl2); // 指を描く for (int i = 0; i < FINGERS_COUNT; i++) { fingers[i].render(gl2); public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); private void quit() { animator.stop(); System.exit(0); package demos.fingers; import com.jogamp.opengl.gl2; public class Palm { private static final float[][] palmvertex = {{-1f, 0f, {+1f, 0f, {+1f, 1f, {-1f, 1f; private final float positionx; private final float positiony; private final float size; private final float[] color; public Palm(float size, int x, int y, float[] color) { this.size = size; this.positionx = x; this.positiony = y; this.color = color; // 手のひら? を描画する protected void render(gl2 gl2) { gl2.gltranslatef(positionx, positiony, 0f); gl2.glpushmatrix(); gl2.glscalef(size, size, 1f); gl2.glcolor3fv(color, 0); gl2.glbegin(gl2.gl_line_loop); for (int i = 0; i < palmvertex.length; i++) { gl2.glvertex3fv(palmvertex[i], 0); gl2.glend(); gl2.glpopmatrix(); package demos.fingers; import com.jogamp.opengl.gl2; public class Finger { private static final float[][] fingervertex = {{-1f, 0f, {+1f, 0f, {+1f, 1f, {-1f, 1f; private final float positionx; private final float positiony; private final float length; private float rotationangle; private final float[] color; public Finger(Finger parent, float length, float rotationangle, float[] color) { this.positionx = parent.getjointx(); this.positiony = parent.getjointy(); this.length = length; this.rotationangle = rotationangle; this.color = color; public Finger(float x, float y, float length, float rotationangle, float[] color) { this.positionx = x; this.positiony = y; this.length = length; this.rotationangle = rotationangle; this.color = color; float getjointy() { return length;
37 float getjointx() { return 0; public void updaterotation(float angle) { this.rotationangle += angle; // 指を描画する protected void render(gl2 gl2) { gl2.gltranslatef(positionx, positiony, 0f); gl2.glrotatef(rotationangle, 0f, 0f, 1f); gl2.glpushmatrix(); gl2.glscalef(1f, length, 1f); gl2.glcolor3fv(color, 0); gl2.glbegin(gl2.gl_line_loop); for (int i = 0; i < fingervertex.length; i++) { gl2.glvertex3fv(fingervertex[i], 0); gl2.glend(); gl2.glpopmatrix(); //( 省略 ) // 指を描く // f o r ( i n t i = 0 ; i < F I N G E R S _ C O U N T ; i + + ) { / / 削除 / / f i n g e r s [ i ]. r e n d e r ( g l 2 ) ; / / 削除 / / / / 削除 / / ここから追加 g l 2. g l P u s h M a t r i x ( ) ; / / ( 1 ) f i n g e r s [ 0 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 1 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 2 ]. r e n d e r ( g l 2 ) ; g l 2. g l P o p M a t r i x ( ) ; / / ( 2 ) g l 2. g l P u s h M a t r i x ( ) ; / / ( 3 ) f i n g e r s [ 3 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 4 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 5 ]. r e n d e r ( g l 2 ) ; g l 2. g l P o p M a t r i x ( ) ; / / ( 4 ) g l 2. g l P u s h M a t r i x ( ) ; / / ( 5 ) f i n g e r s [ 6 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 7 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 8 ]. r e n d e r ( g l 2 ) ; g l 2. g l P o p M a t r i x ( ) ; / / ( 6 ) g l 2. g l P u s h M a t r i x ( ) ; / / ( 7 ) f i n g e r s [ 9 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 1 0 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 1 1 ]. r e n d e r ( g l 2 ) ; g l 2. g l P o p M a t r i x ( ) ; / / ( 8 ) g l 2. g l P u s h M a t r i x ( ) ; / / ( 9 ) f i n g e r s [ 1 2 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 1 3 ]. r e n d e r ( g l 2 ) ; f i n g e r s [ 1 4 ]. r e n d e r ( g l 2 ) ; g l 2. g l P o p M a t r i x ( ) ; / / ( 1 0 ) // ここまでを追加
38 g l S c a l e f ( f l o a t x f l o a t y f l o a t z ) (1)(3)(5)(7)(9) のplPushMatrix() gltranslatef() glrotatef() glscalef()(2) (4)(6)(8)(10) のglPopMatrix() glpushmatrix() とglPopMatrix()glMatrixMode(GL_MODELVIEW)glMatrixMode(GL_PROJECTION) glmatrixmode() i n t [ ] m a x S t a c k S i z e = n e w i n t [ 1 ] ; g l. g l G e t I n t e g e r v ( G L 2. G L _ M O D E L V I E W _ S T A C K _ D E P T H, m a x S t a c k S i z e, 0 ) ;//GL_PROJECTION_STACK_DEPTH あるいは GL_TEXTURE_STACK_DEPTH System.out.println(maxStackSize[0]); package demos.texture; import java.io.ioexception; import java.io.inputstream; import com.jogamp.opengl.debuggl2; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glexception; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.keyadapter; import com.jogamp.newt.event.keyevent; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.animator; import com.jogamp.opengl.util.texture.texture; import com.jogamp.opengl.util.texture.textureio; public class TextureSample implements GLEventListener { private static final String IMAGE_FILE = "nehe.png"; //(1) private final Animator animator; private Texture texture; //(2) public static void main(string[] args) { new TextureSample(); public TextureSample() { GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("texture sample (Newt)"); glwindow.setsize(500, 500); glwindow.addgleventlistener(this); glwindow.addwindowlistener(new WindowAdapter() {
39 ); public void windowdestroyed(windowevent evt) { quit(); glwindow.addkeylistener(new KeyAdapter() { public void keypressed(keyevent key) { switch (key.getkeychar()) { case KeyEvent.VK_ESCAPE: quit(); break; ); case 'q': quit(); break; default: break; glwindow.setvisible(true); animator = new Animator(glWindow); animator.start(); final GL2 gl2 = drawable.getgl().getgl2(); drawable.setgl(new DebugGL2(gl2)); gl2.glclearcolor(1f, 1f, 1f, 1f); try { InputStream resourcestream = this.getclass().getresourceasstream(image_file); //(3) texture = TextureIO.newTexture(resourceStream, false, TextureIO.PNG); //(4) catch (GLException IOException e) { e.printstacktrace(); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { final GL2 gl2 = drawable.getgl().getgl2(); gl2.glclear(gl2.gl_color_buffer_bit); gl2.glloadidentity(); gl2.glscalef(0.9f, 0.9f, 0.9f); texture.enable(gl2); //(5) gl2.glbegin(gl2.gl_quads); gl2.gltexcoord2f(0.0f, 0.0f); //(6) gl2.glvertex2f(-1.0f, -1.0f); gl2.gltexcoord2f(1.0f, 0.0f); gl2.glvertex2f(1.0f, -1.0f); gl2.gltexcoord2f(1.0f, 1.0f); gl2.glvertex2f(1.0f, 1.0f); gl2.gltexcoord2f(0.0f, 1.0f); gl2.glvertex2f(-1.0f, 1.0f); gl2.glend(); public void dispose(glautodrawable drawable) { if(texture!= null) { final GL2 gl2 = drawable.getgl().getgl2(); texture.destroy(gl2); //(7) if(animator!= null) animator.stop(); private void quit() { animator.stop(); System.exit(0); java.io.ioexception: Stream was null at com.jogamp.opengl.util.texture.textureio.newtexturedataimpl(textureio.java:834) at com.jogamp.opengl.util.texture.textureio.newtexturedata(textureio.java:246) at com.jogamp.opengl.util.texture.textureio.newtexture(textureio.java:506) at demos.texture.texturesample.init(texturesample.java:77) ( 以下省略 )
40 com.jogamp.opengl.util.texture.texture c o m. j o g a m p. o p e n g l. u t i l. t e x t u r e. T e x t u r e I O. n e w T e x t u r e ( j a v a. i o. I n p u t S t r e a m s t r e a m, b o o l a n m i p m a p, S t r i n g i m a g e T y p e ) TextureIO.JPG,TextureIO.PNG,TextureIO.GIF,TextureIO.TIFFを指定します String somefolder = " どこかのフォルダー "; File file = new File(someFolder, IMAGE_FILE); InputStream resourcestream = new FileInputStream(file); package demos.glut; import static com.jogamp.opengl.gl.*; import static com.jogamp.opengl.fixedfunc.gllightingfunc.*; import com.jogamp.opengl.gl2; import com.jogamp.opengl.glautodrawable; import com.jogamp.opengl.glcapabilities; import com.jogamp.opengl.gleventlistener; import com.jogamp.opengl.glprofile; import com.jogamp.newt.event.keyadapter; import com.jogamp.newt.event.keyevent; import com.jogamp.newt.event.mouseadapter; import com.jogamp.newt.event.mouseevent; import com.jogamp.newt.event.windowadapter; import com.jogamp.newt.event.windowevent; import com.jogamp.newt.opengl.glwindow; import com.jogamp.opengl.util.animator; import com.jogamp.opengl.util.gl2.glut; public class GlutSampleNewt implements GLEventListener { private final Animator animator; private final GLUT glut; private float viewscale = 0.02f; private final float[] white0 = {0.5f, 0.5f, 0.5f;
41 private final float[] white1 = {1f, 1f, 1f; private final float[] red = {1f, 0f, 0f; private final float[] green = {0f, 1f, 0f; private final float[] blue = {0f, 0f, 1f; private final float[] yellow = {1f, 1f, 0f; private final float[] magenta = {1f, 0f, 1f; private final float[] cyan = {0f, 1f, 1f;; private final float[] light0pos = {-10f, 30.0f, 5.0f, 1.0f; private final float[] light1pos = {-10f, 30.0f, 5.0f, 1.0f; public static void main(string[] args) { new GlutSampleNewt(); public GlutSampleNewt() { GLProfile prof = GLProfile.get(GLProfile.GL2); glut = new GLUT(); GLCapabilities caps = new GLCapabilities(prof); GLWindow glwindow = GLWindow.create(caps); glwindow.settitle("glut demo (Newt)"); glwindow.setsize(500, 500); glwindow.addgleventlistener(this); glwindow.addwindowlistener(new WindowAdapter() { public void windowdestroyed(windowevent evt) { quit(); ); glwindow.addmouselistener(new MouseAdapter() { public void mousewheelmoved(mouseevent e) { float[] rot = e.getrotation(); viewscale *= (rot[1] > 0? 1.005f : 0.995f); System.out.println(viewScale); ); glwindow.addkeylistener(new KeyAdapter() { public void keypressed(keyevent key) { switch (key.getkeychar()) { case KeyEvent.VK_ESCAPE: quit(); break; ); case 'q': quit(); break; default: break; glwindow.setvisible(true); animator = new Animator(glWindow); animator.start(); GL2 gl2 = drawable.getgl().getgl2(); gl2.glclearcolor(1, 1, 1, 1); gl2.glcleardepth(1.0f); gl2.glenable(gl_depth_test); gl2.glenable(gl_cull_face); gl2.glcullface(gl_front); gl2.glenable(gl_lighting); gl2.glenable(gl_light0); gl2.gllightfv(gl_light0, GL_DIFFUSE, white0, 0); gl2.gllightfv(gl_light0, GL_POSITION, light0pos, 0); gl2.glenable(gl_light1); gl2.gllightfv(gl_light1, GL_SPECULAR, white0, 0); gl2.gllightfv(gl_light1, GL_POSITION, light1pos, 0); gl2.glenable(gl_light2); gl2.gllightfv(gl_light2, GL_AMBIENT, white0, 0); public void reshape(glautodrawable drawable, int x, int y, int width, int height) { GL2 gl2 = drawable.getgl().getgl2(); gl2.glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); gl2.glloadidentity(); gl2.glscalef(viewscale, viewscale, viewscale); gl2.glpushmatrix(); gl2.gltranslatef(-30f, -20, 0); gl2.glrotatef(30f, 0f, 1f, 0f); gl2.glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, green, 0); glut.glutsolidsphere(10, 30, 30); //(1) 球 gl2.glpopmatrix(); gl2.glpushmatrix(); gl2.gltranslatef(0f, -20, 0); gl2.glrotatef(30f, 1f, 1f, 0f); gl2.glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, blue, 0); glut.glutsolidteapot(10); //(2) ティーポット gl2.glpopmatrix(); gl2.glpushmatrix(); gl2.gltranslatef(30f, -20, 0); gl2.glrotatef(30f, 0f, 1f, 0f); gl2.glrotatef(30f, 0f, 0f, 1f); gl2.glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, yellow, 0); glut.glutsolidcube(10); //(3) 立方体 gl2.glpopmatrix(); gl2.glpushmatrix(); gl2.gltranslatef(-40f, 20, 0); gl2.glrotatef(30f, 0f, 1f, 0f); gl2.glrotatef(30f, 1f, 0f, 0f); gl2.glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, cyan, 0); glut.glutsolidcylinder(10, 20, 20, 10); //(4) 円筒 gl2.glpopmatrix(); gl2.glpushmatrix();
42 gl2.gltranslatef(0f, 20, 0); gl2.glrotatef(30f, 0f, 1f, 0f); gl2.glrotatef(30f, 0f, 0f, 1f); gl2.glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, magenta, 0); glut.glutsolidtorus(3, 10, 15, 25); //(5) トーラス gl2.glpopmatrix(); gl2.glpushmatrix(); gl2.gltranslatef(30f, 20, 0); gl2.glrotatef(30f, 0f, 1f, 0f); gl2.glrotatef(30f, 0f, 0f, 1f); gl2.glscalef(10f, 10f, 10f); gl2.glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, red, 0); glut.glutsolidoctahedron(); //(6) 正八面体 gl2.glpopmatrix(); public void dispose(glautodrawable drawable) { if(animator!= null) animator.stop(); private void quit() { animator.stop(); System.exit(0); // 省略 showglinfo(drawable); private static void showglinfo(glautodrawable drawable) { System.err.println(" 利用可能なプロファイルのリスト "); for(string prof : GLProfile.GL_PROFILE_LIST_ALL) { System.err.println(prof); System.err.println(); System.err.println(" 選択された GLCapabilities: " + drawable.getchosenglcapabilities()); GL gl = drawable.getgl(); System.err.println("INIT GL: " + gl.getclass().getname()); System.err.println("GL_VENDOR: " + gl.glgetstring(gl.gl_vendor)); System.err.println("GL_RENDERER: " + gl.glgetstring(gl.gl_renderer)); System.err.println("GL_VERSION: " + gl.glgetstring(gl.gl_version)); 利用可能なプロファイルのリスト GL4bc GL3bc GL2 GL4 GL3 GLES3 GL4ES3 GL2GL3 GLES2 GL2ES2 GLES1 GL2ES1 選択された GLCapabilities: GLCaps[rgba 8/8/8/0, opaque, accum-rgba 0/0/0/0, dp/st/ms 16/0/0, dbl, mono, hw, GLProfile[GL4/GL4.hw], offscr[fbo]] INIT GL: jogamp.opengl.gl4.gl4bcimpl GL_VENDOR: Intel Inc. GL_RENDERER: Intel HD Graphics 4000 OpenGL Engine GL_VERSION: 4.1 INTEL
43 GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL3)); GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL4)); GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2)); Exception in thread "main" java.lang.runtimeexception: com.jogamp.opengl.glexception: Caught GLException: Not a GL2 implementation on thread main-display-.macosx_nil-1-edt-1 GL2 gl2 = drawable.getgl().getgl2(); GL3 gl3 = drawable.getgl().getgl3(); gl3.glloadidentity(); gl3.glscalef(viewscale, viewscale, viewscale); gl3.gltranslatef(positionx, positiony, 0f); gl3.glrotatef(rotationangle, 0f, 0f, 1f); gl3.glpushmatrix(); gl3.glcolor3fv(color, 0); gl3.glbegin(gl3.gl_line_loop); gl3.glvertex3fv(fingervertex[i], 0); gl3.glend(); gl3.glpopmatrix(); GLCapabilities caps = new GLCapabilities(GLProfile.getMaxFixedFunc(true)); GL2 gl2 = drawable.getgl().getgl2(); drawable.setgl(new DebugGL2(gl2)); // 以降省略 private final GLPrintStream out; // コンストラクタ public SomeClass { try { out = new GLPrintStream("gltrace.txt"); catch (IOException e) { e.printstacktrace(); GL2 gl2 = drawable.getgl().getgl2(); drawable.setgl(newtracegl2(gl2, out)); // 以降省略
44
3D 描画 Step2 マウスで回転できるようにする ベクトル (a, b, c) を軸として右回りに rot 度の回転は glrotatef(rot, a, b, c); で実行される 従って 座標軸の回転はそれぞれ x 軸まわり,y 軸まわりの回転量 (degree で表す ) を rotx, roty をとすると x 軸まわりの回転は glrotatef(rotx, 1.0f, 0.0f, 0.0f);
Chapter JDK KeyListener keypressed(keyevent e ) keyreleased(keyevent e ) keytyped(keyevent e ) MouseListener mouseclicked(mouseeven
Chapter 11. 11.1. JDK1.1 11.2. KeyListener keypressed(keyevent e ) keyreleased(keyevent e ) keytyped(keyevent e ) MouseListener mouseclicked(mouseevent e ) mousepressed(mouseevent e ) mousereleased(mouseevent
KeyListener init addkeylistener addactionlistener addkeylistener addkeylistener( this ); this.addkeylistener( this ); KeyListener public void keytyped
KeyListener keypressed(keyevent e) keyreleased(keyevent e) keytyped(keyevent e) MouseListener mouseclicked(mouseevent e) mousepressed(mouseevent e) mousereleased(mouseevent e) mouseentered(mouseevent e)
このような 回転や平行移動による座標変換の情報は ModelView 行列 が持っている ModelView 行列は gl.glpushmatrix() でいったん保存しておき 回転や平行移動を重ねて描画した後 gl.glpopmatrix() で保存した状態に戻すことができる ワールド座標系とウィ
3D 描画 Step3 元素名を 2D 描画する OpenGL には文字描画の概念がないこと 元素名は回転してほしくないことの 2 点の理由から 元素名は 2D 描画する そのために 原子の 3 次元空間の座標から 文字を描画するウィンドウ上の座標を求める < 座標変換について > 座標変換について まとまった詳しい解説は OpenGL による 3D 描画の基礎知識 の 3 ページ以降に記述してあるので
Assignment_.java /////////////////////////////////////////////////////////////////////// // 課題 星の画像がマウスカーソルを追従するコードを作成しなさい 次 ///////////////////
Assignment_.java 0 0 0 0 0 /////////////////////////////////////////////////////////// // 課題 次のようにマウスのカーソルに同期しメッセージを /////////////////////////////////////////////////////////// class Assignment_ extends
r3.dvi
10 3 2010.9.21 1 1) 1 ( 1) 1: 1) 1.0.1 : Java 1 import java.awt.*; import javax.swing.*; public class Sample21 extends JPanel { public void paintcomponent(graphics g) { g.setcolor(new Color(255, 180, 99));
問題1 以下に示すプログラムは、次の処理をするプログラムである
問題 1 次に示すプログラムは 配列 a の値を乱数で設定し 配列 a の値が 333 より大きく 667 以下の値 の合計値を求めるプログラムである 1 と 2 に適切なコードを記述してプログラムを完 成させよ class TotalNumber { public static void main(string[] args) { int[] a = new int[1000]; // 1 解答条件
Microsoft PowerPoint prog1_doc2x.pptx
アプレット public class extends Applet { public void paint(graphics g) { // アプレット描画 g.drawstring( Hello World, 10, 20 ); page 1 アプレット : 色 public class extends Applet { Color col; // カラークラス int red, grn, blu;
I 4 p.2 4 GUI java.awt.event.* import /* 1 */ import mouseclicked MouseListener implement /* 2 */ init addmouselistener(this) this /* 3 */ this mousec
I 4 p.1 4 GUI GUI GUI 4.1 4.1.1 MouseTest.java /* 1 */ public class MouseTest extends JApplet implements MouseListener /* 2 */ { int x=50, y=20; addmouselistener(this); /* 3 */ public void mouseclicked(mouseevent
次の演習課題(1),(2)のプログラムを完成させよ
次の演習課題 (1),(2) のプログラムを作成せよ. 課題 (1) ボタン押下時の処理を追加し以下の実行結果となるようにプログラムを作成しなさい ( ボタン押下時の処理 ) import java.lang.*; class Figure extends JFrame implements ActionListener{ JPanel panel; JScrollPane scroll; JTextArea
やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF
pref : 2004/6/5 (11:8) pref : 2004/6/5 (11:8) pref : 2004/6/5 (11:8) 3 5 14 18 21 23 23 24 28 29 29 31 32 34 35 35 36 38 40 44 44 45 46 49 49 50 pref : 2004/6/5 (11:8) 50 51 52 54 55 56 57 58 59 60 61
K227 Java 2
1 K227 Java 2 3 4 5 6 Java 7 class Sample1 { public static void main (String args[]) { System.out.println( Java! ); } } 8 > javac Sample1.java 9 10 > java Sample1 Java 11 12 13 http://java.sun.com/j2se/1.5.0/ja/download.html
Microsoft PowerPoint prog1_doc2.pptx
2011 年 12 月 6 日 ( 火 ) プログラミング Ⅰ Java Applet プログラミング 文教大学情報学部経営情報学科堀田敬介 アプレット Applet public class クラス名 extends Applet { public void paint(graphics g) { // アプレット描画 g.drawstring( Hello World, 10, 20); 10
II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K
II Java 09 2 13 10:30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K KeyListener J JApplet L addmouselistener M MouseListener
Java講座
~ 第 1 回 ~ 情報科学部コンピュータ科学科 2 年竹中優 プログラムを書く上で Hello world 基礎事項 演算子 構文 2 コメントアウト (//, /* */, /** */) をしよう! インデントをしよう! 変数などにはわかりやすい名前をつけよう! 要するに 他人が見て理解しやすいコードを書こうということです 3 1. Eclipse を起動 2. ファイル 新規 javaプロジェクト
:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme
2014 8 01 10:30 12:00 I. I VII II. III. IV. ( a d) V. VI. 80 100 60 : this==null, T == N A ActionListener A addactionlistener C class D actionperformed E ActionEvent G getsource I implements J JApplet
I. (i) Foo public (A). javac Foo.java java Foo.class (C). javac Foo java Foo (ii)? (B). javac Foo.java java Foo (D). javac Foo java Foo.class (A). Jav
2018 06 08 11:00 12:00 I. I III II. III. IV. ( a d) V. VI. 80 40 40 100 60 : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeylistener KE KeyEvent M MouseListener am addmouselistener
Java演習(4) -- 変数と型 --
50 20 20 5 (20, 20) O 50 100 150 200 250 300 350 x (reserved 50 100 y 50 20 20 5 (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics; (reserved public class Blocks1 extends
文字列操作と正規表現
文字列操作と正規表現 オブジェクト指向プログラミング特論 2018 年度只木進一 : 工学系研究科 2 文字列と文字列クラス 0 個以上の長さの文字の列 Java では String クラス 操作 文字列を作る 連結する 文字列中に文字列を探す 文字列中の文字列を置き換える 部分文字列を得る 3 String クラス 文字列を保持するクラス 文字列は定数であることに注意 比較に注意 == : オブジェクトとしての同等性
#include <stdio.h> 2 #include <stdlib.h> 3 #include <GL/glut.h> 4 Program 1 (OpenGL GameSample001) 5 // 6 static bool KeyUpON = false; // 7 sta
1 1. 1 #include 2 #include 3 #include 4 Program 1 (OpenGL GameSample001) 5 // 6 static bool KeyUpON = false; // 7 static bool KeyDownON = false; // 8 static bool KeyLeftON
シミュレーションの簡単な例 GUI 無しのシミュレーションを作る GUI を作る パラメタを設定するデモンストレーションをする 2 オブジェクト指向プログラミング特論
例 : 簡単な酔歩シミュレーション 1 オブジェクト指向プログラミング特論 シミュレーションの簡単な例 GUI 無しのシミュレーションを作る GUI を作る パラメタを設定するデモンストレーションをする 2 オブジェクト指向プログラミング特論 簡単な二次元酔歩 Walker は二次元面内で 4 方向に等確率で移動 メソッド move で移動し 新しい位置を返す Simulation クラス 多数の
新・明解Java入門
537,... 224,... 224,... 32, 35,... 188, 216, 312 -... 38 -... 38 --... 102 --... 103 -=... 111 -classpath... 379 '... 106, 474!... 57, 97!=... 56 "... 14, 476 %... 38 %=... 111 &... 240, 247 &&... 66,
I. (i) Java? (A). Foo_Bar (B). G day (C). 999 (D). Golgo13 (ii)? (A). Java public (B). Java (C). Java JavaScript (D). Java C Java C (iii)? (A). Java (
2016 07 29 10:30 12:00 I. I V II. III. IV. ( a d) V. VI. 80 100 60 : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeylistener KE KeyEvent M MouseListener am addmouselistener
10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV
[email protected] [email protected] http://www.misojiro.t.u-tokyo.ac.jp/ tutimura/sem3/ 2002 12 11 p.1/33 10/16 1. 10/23 2. 10/30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20
Java学習教材
Java 2016/4/17 Java 1 Java1 : 280 : (2010/1/29) ISBN-10: 4798120987 ISBN-13: 978-4798120980 2010/1/29 1 Java 1 Java Java Java class FirstExample { public static void main(string[] args) { System.out.println("
I HTML HashMap (i) (ii) :.java import java.net.*; import java.io.*; import java.util.hashmap; public class SimpleStopWatch { public static voi
II Java 10 2 12 10:30 12:00 I. I III II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K KeyListener J JApplet L addmouselistener M MouseListener
コンピュータグラフィックスS 演習資料
コンピュータグラフィックス S 演習資料 第 4 回シェーディング マッピング 九州工業大学情報工学部システム創成情報工学科講義担当 : 尾下真樹 1. 演習準備 今回の演習も 前回までの演習で作成したプログラムに続けて変更を行う まずは シェーディングの演習のため 描画処理で 回転する一つの四角すいを描画するように変更する 画面をクリア ( ピクセルデータと Z バッファの両方をクリア ) glclear(
Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の break; まで処理しますどれにも一致致しない場合 def
Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の まで処理しますどれにも一致致しない場合 default: から直後の まで処理します 式の結果 ラベル 定数 整数または文字 (byte, short, int,
2 1 Web Java Android Java 1.2 6) Java Java 7) 6) Java Java (Swing, JavaFX) (JDBC) 7) OS 1.3 Java Java
1 Java Java 1.1 Java 1) 2) 3) Java OS Java 1.3 4) Java Web Start Web / 5) Java C C++ Java JSP(Java Server Pages) 1) OS 2) 3) 4) Java Write Once, Run Anywhere 5) Java Web Java 2 1 Web Java Android Java
2008 e-learning T050050
e-learning T050050 e-learning B NintendoDS e-learning html 1 e-learning Java Applet html 2 2008 e-learning T050050 1 1 1.1.................................. 1 1.2............................ 1 2 2 2.1..............................
Fair Curve and Surface Design System Using Tangent Control
情報工学 2016 年度後期第 6 回 [11 月 16 日 ] 静岡大学工学研究科機械工学専攻ロボット 計測情報講座創造科学技術大学院情報科学専攻 三浦憲二郎 講義アウトライン [11 月 16 日 ] ビジュアル情報処理 3 モデリング 3.3 曲線 曲面 OpenGL 色の取り扱い シェーディング 照明モデルと照光処理 拡散光 鏡面光 環境光 ビジュアル情報処理 3-3 曲線 曲面 3-3-1
2 static final int DO NOTHING ON CLOSE static final int HIDE ON CLOSE static final int DISPOSE ON CLOSE static final int EXIT ON CLOSE void setvisible
12 2013 7 2 12.1 GUI........................... 12 1 12.2............................... 12 4 12.3..................................... 12 7 12.4....................................... 12 9 12.5 : FreeCellPanel.java............................
Graphical User Interface 描画する
Graphical User Interface 描画する オブジェクト指向プログラミング特論 2016 年度 只木進一 : 工学系研究科 2 描画の基本 javax.swing.jpanel に描画する paint() または paintcomponent() メソッドを上書きすることによって描画する この中で描画対象を描く 基本的図形要素は準備されている しかし 画面の重なりによる再描画の場合
1 1. Program 1 OpenCV (OpenCV Sample001) 1 /* 2 - > - > - >VC++ 3 ( ) 4 C:\opencv\build\include 5 ( ) 6 C:\opencv\build\x86\vc10\lib 7 - > - > - > - >
1 1. Program 1 OpenCV (OpenCV Sample001) 1 /* 2 - > - > - >VC++ 3 ( ) 4 C:\opencv\build\include 5 ( ) 6 C:\opencv\build\x86\vc10\lib 7 - > - > - > - > 8 (240 O p e n C V ) 9 opencv_core240d.lib 10 opencv_imgproc240d.lib
< F2D F B834E2E6A7464>
ランダムウォーク [Java アプレット ] [Java アプレリケーョン ] 1. はじめに 酔っぱらいは前後左右見境なくふらつきます 酔っぱらいは目的地にたどり着こうと歩き回っているうちに何度も同じところに戻って来てしまったりするものです 今 酔っぱらいが数直線上の原点にいるとします 原点を出発して30 回ふらつくとき 30 回目に酔っぱらいがいる位置は 出発点である原点からどれくらい離れてしまっているのでしょうか
人工知能入門
藤田悟 黄潤和 探索とは 探索問題 探索解の性質 探索空間の構造 探索木 探索グラフ 探索順序 深さ優先探索 幅優先探索 探索プログラムの作成 バックトラック 深さ優先探索 幅優先探索 n 個の ueen を n n のマスの中に 縦横斜めに重ならないように配置する 簡単化のために 4-ueen を考える 正解 全状態の探索プログラム 全ての最終状態を生成した後に 最終状態が解であるかどうかを判定する
2
問題 次の設問に答えよ 設問. Java のソースコードをコンパイルするコマンドはどれか a) java b) javac c) javadoc d) javaw 設問. Java のバイトコード ( コンパイル結果 ) を実行するコマンドはどれか a) java b) javac c) javadoc d).jar 設問. Java のソースコードの拡張子はどれか a).c b).java c).class
JavaプログラミングⅠ
Java プログラミング Ⅰ 12 回目クラス 今日の講義で学ぶ内容 クラスとは クラスの宣言と利用 クラスの応用 クラス クラスとは 異なる複数の型の変数を内部にもつ型です 直観的に表現すると int 型や double 型は 1 1 つの値を管理できます int 型の変数 配列型は 2 5 8 6 3 7 同じ型の複数の変数を管理できます 配列型の変数 ( 配列変数 ) クラスは double
< F2D82518CC282CC D2E6A7464>
2 個のさいころ 1. はじめに [Java アプレット ] [Java アプリケーション ] 2 個のさいころを同時に投げたときの目の出方を考えてみましょう この 2 個のさいころをそれぞれ さいころ Ⅰ さいころ Ⅱ とすると その目の出方は順に 1 1 2 1 3 1 4 1 5 1 6 1 1 2 2 2 3 2 4 2 5 2 6 2 1 3 2 3 3 3 4 3 5 3 6 3 1 4
第3章 OpenGL の基礎
3 OpenGL April 11, 2017 1 / 28 3.1 ( ) OpenGL OpenGL 2 / 28 3.2 OpenGL OpenGL OpenGL (Open Graphics Library) Silicon Graphics, Inc. 2 3 API (Application Program Interface) [4] UNIX OS Windows Macintosh
2
問題 1 次の設問 1~5 に答えよ 設問 1. Java のソースプログラムをコンパイルするコマンドはどれか a) java b) javac c) javadoc d) jdb 設問 2. Java のバイトコード ( コンパイル結果 ) を実行するコマンドはどれか a) java b) javac c) javadoc d) jdb 設問 3. Java のソースプログラムの拡張子はどれか a).c
< F2D82518E9F8AD CC95BD8D7388DA93AE2E6A7464>
2 次関数のグラフの平行移動 [Java アプレット ] [Java アプリケーション ] 1. はじめに 2 2 y=ax のグラフとy=a(x-b) +c のグラフは 位置は違うけれど 形も広がりも全く同じです 2 2 y=a(x-b) +c のグラフは y=ax のグラフをx 軸方向に ( 右方向に ) +b y 軸方向に ( 上方向に ) +c だけ平行移動したものです 2 シミュレーションソフト
JAVA入門
JAVA 入門後期 10 情報処理試験例題解説 H14 年度秋問 8 次の Java プログラムの説明及びプログラムを読んで, 設問に答えよ プログラムの説明 ディジタル論理回路シミュレータを作成するためのクラスとテスト用クラスである (1) ゲートを表す抽象クラス Gate のサブクラスとして, NOT ゲートを表すクラス NotGate 及び AND ゲートを表すクラス AndGate を定義する
明解Javaによるアルゴリズムとデータ構造
21 algorithm List 1-1 a, b, c max Scanner Column 1-1 List 1-1 // import java.util.scanner; class Max3 { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); Chap01/Max3.java
Java 2 - Lesson01
第 2 回 GUI コンポーネントのイベント処理 GUI Component Event Handling キーポイント イベント イベントリスナー イベント処理とは何か? ActionEventとActionListenerについて ItemEventとItemListenerについて TextEventとTextListenerについて KeyEventとKeyListenerについて AdjustmentEventとadjustmentListenerについて
Javaセキュアコーディングセミナー2013東京第1回 演習の解説
Java セキュアコーディングセミナー東京 第 1 回オブジェクトの生成とセキュリティ 演習の解説 2012 年 9 月 9 日 ( 日 ) JPCERT コーディネーションセンター脆弱性解析チーム戸田洋三 1 演習 [1] 2 演習 [1] class Dog { public static void bark() { System.out.print("woof"); class Bulldog
< F2D825282CC947B909482CC A815B83682E6A>
3 の倍数のトランプカード 1. はじめに [Java アプレット ] [Java アプリケーション ] ここにトランプが 1 組あります ジョーカー 2 枚を除いて 52 枚を使います 3 の倍数は スペード クローバ ダイヤ ハートに それぞれ 3 と 6 と 9 と 12 の 4 枚ずつあるので 4 4=16 枚あります この 52 枚のトランプから 1 枚引いたとき そのカードが 3 の倍数である確率を考えます
解きながら学ぶJava入門編
44 // class Negative { System.out.print(""); int n = stdin.nextint(); if (n < 0) System.out.println(""); -10 Ÿ 35 Ÿ 0 n if statement if ( ) if i f ( ) if n < 0 < true false true false boolean literalboolean
IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap
1-1 1-2 IE6 2 BMI 3-1 3-2 4 5 chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chapter8 : 13-1 13-2 14 15 PersonTest.java KazuateGame.java
/*p7-1-1*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 /* e8.c Copyright (c) 2003 by T. HAYASHI and
目 次 Java GUI 3 1 概要 クラス構成 ソースコード例 課題...7 i
Java GUI 3 Java GUI 3 - サンプルプログラム (1) - 2011-09-25 Version 1.00 K. Yanai 目 次 Java GUI 3 1 概要...1 2 クラス構成...2 3 ソースコード例...3 4 課題...7 i 1 概要まずは簡単なサンプルプログラムをみながら Java GUI の基本的なことを学びましょう 本サンプルは 図に示すようなひとつのメイン画面を使用します
8 if switch for while do while 2
(Basic Theory of Information Processing) ( ) if for while break continue 1 8 if switch for while do while 2 8.1 if (p.52) 8.1.1 if 1 if ( ) 2; 3 1 true 2 3 false 2 3 3 8.1.2 if-else (p.54) if ( ) 1; else
19 3!! (+) (>) (++) (+=) for while 3.1!! (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics;
19 3!!...... (+) (>) (++) (+=) for while 3.1!! 3.1.1 50 20 20 5 (20, 20) 3.1.1 (1)(Blocks1.java) public class Blocks1 extends JApplet { public void paint(graphics g){ 5 g.drawrect( 20, 20, 50, 20); g.drawrect(
アルゴリズムとデータ構造1
1 2005 7 22 22 (sakai.keiichi@kochi [email protected]) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2005/index.html tech.ac.jp/k1sakai/lecture/alg/2005/index.html f(0) = 1, f(x) =
