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

Posted on 2009-02-08
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.
Question by:david-lee
    LVL 18

    Accepted Solution

    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.

    Author Comment

    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++.
    LVL 18

    Assisted Solution

    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
    Let me suggest to read the Java OpenGL FAQ:
    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 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

    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


    Author Comment

    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.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now