JOGL help on bar chart project

Hi there,

I’m a beginner in JOGL and I’m not all that familiar with it’s terminology and syntax. I’m trying to build a bar graph program using JOGL and I want to use GUI/Swing components within it.

The whole basis of how this program is to work is that the user selects an amount of bars they want displayed using a textfield, choose a color for each bar and the x and y points using textfields using Radio buttons , then it is diplayed on the canvas.

Now this what I have here:

import java.awt.;
import java.awt.event.
;
import net.java.games.jogl.;
import java.io.
;
import cs1.Keyboard;

public class Experiment extends JFrame implements ActionListener
{

       private JPanel panel, colorBox, barArea, pointArea, buttonArea,screen; 
    private JLabel enterBar, enterP1,enterP2,/*howMany,*/blank1, blank2, blank3; 
    private JTextField barNum, point1, point2; 
    private JRadioButton red, blue, green, yellow, magenta, black, cyan;
    private JButton readFile, display, /*showColor,*/clear1;       
         
    
public static void main( String args[])
 { 
        JFrame frame = new JFrame ();//frame
  
  
  
        
        GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());



          
         canvas.addGLEventListener(new Renderer ()); 
          
        
          
          frame.add("Center", canvas);
          frame.setSize(800,800);
          
          frame.addWindowListener(new WindowAdapter()

            {
        
              public void windowClosing(WindowEvent e)
        
              {
        
                
        
                System.exit(0);
        
              }
        
            });        
                         
         frame.show();
         canvas.requestFocus();  
 
 
      
  
        
       


 Experiment () 
 { 
      super ("Bar Selection Window");
      setSize (320, 300); 
      panel = new JPanel (); 
      panel.setBackground (Color.white); 
     
     //Buttons
      readFile = new JButton (" Read");
      display = new JButton ("Display Bar Chart");
      //showColor = new JButton ("Add Color");
      clear1 = new JButton ("Clear Screen");
      clear2 = new JButton ("Clear Canvas");
      
      
      //Labels
      enterBar  = new JLabel ("Enter Bar Amount To Display: "); 
      enterP1 = new JLabel ("Enter First point value:");
      enterP2= new JLabel ("Enter Second point value:");
      //howMany = new JLabel (" You can show up to 6 bars..No more, No less:");
      blank1 = new JLabel (" ");
      blank2 = new JLabel (" ");
      blank3 = new JLabel (" ");
      
     
     //TextFields
      barNum = new JTextField (5);
      point1 = new JTextField (10); 
      point2 =  new JTextField (10);
      
      //Choice Menu
      //Choice languageMenu = new Choice(); 
      
      //Radio Buttons
      
      red = new JRadioButton (" Red",true);
      blue = new JRadioButton (" Blue", false);
      green = new JRadioButton ("Green", false);
      yellow = new JRadioButton ("Yellow", false);
      magenta = new JRadioButton (" Magenta", false);
      black = new JRadioButton (" Black", false);
      cyan = new JRadioButton (" Cyan", false);
      
      
               
      
      
      
      //*Adding Components*//
      
      //Bar Chart Main Label
      //panel.add (howMany);
      panel.add( new Label("Show up to six bars on Graphic Screen:",Label.CENTER)); 

      //Bar Area
      barArea = new JPanel ();
      barArea.setLayout (new BoxLayout (barArea, BoxLayout.Y_AXIS));
      barArea.add (enterBar);
      barArea.add (barNum);
     
     
     
      
      //Point Area
      pointArea = new JPanel (); 
      pointArea.setLayout (new BoxLayout (pointArea, BoxLayout.Y_AXIS));
      pointArea.add (enterP1);
      pointArea.add (point1);
      pointArea.add (blank1);
      pointArea.add (enterP2); 
      pointArea.add (point2);
      
      //Button Area
      buttonArea = new JPanel ();
      buttonArea.setLayout (new BoxLayout (buttonArea, BoxLayout.Y_AXIS));
      buttonArea.add (readFile);
      buttonArea.add (display);
      buttonArea.add (blank2);
      buttonArea.add (clear1);
      buttonArea.add (clear2);
      
      
      
        
              
        //RadioButton Area
        colorBox = new JPanel ();
      colorBox.setLayout (new BoxLayout (colorBox, BoxLayout.Y_AXIS));
      colorBox.add (red);
      colorBox.add (blue);
      colorBox.add (green);
      colorBox.add (yellow);
          colorBox.add (cyan); 
        colorBox.add (magenta); 
        
        
          //Panel Set-Up
          screen = new JPanel ();
          screen.setLayout (new BorderLayout(25, 20));
          screen.add (barArea, BorderLayout.NORTH);
          screen.add (colorBox, BorderLayout.WEST);
          screen.add (pointArea, BorderLayout.CENTER);
          screen.add (buttonArea, BorderLayout.EAST);
          setContentPane (screen);
          
             

    //Button Listeners 
    readFile.addActionListener (new ReadListener ()); 
                
                 

    //showColor.addActionListener (new ShowColorListener ());             
     
    clear1.addActionListener (new ClearOneListener ()); 
                     
   
           //Color Listeners  
    red.addActionListener (new RedListener());
      
    blue.addActionListener (new BlueListener ());
    
    green.addActionListener (new GreenListener ());
    
    yellow.addActionListener (new YellowListener ());
    
    cyan.addActionListener (new CyanListener ());
    
    magenta.addActionListener (new MagentaListener ());
    
    black.addActionListener (new BlackListener ());
          
          
          
   }//init  

} //void main

  static class Renderer implements GLEventListener, KeyListener
  
  { //static class start
  
      
  
      
        
            public void barGraph (GL gl)
        
              {
        
              gl.glClear (GL.GL_COLOR_BUFFER_BIT);  // Set display window to color.
              
              class ReadListener implements ActionListener 
              { 
             public void actionPerformed (ActionEvent e) 
              {
                        int counter = 0;
                        int x1, x2;
                         x1 = Integer.parseInt(point1.getText());
                              x1.setText((gl.glRecti(x1,0,x2,0)));
                        
                        x2 = Integer.parseInt (point2.getText ());
                              x2.setText((gl.glRecti(x1,0,x2,0)));
              
                        int numOFbar = Integer.parseInt (barNum.getText());
                             numOFbar.setText (counter = counter + numOFbar);
                             
            
            
            }
            
       } // class ReadListener 

(Continued)

         // inner classes that define the action listeners for the buttons. 
       class RedListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent e)
                {
                     
                   gl.glColor3f(1.0f, 0.0f, 0.0f); //red
  
                             
                             
                       }          
         }    
                     
                     
       class BlueListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent e)
            
                 { 
                    
                                      gl.glColor3f (0.0f, 0.0f, 0.75f);  //blue
                       }
                             
                               
         }    
                     
       class GreenListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent e)
            
                 { 
                     
                                gl.glColor3f(0.0f, 1.0f, 0.0f); //green 
                          }
                             
                         
         }    
                     
                                                
       class YellowListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent e)
            
              { 
                           gl.glColor3f(1.0f, 1.0f, 0.0f); //yellow
                             
                             
                    }          
         }    
                     
                     
       class CyanListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent e)
                         
              { 
                          gl.glColor3f(0.0f, 1.0f, 1.0f); //cyan
  
                  }
                             
                               
         }   
                     
       class BlackListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent e)
            
                 { 
                    
                               gl.glColor3f(0.0f, 0.0f, 0.0f);  //black
                          }
                             
                               
         }    
                     
                     
       class MagentaListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent e)
            
              { 
                                    
                    gl.glColor3f(1.0f, 0.0f, 1.0f); //magenta            
                    }
                             
                              
         }   
                                                               
                     
     
        class ClearOneListener implements ActionListener 
       { 
            public void actionPerformed (ActionEvent event)
            
                 { 
                      barNum.clear();
                      point1.clear();
                      point2.clear();
                 }
       }                       
                                  
        
        
      
          
  
   }
  
              
              
  
  
   
  public void display(GLDrawable gLDrawable)
  
      {
  
        final GL gl = gLDrawable.getGL();
  
        
  
        gl.glMatrixMode (GL.GL_MODELVIEW);
  
        gl.glLoadIdentity();
  
        
  
        barChart(gLDrawable.getGL());
  
       
  
      }
  
      
  
      
  
         public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
  
      {
  
      }
  
      
  
      public void init(GLDrawable gLDrawable)
  
      {
  
        final GL gl = gLDrawable.getGL();
  
        final GLU glu = gLDrawable.getGLU();
  
              
  
        gl.glMatrixMode (GL.GL_PROJECTION);  
  
        gl.glClearColor (1.0f, 1.0f, 1.0f, 0.0f);   //set background to white 
  
        glu.gluOrtho2D (0.0, 200.0, 0.0, 150.0);  // define drawing area
  
        gLDrawable.addKeyListener(this);
  
      }
  
      
  
         public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height)
  
      {
  
        
  
      }
  
   
  
      
  
      public void keyPressed(KeyEvent e)
  
      {
  
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
  
          System.exit(0);
  
      }
  
         
  
      public void keyReleased(KeyEvent e) {}
  
      
  
      public void keyTyped(KeyEvent e) {}

}//static end

}//class Experiment end

