You are on page 1of 27

FES ACATLAN Graficacin por Computadora Profesor: Oscar Gabriel Caballero Martnez Alumnos: Bueno Ocaa Aldo Guillermo

Puebla Martnez Ana Karen Selma Isabel Ruiz Madrigal Fecha: 04/04/2013

Algoritmo DDA en OpenGL


package Graficacion; import com.sun.opengl.util.*; import java.awt.*; import java.awt.event.*; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU;

/** * * @author Aldo Guillermo Bueno Ocana * Selma Isabel Ruiz Madrigal * Ana Karen Puebla Martinez * * @materia Graficacin por Computadora * * @grupo 2803 * * @fecha 3/04/2013 * */ public class DDAV2 implements GLEventListener{ public static void main(String[] args) { Frame frame = new Frame("DDAV2.java"); GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(new DDAV2()); frame.add(canvas); frame.setSize(640, 480); final Animator animator = new Animator(canvas); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { new Thread(new Runnable() { public void run() { animator.stop(); System.exit(0); } }).start(); } });

frame.setLocationRelativeTo(null); frame.setVisible(true); animator.start(); } //Fin del main public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); GLU glu = new GLU(); gl.glClearColor(1,1,1,0); gl.glMatrixMode(GL.GL_PROJECTION); glu.gluOrtho2D (0.0, 10.0, 0.0, 10.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glLoadIdentity(); gl.glPointSize(3.0f); gl.glColor3d(24/255.0,128/255.0,21/255.0); //correDDA1(gl); gl.glFlush(); } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); gl.glViewport(0, 0, width, height); } public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }

