Making the transition from C++ to Java

Hello everyone,
I’m a student in a computer graphics course at SUNY Newpaltz, and our instructor is using C++. He said he will give extra credit if I can learn to do it with Java, but he will provide no support, as his focus is C. He has given me permission to use any help I can find to do it in Java, and so I come to you people. I attach a file of my current exercise I wish to accomplish with OpenGL for your help and viewing pleasure…Its to draw a simple house.

Most appreciatively,

bernie

Are you sure you don’t want to do this in Java2D, as OpenGL is a whole new world to discover.

And you’ll find way more tutorials about Java2D, which is also simpler to work with in this case.

Are you asking us to do your homework for you?

Hi, no, I havent done any Java before, thus I am more like asking, what Development environments are you using, how to install
a jogl library to said environment, and are the functions the same…you could say, in a sense I need help more with Java than with open GL.
I think that is reasonable. Meanwhile I’m doing the assignment in C++ myself. So, I guess if someone could just help me get an openGL for Java environment up and running, that would be good.

bernie

PS, thank you all for replying…most appreciated

Are you doing the drawing in OpenGL with C++? If so then using a Java binding won’t be much different.

Look at the JOGL User’s Guide in the section " Local installation for development" for instructions on how to download and properly reference the JOGL jar file and native libraries. You don’t need a lot of infrastructure in order to develop Java applications using OpenGL. Just download the JDK and make sure its bin directory is in your PATH. All you will likely ever need to execute are java and javac. You can edit .java files in any text editor.

Download the jogl-demos-src.zip from the JOGL home page or one of the release builds and look at the Gears demo to see the general structure of a Java application using JOGL. You basically need to create a GLCanvas, install it in a frame, add a GLEventListener, and make the frame visible. Your GLEventListener looks very similar to the GLUT callbacks. Just fetch the GL object out of the GLAutoDrawable passed to any of your methods and use it as you would in C++.

Text parsing, string manipulation, etc. are much easier in Java than in C++. Look at String, BufferedInputStream, FileInputStream, StringTokenizer, StreamTokenizer, Float.parseFloat(), Integer.parseInt(), etc.

Most appreciated, the impression I got from my instructor was that it would be “worth it” to use Java for these things.

Does anyone suggest a book for OpenGL with Java?

bernie

For something this simple all you need to do is take the Gears demo, rip out all of the code that draws the gears, and replace it with the code to draw the house. It really isn’t that much different from the C code. I know I would spend more time trying to get the regex part of the text loading to work than I would spend on the GL code.

The APIs are almost exactly the same as in C. The overview documentation in the browsable javadoc shows how conversion between the C APIs and Java APIs is done. I would really recommend you look at the Gears demo and maybe some of the other demos as well as Pepijn Van Eeckhoudt’s ports of the NeHe demos at http://pepijn.fab4.be/nehe/ to get a feel for what OpenGL apps in Java look like.

I would suggest you grab eithe Netbeans or Eclispe as a dev environment. Both are good and are free.

Do you have any C++ experience or just C? If you havetn done C++ then objects are likely to be new to you. The gpood news is that you dont really have to do too much with them for a straight port of a small C program. I’d get anyone of the infintie number of intro Java books. They will show you how to organize a basic “hello world” in Java and from there you shou7ld eb in good shape. As already mentioned some of the soimple JOGL demos are a great way to get started too, as you can grab them and just modify their draw code.

FInally, as you run into problems you cant figure out Id make liberal use of the “Newless CLubies” forum for basic Java questions, or these forums for Java specifc questions.

Good luck!

The book they gave us in school which was very good was
Java: How to Program by Harvey M. Deitel and Paul J. Deitel isbn: 0131483986

I have one semester of programming in C++ (accelerated, with data structures)… That’s it. So…newbee-ish…Any help is appreciated.

thanks to all

bernie

Can Somone give me a consise list of all import statements that use GL…
thanks
bernie

I’m not sure what you’re asking. Are you asking what imports are necessary in order to use the GL and related classes?


import javax.media.opengl.*;
import javax.media.opengl.glu.*;

