Udayakumar.P
|
Posted: March 04, 2010 08:40 by Udayakumar.P
|
|
I wanted to create a Graphical software using JOGL. While doing the coding I realized that it is not possible to insert Graphical components separately into the Graphical view area. Is there any way it can be done using JOGL. I am adding the complete code below. import java.awt.Color;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import com.sun.opengl.util.Animator;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
public class Grasoft extends JFrame {
static {
// When using a GLCanvas, we have to set the Popup-Menues to be HeavyWeight,
// so they display properly on top of the GLCanvas
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
}
private Animator animator;
/** Creates new form MainFrame */
public Grasoft() {
initComponents();
setTitle("Grasoft");
canvas.addGLEventListener(new GLRenderer());
animator = new Animator(canvas);
// This is a workaround for the GLCanvas not adjusting its size, when the frame is resized.
canvas.setMinimumSize(new Dimension());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
}
@Override
public void setVisible(boolean show){
if(!show)
animator.stop();
super.setVisible(show);
if(!show)
animator.start();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
JLabel label = new JLabel();
canvas = new GLCanvas(createGLCapabilites());
jPanel1 = new JPanel();
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
jButton4 = new JButton();
jButton5 = new JButton();
jPanel2 = new JPanel();
jButton6 = new JButton();
jButton7 = new JButton();
jButton8 = new JButton();
jButton9 = new JButton();
jButton10 = new JButton();
jColorChooser1 = new JColorChooser();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
label.setText(" Grasoft ");
jPanel1.setForeground(new Color(204, 204, 255));
jButton1.setText("Line");
jButton2.setText("Rectangle");
jButton3.setText("Triangle");
jButton4.setText("Circle");
jButton5.setText("Polygon");
GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
.addComponent(jButton5)
.addGroup(jPanel1Layout.createParallelGroup(Alignment.TRAILING, false)
.addComponent(jButton3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, Alignment.LEADING))
.addComponent(jButton4)
.addComponent(jButton1))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(jButton5))
);
jButton6.setText("Translate");
jButton7.setText("Rotate");
jButton8.setText("Scale");
jButton9.setText("Shear");
jButton10.setText("Exit");
GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addComponent(jButton6)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton10))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButton6)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(jButton7)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(jButton8)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton9)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton10)
.addContainerGap(131, Short.MAX_VALUE))
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(canvas, GroupLayout.PREFERRED_SIZE, 602, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(label, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(61, 61, 61)
.addComponent(jPanel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(canvas, GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE))
.addContainerGap())
);
GroupLayout layout1 = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(jColorChooser1, GroupLayout.DEFAULT_SIZE, 1222, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(34, 34, 34)
.addComponent(canvas, GroupLayout.DEFAULT_SIZE, 1075, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(286, 286, 286)
.addComponent(label)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(label, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40)
.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addComponent(canvas, GroupLayout.PREFERRED_SIZE, 543, GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addComponent(jColorChooser1, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
// </editor-fold>
// </editor-fold>
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
/**
* Called from within initComponents().
* hint: to customize the generated code choose 'Customize Code' in the contextmenu
* of the selected UI Component you wish to cutomize in design mode.
* @return Returns customized GLCapabilities.
*/
private GLCapabilities createGLCapabilites() {
GLCapabilities capabilities = new GLCapabilities();
capabilities.setHardwareAccelerated(true);
// try to enable 2x anti aliasing - should be supported on most hardware
// capabilities.setNumSamples(2);
//capabilities.setSampleBuffers(true);
return capabilities;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// Run this in the AWT event thread to prevent deadlocks and race conditions
EventQueue.invokeLater(new Runnable() {
public void run() {
// switch to system l&f for native font rendering etc.
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.INFO, "can not enable system look and feel", ex);
}
Grasoft frame = new Grasoft();
frame.setSize(1280,768);
frame.setVisible(true);
}
});
}
public class GLRenderer implements GLEventListener {
public void init(GLAutoDrawable drawable) {
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Enable VSync
gl.setSwapInterval(1);
// Setup the drawing area and shading mode
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
}
public void reshape (GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = new GLU();
if (height <= 0) { // avoid a divide by zero error!
height = 1;
}
final float h = (float) width / (float) height;
gl.glViewport(0,0, 700, 700);
///gl.glViewport(0, 0, height, width);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
private void drawLine(GL gl, float mode)
{
// Move the "drawing cursor" around
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
// Drawing Using Triangles
gl.glBegin(GL.GL_LINES);
gl.glColor3f(1.0f, 1.0f, 0.0f); // Set the current drawing color to red
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
// Finished Drawing The Triangle
gl.glEnd();
gl.glFlush();
}
private void drawTriangle(GL gl, float mode)
{
// Move the "drawing cursor" around
//gl.glTranslatef(-1.5f, 0.0f, -6.0f);
// Drawing Using Triangles
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1.0f, 0.0f, 0.0f); // Set the current drawing color to red
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top
gl.glColor3f(0.0f, 1.0f, 0.0f); // Set the current drawing color to green
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the current drawing color to blue
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
// Finished Drawing The Triangle
gl.glEnd();
}
private void drawRectangle(GL gl, float mode)
{
// Move the "drawing cursor" to another position
// gl.glTranslatef(3.0f, 0.0f, 0.0f);
// Draw A Quad
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0.5f, 0.5f, 1.0f); // Set the current drawing color to light blue
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
// Done Drawing The Quad
gl.glEnd();
gl.glFlush();
}
public void display(GLAutoDrawable drawable) {
final GL gl = drawable.getGL();
// Clear the drawing area
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();
drawLine(gl, GL.GL_RENDER);
//drawRectangle(gl, GL.GL_RENDER);
//drawTriangle(gl, GL.GL_RENDER);
jButton1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
drawTriangle(gl, GL.GL_RENDER);
}
});
jButton2.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
//drawRectangle(gl, GL.GL_RENDER);
//gl.glTranslatef(3.0f, 0.0f, 0.0f);
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0.5f, 0.5f, 1.0f); // Set the current drawing color to light blue
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
// Done Drawing The Quad
gl.glEnd();
//gl.glFlush();
//animator.stop();
canvas.repaint();
}
});
jButton3.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
drawTriangle(gl, GL.GL_RENDER);
gl.glFlush();
}
});
jButton6.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
gl.glPushMatrix();
gl.glTranslatef(0.0f, 0.0f, 5.0f);
gl.glPopMatrix();
gl.glFlush();
}
});
jButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
gl.glRotatef(0.0f, 1.0f, 0.0f, 0.0f);
}
});
jButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
drawTriangle(gl, GL.GL_RENDER);
}
});
jButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
drawRectangle(gl, GL.GL_RENDER);
canvas.display();
}
});
jButton10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
animator.stop();
System.exit(0);
}
});
// Flush all drawing operations to the graphics card
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private GLCanvas canvas;
private JPanel jPanel1;
private JPanel jPanel2;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JButton jButton4;
private JButton jButton5;
private JButton jButton6;
private JButton jButton7;
private JButton jButton8;
private JButton jButton9;
private JButton jButton10;
private JColorChooser jColorChooser1;
// End of variables declaration//GEN-END:variables
}Only Exit seems to work perfectly. Please forgive me for the cluttered code in Eventlisteners. That was done to test whether anything works. So should I backoff and use Java and its 2D/3D features for developing such a software? |
JOGL and Graphical software
Replies: 0 - Last Post: March 04, 2010 08:40
by: Udayakumar.P
by: Udayakumar.P
showing 1 - 1 of 1
Replies: 0 - Last Post: March 04, 2010 08:40
by: Udayakumar.P
by: Udayakumar.P