class AlgoDDA { int array[][] = new int[1000][1000]; int dx,dy,y0; int c; public void DDAline(int xa,int ya,int xb,int yb) { int x0 , x1, y1 ; int z; float k,x,y; //int c;

x0 = xa;y0 = ya;x1 = xb;y1 = yb;//2*ya-yb; if(Math.abs(x1 - x0)>Math.abs(y1 - y0))//|k|<1 { c = 1;//|k|<1 if (x0>x1) { int t; t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } dx = x1-x0; dy = y1-y0; k = (float)dy/(float)dx; y = y0; int i = 0; for ( x = x0; x <= x1; x++) { array[i][0] = (int)x; z=(int) (y+0.5); //y array[i][1] = z; i++; y+=k; // System.out.print(z+" "); // System.out.println(y); } } else//|k|>1 { c = 2; if (y0>y1) { int t; t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } dx = x1-x0; dy = y1-y0; k = (float)dx/(float)dy; // System.out.println(k); x = x0; int i = 0; for (y = y0; y <= y1; y++) { array[i][1] = (int)y; z=(int) (x+0.5); //y

// System.out.print(z+" "); array[i][0] = z; i++; x+=k; //System.out.print(x+" "); // System.out.println(y); } } } }

class DDA extends Canvas implements MouseListener//Applet { static final long serialVersionUID =1000; AlgoDDA algo; int x0,y0,x1,y1; int n; int can = 0; DDA() { setBackground(Color.LIGHT_GRAY); addMouseListener(this); } public void setX0(int x0) { this.x0 = x0; //System.out.print(x0); } public void setY0(int y0) { this.y0 = y0; } public void setX1(int x1) { this.x1 = x1; } public void setY1(int y1) { this.y1 = y1; }

int i = 0; public void mousePressed(MouseEvent e) { can = 1; if(i == 0) { this.x0 = e.getX(); this.y0 = e.getY(); System.out.print(x0+" "); System.out.println(y0); i++; } else { this.x1 = e.getX(); this.y1 = e.getY(); System.out.print(x1+" "); System.out.println(y1); if(Math.abs(x1 - x0)>Math.abs(y1 - y0))//|k|<1 { if (x0>x1) { int t; t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } } else//|k|>1 { if (y0>y1) { int t; t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } } repaint(); i = 0; } } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseReleased(MouseEvent e){can = 0;}

public void paint(Graphics g) { algo = new AlgoDDA(); algo.DDAline(x0,y0,x1,y1); int d; if(algo.c == 1)//|k|<1 d = Math.abs(algo.dx); else d = Math.abs(algo.dy); for (int i = 0;i<d;i++) { int x = algo.array[i][0]; int y = algo.array[i][1]; //y=2*algo.y0-y; //if (can == 0)//button //y = 200-y;//2*algo.y0 - y; g.drawString(".", x, y); } } } }

Algoritmo Alpha en OpenGL


package Graficacion; import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; /** * Aldo Guillermo Bueno Ocana * Puebla Martinez Ana Karen * Ruiz Madrigal Selma Isabel */ public class Alpha extends Applet implements MouseListener, ActionListener{ int xini=7, yini=3, xfin=4, yfin=5, pasos, contar;

boolean puntos, procesos; Button proceso; void cuadro(Graphics g, Color c){ int i; g.setColor(c); for(i=0;i<=250;i+=10){ g.drawLine(i, 0, i, 250); g.drawLine(0, i, 250, i); } } void correAlpha(Graphics g){ int dx = xfin - xini, dy = yfin - yini; int i; float alpha, x = xini, y = yini, incremento; contar++; if(Math.abs(dy)>Math.abs(dx)){ pasos = Math.abs(dy); } else { pasos = Math.abs(dx); } incremento = 1f / pasos; alpha = incremento; g.drawString("Valores Reales",290,15); g.drawString("Valores puestos",480,15); for(i=0;i<=contar; i++){ dibujaPunto(g, (int) x, (int) y, Color.green); g.setColor(Color.black); g.drawString("(" + x + "," + y + ")", 300, i*10+27); g.drawString("(" + (int) x + "," + (int) y + ")", 500, i*10+27); x = xini*(1-alpha) + xfin*alpha; y = yini*(1-alpha) + yfin*alpha; alpha += incremento; } if(contar == pasos){ proceso.setEnabled(false); procesos = false; } } @Override public void init() { procesos = puntos = false; xini = xfin = yini = yfin = contar = 0; pasos = 0; addMouseListener(this); proceso = new Button("<Proceso>"); proceso.addActionListener(this); proceso.setLocation(280, 120); proceso.setSize(50, 13);

add(proceso); } void dibujaPunto(Graphics g,int x, int y, Color c){ g.setColor(c); g.fillRect(x*10, y*10, 10, 10); } @Override public void mouseClicked(MouseEvent me) {} @Override public void mousePressed(MouseEvent me) { if (me.getX()<250 && me.getY()<250){ xini = me.getX()/10; yini = me.getY()/10; } } @Override public void mouseReleased(MouseEvent me) { if(me.getX()<250 && me.getY()<250){ xfin = me.getX()/10; yfin = me.getY()/10; puntos = true; repaint(); } } @Override public void mouseEntered(MouseEvent me) {} @Override public void mouseExited(MouseEvent me) {} @Override public void paint(Graphics g){ if (puntos){ dibujaPunto(g, xini, yini, Color.blue); dibujaPunto(g, xfin, yfin, Color.blue); g.setColor(Color.black); g.drawLine(xini*10+5, yini*10+5, xfin*10+5, yfin*10+5); } if (procesos){ correAlpha(g); } cuadro(g,Color.black); } @Override public void update(Graphics g){ paint(g); }

@Override public void actionPerformed(ActionEvent ae) { if(ae.getSource()==proceso){ procesos = true; repaint(); } } }

Applet el Algoritmo de Bresemham


package Graficacion; import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; /** * * Aldo Guillermo Bueno Ocana * Ana Karen Puebla Martinez * Selma Isabel Ruiz Madrigal */ public class bresenham3 extends Applet implements MouseListener, ActionListener{ int xini=1; int yini=1; int xfin=3; int yfin=5; int x0,y0,x1,y1; int pasos, contar; boolean puntos, procesos; Button proceso; void cuadro(Graphics g, Color c){ int i; g.setColor(c); for(i=0;i<=250;i+=10){ g.drawLine(i, 0, i, 250); g.drawLine(0, i, 250, i); } } public void correbre(Graphics g,int x0, int y0, int x1, int y1)

{ int x, y, dx, dy, p, incE, incNE, stepx, stepy; dx = (x1 - x0); dy = (y1 - y0); /* determinar que punto usar para empezar, cual para terminar */ if (dy < 0) { dy = -dy; stepy = -1; } else stepy = 1; if (dx < 0) { dx = -dx; stepx = -1; } else stepx = 1; x = x0; y = y0; g.drawLine( x0, y0, x0, y0); /* se cicla hasta llegar al extremo de la lnea */ if(dx>dy){ p = 2*dy - dx; incE = 2*dy; incNE = 2*(dy-dx); while (x != x1){ x = x + stepx; if (p < 0){ p = p + incE; } else { y = y + stepy; p = p + incNE; } g.drawLine( x, y, x, y); } } else{ p = 2*dx - dy; incE = 2*dx; incNE = 2*(dx-dy); while (y != y1){ y = y + stepy; if (p < 0){ p = p + incE; } else { x = x + stepx; p = p + incNE; } g.drawLine( x, y, x, y);

} } } @Override public void init() { procesos = puntos = false; xini = 1;xfin =2; yini =3; yfin =3; contar = 0; pasos = 0; addMouseListener(this); proceso = new Button("<Proceso>"); proceso.addActionListener(this); proceso.setLocation(280, 120); proceso.setSize(50, 13); add(proceso); } void dibujaPunto(Graphics g,int x, int y, Color c){ g.setColor(c); g.fillRect(x*10, y*10, 10, 10); } @Override public void mouseClicked(MouseEvent me) {} @Override public void mousePressed(MouseEvent me) { if (me.getX()<250 && me.getY()<250){ xini = me.getX()/10; yini = me.getY()/10; } } @Override public void mouseReleased(MouseEvent me) { if(me.getX()<250 && me.getY()<250){ xfin = me.getX()/10; yfin = me.getY()/10; puntos = true; repaint(); } } @Override public void mouseEntered(MouseEvent me) {} @Override public void mouseExited(MouseEvent me) {} @Override public void paint(Graphics g){ if (puntos){ dibujaPunto(g, xini, yini, Color.blue);

dibujaPunto(g, xfin, yfin, Color.blue); g.setColor(Color.black); g.drawLine(xini*10+5, yini*10+5, xfin*10+5, yfin*10+5); } if (procesos){ correbre(g,x0,y0,y1,y1); } cuadro(g,Color.black); } @Override public void update(Graphics g){ paint(g); } @Override public void actionPerformed(ActionEvent ae) { if(ae.getSource()==proceso){ procesos = true; repaint(); } } }

Algoritmo de Bresenham en OpenGL


package Graficacion; import java.awt.*; import java.awt.event.*; import java.util.*; /** * * Aldo Guillermo Bueno Ocana * Ana Karen Puebla Martinez * Selma Isabel Ruiz Madrigal */ public class Bresenham extends Frame { public static void main(String[] args){new Bresenham();} Bresenham() { super("Bresenham"); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}}); setSize(340, 230); add("Center", new CvBresenham()); show(); }

} class CvBresenham extends Canvas { float rWidth = 10.0F, rHeight = 7.5F, pixelSize; int centerX, centerY, dGrid = 10, maxX, maxY; void initgr() { Dimension d; d = getSize(); maxX = d.width - 1; maxY = d.height - 1; pixelSize = Math.max(rWidth/maxX, rHeight/maxY); centerX = maxX/2; centerY = maxY/2; } int iX(float x){return Math.round(centerX + x/pixelSize);} int iY(float y){return Math.round(centerY - y/pixelSize);} void putPixel(Graphics g, int x, int y) { int x1 = x * dGrid, y1 = y * dGrid, h = dGrid/2; g.drawOval(x1 - h, y1 - h, dGrid, dGrid); } void drawLine(Graphics g, int xP, int yP, int xQ, int yQ) { int x = xP, y = yP, D = 0, HX = xQ - xP, HY = yQ - yP, c, M, xInc = 1, yInc = 1; if (HX < 0){xInc = -1; HX = -HX;} if (HY < 0){yInc = -1; HY = -HY;} if (HY <= HX) { c = 2 * HX; M = 2 * HY; for (;;) { putPixel(g, x, y); if (x == xQ) break; x += xInc; D += M; if (D > HX){y += yInc; D -= c;} } } else { c = 2 * HY; M = 2 * HX; for (;;) { putPixel(g, x, y); if (y == yQ) break; y += yInc; D += M; if (D > HY){x += xInc; D -= c;} } }

} void drawCircle(Graphics g, int xC, int yC, int r) { int x = 0, y = r, u = 1, v = 2 * r - 1, E = 0; while (x < y) { putPixel(g, xC + x, yC + y); // NNE putPixel(g, xC + y, yC - x); // ESE putPixel(g, xC - x, yC - y); // SSW putPixel(g, xC - y, yC + x); // WNW x++; E += u; u += 2; if (v < 2 * E){y--; E -= v; v -= 2;} if (x > y) break; putPixel(g, xC + y, yC + x); // ENE putPixel(g, xC + x, yC - y); // SSE putPixel(g, xC - y, yC - x); // WSW putPixel(g, xC - x, yC + y); // NNW } } void showGrid(Graphics g) { for (int x=dGrid; x<=maxX; x+=dGrid) for (int y=dGrid; y<=maxY; y+=dGrid) g.drawLine(x, y, x, y); } public void paint(Graphics g) { initgr(); showGrid(g); drawLine(g, 1, 1, 12, 5); drawCircle(g, 23, 10, 8); } }

Grfica de una lnea, calculando el punto medio. Y Programe el Algoritmo de punto medio en OpenGL
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Graficacion.linea; /** * * @author Aldo */

public class formulario extends javax.swing.JFrame { /** * Creates new form formulario */ public formulario() { initComponents(); } /** * 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"> private void initComponents() { area_grafica1 = new Graficacion.linea.area_grafica(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout area_grafica1Layout = new javax.swing.GroupLayout(area_grafica1); area_grafica1.setLayout(area_grafica1Layout); area_grafica1Layout.setHorizontalGroup( area_grafica1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 315, Short.MAX_VALUE) ); area_grafica1Layout.setVerticalGroup( area_grafica1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 215, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(27, 27, 27) .addComponent(area_grafica1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(58, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(35, 35, 35)

.addComponent(area_grafica1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(50, Short.MAX_VALUE)) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new formulario().setVisible(true);

} }); } // Variables declaration - do not modify private Graficacion.linea.area_grafica area_grafica1; // End of variables declaration } package Graficacion.linea; import java.awt.Color; import java.awt.Graphics; public class area_grafica extends javax.swing.JPanel { private int x0,y0,x1,y1,aux; Color color; @Override public void paint(Graphics g) { super.paint(g); g.setColor(color.blue); int dx=x1-x0; int dy=y1-y0; if(Math.abs(dx)>Math.abs(dy)) { float m=(float) dy /(float) dx; float b=y0- m*x0; if(dx<0) { dx=-1; } else { dx=1; } while(x0!=x1) { x0+=dx; y0=Math.round(m*x0+b); g.drawLine(x0,y0,x0,y0); } } g.drawString(Distancialinea(),50,50); g.drawString(mediolinea(),50,60); } public String Distancialinea()

{ double distancia=Math.sqrt(Math.pow((x1-x0),2)+(Math.pow((y1-y0),2))); String cad="Distancia entre los dos puntos"+(distancia); return cad; } public String mediolinea() { double x2=((x1+x0)/2); double y2=((y1+y0)/2); String cad="Punto medio: X: " + (x2)+" Y:"+(y2); return cad; } public area_grafica() { initComponents(); }

@SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { formMousePressed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); }// </editor-fold> private void formMousePressed(java.awt.event.MouseEvent evt) { // TODO add your handling code here: if(aux==2) { x1=evt.getX(); y1=evt.getY();

aux=0; repaint(); } if(aux==1) { x0=evt.getX(); y0=evt.getY(); } aux++; }

// Variables declaration - do not modify // End of variables declaration }

Circulo OpenGl
package Graficacion; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Ellipse2D; import javax.swing.JApplet; import javax.swing.JFrame; public class Circulo3 extends JApplet { final static BasicStroke stroke = new BasicStroke(2.0f); public void init() { setBackground(Color.white); setForeground(Color.white); }

public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.gray); int x = 5; int y = 7;

g2.setStroke(stroke); g2.draw(new Ellipse2D.Double(x, y, 200, 200)); g2.drawString("Circulo", x, 250); } public static void main(String s[]) { JFrame f = new JFrame(""); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JApplet applet = new Circulo3(); f.getContentPane().add("Center", applet); applet.init(); f.pack(); f.setSize(new Dimension(300, 300)); f.show(); } }

Elipse Opengll
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Graficacion; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Arc2D; import java.awt.geom.Ellipse2D; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D;

import java.awt.geom.RoundRectangle2D; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JApplet; import java.awt.Graphics; public class Elipse extends JApplet { public void paint( Graphics g ) { super.paint( g ); g.fillOval( 100, 50, 40, 100 ); g.drawOval( 100, 200, 100, 40 ); int X = 250, Y = 225; int r = 25; g.drawOval( X - r, Y - r, r * 2, r * 2 ); } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */

Elipse applet
package Graficacion; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; /** * * @author Aldo Guillermo Bueno Ocana * Selma Isabel Ruiz Madrigal * Ana Karen Puebla Martinez * * @materia Graficacin por Computadora * * @grupo 2803 * * @fecha 4/04/2013

* */

public class elipse1 extends JApplet implements Runnable { private static Color colors[] = { Color.blue, Color.cyan, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.yellow, Color.lightGray, Color.white }; private Thread thread; private BufferedImage bimg; private Ellipse2D.Float[] ellipses; private double esize[]; private float estroke[]; private double maxSize;

public void init() { setBackground(Color.black); // an array of type Ellipse2D.Float ellipses = new Ellipse2D.Float[25]; // a double array initialized to the length of the ellipses array esize = new double[ellipses.length]; // a float array initialized to the length of the ellipses array estroke = new float[ellipses.length]; // fills the ellipses array with Ellipse2D.Float objects for (int i = 0; i < ellipses.length; i++) { ellipses[i] = new Ellipse2D.Float(); // gets location for each ellipse with the given random size getRandomXY(i, 20 * Math.random(), 200, 200); } }

/* * sets the bounds of the ellipse specified by i, using the given * width, height and random size. */ public void getRandomXY(int i, double size, int w, int h) { esize[i] = size; estroke[i] = 1.0f; double x = Math.random() * (w-(maxSize/2)); double y = Math.random() * (h-(maxSize/2)); ellipses[i].setFrame(x, y, size, size);

/* * resets the bounds of the ellipses with the given width and height */ public void reset(int w, int h) { maxSize = w/10; for (int i = 0; i < ellipses.length; i++ ) { getRandomXY(i, maxSize * Math.random(), w, h); } }

/* * increase each stroke size and ellipse size until maxSize */ public void step(int w, int h) { for (int i = 0; i < ellipses.length; i++) { estroke[i] += 0.025f; esize[i]++; if (esize[i] > maxSize) { getRandomXY(i, 1, w, h); } else { ellipses[i].setFrame(ellipses[i].getX(), ellipses[i].getY(), esize[i], esize[i]); } } }

public void drawDemo(int w, int h, Graphics2D g2) { // sets the color and stroke size and draws each ellipse for (int i = 0; i < ellipses.length; i++) { g2.setColor(colors[i%colors.length]); g2.setStroke(new BasicStroke(estroke[i])); g2.draw(ellipses[i]); } }

public Graphics2D createGraphics2D(int w, int h) { Graphics2D g2 = null; if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) { bimg = (BufferedImage) createImage(w, h); reset(w, h); } g2 = bimg.createGraphics();

g2.setBackground(getBackground()); g2.clearRect(0, 0, w, h); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return g2; }

public void paint(Graphics g) { Dimension d = getSize(); step(d.width, d.height); Graphics2D g2 = createGraphics2D(d.width, d.height); drawDemo(d.width, d.height, g2); g2.dispose(); g.drawImage(bimg, 0, 0, this); }

public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); }

public synchronized void stop() { thread = null; }

public void run() { Thread me = Thread.currentThread(); while (thread == me) { repaint(); try { thread.sleep(10); } catch (InterruptedException e) { break; } } thread = null; }

public static void main(String argv[]) { final elipse1 demo = new elipse1(); demo.init(); Frame f = new Frame("Java 2D(TM) Demo - Ellipses"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);}

public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }); f.add(demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }

Parbola en OpenGL.
package Graficacion; import java.awt.*; import javax.swing.*; /** * * @author Aldo Guillermo Bueno Ocana * Selma Isabel Ruiz Madrigal * Ana Karen Puebla Martinez * * @materia Graficacin por Computadora * * @grupo 2803 * * @fecha 4/04/2013 * */ class MiArco extends JFrame{ //int x=250,y=200; final int fx=400,fy=400; int x,y; public MiArco(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(1500,1500); setVisible(true); } public void m(){ int t=15; for(int h=0;h<t;h++){ int auxx=t-h; int auxy=auxx*auxx;

try{ Thread.sleep(15);}catch(Exception e){} x=(fx-auxx*12); y=(fy+auxx*auxx); repaint(); } try{ Thread.sleep(50);}catch(Exception e){}

for(int h=0;h<t;h++){ int auxx=h; int auxy=auxx*auxx; try{ Thread.sleep(15);}catch(Exception e){} x=(fx+auxx*12); y=(fy+auxx*auxx); repaint(); } } public void paint(Graphics g){ try{ Thread.sleep(50); }catch(Exception e){} System.out.println("x"+x+"y"+y); g.fillOval(x,y, 8,8); } public static void main(String arg[]){ MiArco mi=new MiArco(); try{ Thread.sleep(1000);}catch(Exception e){} mi.m(); } }

You might also like