Have you looked at the Gears demo? GKW’s advice to start with that and rip everything out of it is good.

Below, my current C++ code for this assignment (see pdf attachement earlier) …please comment if you are willing to look at c code
thanks
bernie

ps, yes, I’ve pulled down the Gears demo and am getting into it. Thanks

#include
#include
#include <glut.h>
using namespace std;

class Point{
double x;
double y;
Point (double x=0, double y=0){this.x=x;this.y=y;}
//constructor connects x and y values of the calling object
//‘this’

};

class House{
private:
double w; //width
double h; // height
double p; //the peak
Point O; //House origin. bottom left corner of the shell of
//the house

public:
House(double w, double h, double p, Point origin); // constructor
//for a house

/* void draw_shell();
void draw_door();
void draw_chimney();
void draw_window();
*/

void draw_shell(){
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0); .
glBegin (GL_LINES);
	glVertex2i(O.x, O.y); //vertices of shell in terms of origin(O.x, O.y)
	glVertex2i(O.x, O.y+h);
	glVertex2i(O.x+w, O.y);
	glVertex2i(O.x+(1/2)w, p);
	glVertex2i(O.x+w, O.y+h);

glEnd ( );
glFlush ( );
}

void draw_door(){ 
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0); .
glBegin (GL_LINES);
	glVertex2i(O.x+(1/8)w, O.y);
	glVeretex2i(O.x+(1/8)w, O.y+h);
	glVertex2i(O.x+(1/4)w, O.y+h);
	glVertex2i(O.x+(1/4)w, O.y);

glEnd ( );
glFlush ( );
}
void draw_window(){
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0); .
glBegin (GL_LINES);
	glVertex2i(O.x+(5/8)w, O.y+(1/2)h);
	glVertex2i(O.x+(5/8)w, O.y+(3/4)h);
	glVertex2i(O.x+(7/8)w, O.y+(3/4)h);
	glVertex2i(O.x+(7/8)w, O.y+(1/2)h);

glEnd ( );
glFlush ( );
}

void draw_chimney(){
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0); .
glBegin (GL_LINES);
	glVertex2i(O.x+(1/8)w, O.y+((1/4)*(p-h)+h);
	glVertex2i(O.x+(1/8)w, O.y+p);
	glVertex2i(O.x+(3/16)w, O.y+p);
	glVertex2i(O.x+(3/16)w, O.y+((3/8)*(p-h)+h);

glEnd ( );
glFlush ( );
}

void init(){


glClearColor (1.0, .0, .0, 0.6);
//glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 200.0);

}

void display_house(){
draw_shell();
draw_door();
draw_window();
draw_chimney();

}

void display(){
	House h1(100, 150, 200, Point (0,0) );
	House h2 (100, 200, 280, Point(150,0) );
	House h3 (50, 70, 90, Point (0, 300) );
//some gl code

	h1.display_house();
	//maybe some more gl code
	h2.display_house();
	h3.display_house();
}

};

void main (int argc, char** argv)
{
Point origin(20,20);
House h4(2, 10, 15, origin);
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (50, 100);
glutInitWindowSize (400, 300);
glutCreateWindow (“Bernie Project 1”);
//init ( ); //
glutDisplayFunc (display_house);
glutMainLoop ( );
}

If you use Netbeans and Windows, you can download my JOGL project plugin. There is a wizard to create a runnable OpenGL Application in there. You will just have to copy your classes into the provided main class, add a “gl.” before each gl call and copy the display_house code into the already provided display()-method.


public class JOGLApp{
  class Point{
    // (...)
  }

  class House{
    // (...)
  }

  // (...)

  public void display(GLAutoDrawable d)
  {
     GL gl= d.getGL() ;
     // your display_house code here
  }
}

See http://www.java-gaming.org/forums/index.php?topic=12530.0 for information how to download the plugin

hi…For netbeans environement, I just d/l Netbeans 5.0 IDE, one file only…Or do I need anything additional? Then I add your module…

thanks for all the help.

bernie

Just the IDE-installer and my plugin should be sufficient

Ok, netbeans is cool