Now it looks alot but it really isn’t. Basically, I’ve establishd my components (textfields, buttons, etc.) and ActionListeners and tried displaying them on the canvas.

One problem is that when I compiled the project , it did not display the buttons, textfields on the canvas.

I assume you need this portion of code to establish the canvas and GL features to work in the program:

GLCapabilities caps = new GLCapabilities();
BarWindow frame = new BarWindow ();
frame.addWindowListener (new WindowCloser ());
GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

canvas.addGLEventListener(new Renderer());

frame.add(canvas);

frame.setSize(400, 300);

Do I need to establish some other frame so that the GUI components I have can work in some way? Or is the GL canvas sufficient enough to get my GUI/swing stuff working AND display the bars?

Plus I’m having problems getting data from the textfields and transferring them into another set of data:

For instance, when you ask the user for a point x1, and y1 and point x2, and y2, I want to use those integers in the (gl.glRecti(x1,y2,x2,y2) GL component that creates bars.

So if the user says they want point 60,120,90,250 in the textfields, then that data gets inserted in this: (gl.glRecti(60,120,90,250) to display a bar. You get what I’m trying to do?

It’s suppose to be some user interface of some kind to get the user to insert some points and how many bars they want display with whatever color and it’s suppose to someway show on the GL canvas or whatever you call it.

I just have problems getting the syntax to work consistently and fluently.

If anyone has better suggestions or code examples, I’d REALLY appreciate it. Again, this is my first time using it and I’m still learing and brainstorming along the way, so please be thorough and detailed in your examples or code suggestions if you have one.

Thank You.

Ick, thats a lot of code. You don’t want to be changing GL state in the wrong thread, so those colour3f calls are going to cause all sorts of unknown havok. Move them into the same thread that calls the GLEventListener methods.