We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


using opengl and java to draw pentagon or triangle moving in a circle

Medium Priority
Last Modified: 2013-11-19
how to draw  pentagon or triangle (wireframe for lines and solid for triangles) rotating in a circle using java.  Apply the Bresenham's line algorithm or a triangle filling scan-conversion algorithm.
Watch Question

Graphics Expert
We don't use Breseham's or other algorithms to draw lines by ourselves, when we use OpenGL. The same applies to filling polygons. OpenGL has functions to draw any polygon in wire frame or solid.
If your program must draw the lines or fill polygons, in a pixel by pixel basis, you don't need OpenGL. By the way, it will be extremely slower than OpenGL.
If you want to mix OpenGL and Java, take a look at
to understand the problems we have to join them...
I noticed the DHTML zone, so I'm assuming your objective is to develop a web page with 2D animated entities: triangles and pentagons, so, if my assumption is correct, consider the following combinations:
  • java and html, it is OK;
  • java and OpenGL, it is bad;
  • java + OpenGL + HTML; this is a confusion.
If you need interactive, high performance 3D, OpenGL and C++ to run in your computer, that's perfect.
If you need just 2D simple animation, take a look at Flash. Easer, simpler, faster to develop and perfectly adequated to a web page.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


may be I selected wrong zone.  my problem is to draw a circle then fill color triangle which can be rotated inside the circle.  the requirement is filling scan conversion algorithm.  the language is java with opengl.  this is computer graphic program, not for web design.  I do not know C++.
Jose ParrotGraphics Expert
At http://www.gamedev.net/reference/articles/article767.asp
there is a complete tutorial, so you can learn how Bresenham's algorithms calculate the pixels for lines and circles. There are Pascal programs there, which can be translated to java.
By the way, if you find C or C++ code it is easy to translate it to java, don't worry about.

If you must develop Breseham lines and circles, what is the purpose of using OpenGL? Do you will use OpenGL just to draw pixels? This is not a right approach to that objective.
To be efficient, the program should set pixels, in general, by using direct access to the graphic board, by using assembler or C. Or, better, by calling OpenGL functions directly to draw graphics primitives.
But, if you MUST use OpenGL you need Java JDK and JOGL. You can download them from http://opengl.j3d.org/installing.html
Let me suggest to read the Java OpenGL FAQ: http://opengl.j3d.org/faq/index.html
The difficult I have to answer directly what you are looking for is that I never read about Java + OpenGL + Breseham. Some considerations:
  • Java + OpenGL
    I have no experience on that, but know that JOGL is the right approach.
    To draw a line, the Java code calls OpenGL functions as in CODE_1 below.
  • Line algorithm in Java + OpenGL
    Makes no sense a slow program (Java, C#) to calculate pixel by pixel in a loop (Breseham or not) and to ask OpenGL just to draw it in a pixel by pixel basis, as in CODE_2 below.
    OpenGL is a powerful 3D environment (can be used also for 2D) and that approach will result in a very slow execution.
  • Breseham
    Useful if developed in assembler or in a high speed language with direct access to video memory. Makes no sense calculate each pixel in 0.001 second and spend 0.05 s to draw it in the screen, if I can draw the entire line in 0.005 s.
    Extremely useful if writen in machine code inside a graphics card firmware.
The same reasons apply to circle also. Anyway, the generic ellipse algorithm can be found at http://www.robots.ox.ac.uk/~awf/graphics/bres-ellipse.html If you need a circle, just make the axies equal.
Finnaly, every time I write or read about Breseham's algorithm, I make a wow to the brilliant Mr. Jack, who created it in 1962 when he was with IBM.
Full tutorial at http://gamedev.cs.colorado.edu/tutorials/Bresenham.pdf

GL.glVertex3f(-2.0f, 2.0f, 0.0f);
GL.glVertex3f(2.0f, -2.0f, 0.0f);
for(int i=0; i < x; i+=2)
    GL.glVertex3f(-2.0f, 2.0f, 0.0f);

Open in new window


thank you for your explaining and your links.  I have a program assignment which I have to write a circle then in that circle there will be a fillled color triangle, or small circle rotating around the circle.  I am struggling with algorithm  how to fill color for triangle with polygon scan conversion method.  Can you help me on that.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.