This program translates the circle on pressing the translate button.
Here is the source:(online Viewing)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.awt.GLJPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class CircleTranslate extends GLJPanel implements GLEventListener,ActionListener {
/**
* Interface to the GLU library.
*/
private GLU glu;
int x,y;
static JButton okButton;
/**
* Take care of initialization here.
*/
CircleTranslate()
{
// super(g);
super( new GLCapabilities(null) ); // Makes a panel with default OpenGL "capabilities".
GLJPanel drawable = new GLJPanel(); // new GLJPanel inside GLJPanel
drawable.setPreferredSize(new Dimension(200,100));
setLayout(new BorderLayout());
add(drawable, BorderLayout.CENTER);
drawable.addGLEventListener(this); // Set up events for OpenGL drawing!
// drawable.addMouseListener(this);
//drawable.addMouseMotionListener(this);
//drawable.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// System.exit(0);
x++;
repaint();
}
public void init(GLAutoDrawable gld) {
GL2 gl = gld.getGL().getGL2();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
x=10;y=30;
repaint();
}
/**
* Take care of drawing here.
*/
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
drawCircle(gl, x, y, 50);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
private void drawCircle(GL2 gl, int x1, int y1, int r) {
gl.glPointSize(1.0f);
gl.glBegin(GL2.GL_POINTS);
int x=0,y=r,p=1-r;
while(x<y)
{
if(p<0)
p+=2*x+3;
else {
p+=2*(x-y)+5;
y--;
}
x++;
circleSymmetry(gl,x1,y1,x,y);
}
gl.glEnd();//end drawing of points
}
public void dispose(GLAutoDrawable arg0)
{
}
public void circleSymmetry(GL2 gl,int xc,int yc,int x,int y)
{
gl.glVertex2i(xc+x , yc+y );
gl.glVertex2i(xc-x , yc+y );
gl.glVertex2i(xc+x , yc-y );
gl.glVertex2i(xc-x , yc-y );
gl.glVertex2i(xc+y , yc+x );
gl.glVertex2i(xc+y , yc-x );
gl.glVertex2i(xc-y , yc+x );
gl.glVertex2i(xc-y , yc-x );
}
public static void main(String args[])
{
JFrame window = new JFrame("Circle Translate");
// The canvas
CircleTranslate panel = new CircleTranslate();
panel.setPreferredSize(new Dimension(1000,1000));
okButton = new JButton("Translate");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(panel, BorderLayout.CENTER);
content.add(okButton, BorderLayout.SOUTH);
okButton.addActionListener(panel);
window.setContentPane(content);
window.pack();
window.setLocation(0,0);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
panel.requestFocusInWindow();
}
}
Here is the source:(online Viewing)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.awt.GLJPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class CircleTranslate extends GLJPanel implements GLEventListener,ActionListener {
/**
* Interface to the GLU library.
*/
private GLU glu;
int x,y;
static JButton okButton;
/**
* Take care of initialization here.
*/
CircleTranslate()
{
// super(g);
super( new GLCapabilities(null) ); // Makes a panel with default OpenGL "capabilities".
GLJPanel drawable = new GLJPanel(); // new GLJPanel inside GLJPanel
drawable.setPreferredSize(new Dimension(200,100));
setLayout(new BorderLayout());
add(drawable, BorderLayout.CENTER);
drawable.addGLEventListener(this); // Set up events for OpenGL drawing!
// drawable.addMouseListener(this);
//drawable.addMouseMotionListener(this);
//drawable.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// System.exit(0);
x++;
repaint();
}
public void init(GLAutoDrawable gld) {
GL2 gl = gld.getGL().getGL2();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
x=10;y=30;
repaint();
}
/**
* Take care of drawing here.
*/
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
drawCircle(gl, x, y, 50);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
private void drawCircle(GL2 gl, int x1, int y1, int r) {
gl.glPointSize(1.0f);
gl.glBegin(GL2.GL_POINTS);
int x=0,y=r,p=1-r;
while(x<y)
{
if(p<0)
p+=2*x+3;
else {
p+=2*(x-y)+5;
y--;
}
x++;
circleSymmetry(gl,x1,y1,x,y);
}
gl.glEnd();//end drawing of points
}
public void dispose(GLAutoDrawable arg0)
{
}
public void circleSymmetry(GL2 gl,int xc,int yc,int x,int y)
{
gl.glVertex2i(xc+x , yc+y );
gl.glVertex2i(xc-x , yc+y );
gl.glVertex2i(xc+x , yc-y );
gl.glVertex2i(xc-x , yc-y );
gl.glVertex2i(xc+y , yc+x );
gl.glVertex2i(xc+y , yc-x );
gl.glVertex2i(xc-y , yc+x );
gl.glVertex2i(xc-y , yc-x );
}
public static void main(String args[])
{
JFrame window = new JFrame("Circle Translate");
// The canvas
CircleTranslate panel = new CircleTranslate();
panel.setPreferredSize(new Dimension(1000,1000));
okButton = new JButton("Translate");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(panel, BorderLayout.CENTER);
content.add(okButton, BorderLayout.SOUTH);
okButton.addActionListener(panel);
window.setContentPane(content);
window.pack();
window.setLocation(0,0);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
panel.requestFocusInWindow();
}
}
Thanks!!!
No comments:
Post a Comment