Ver Mensaje Individual
  #3 (permalink)  
Antiguo 23/05/2010, 00:04
joseluisbz
 
Fecha de Ingreso: febrero-2007
Ubicación: Barranquilla, Colombia
Mensajes: 181
Antigüedad: 17 años, 3 meses
Puntos: 3
De acuerdo Solución: Pintar Forma después de presionar botón.

Ahora veremos la solución:

La clase inicial que antes se llamaba Objeto2D ahora se llama Object2D:
Código Java:
Ver original
  1. class Object2D {
  2.   Color mc;
  3.   int TipoObjeto;
  4.   static int mLINE = 1;
  5.   static int mRECT = 2;
  6.   int X0,Y0, X1,Y1;
  7.   float ms;
  8.   Object2D(Color c, int t, int x0, int x1, int y0, int y1, float s){
  9.     TipoObjeto = t;
  10.     mc = c;
  11.     ms = s;
  12.     X0 = x0;
  13.     Y0 = y0;
  14.     X1 = x1;
  15.     Y1 = y1;
  16.   }
  17.   void ViewObject2D(BufferedImage bi){
  18.     Graphics2D g2d = bi.createGraphics();
  19.     g2d.setColor(mc);
  20.     g2d.setStroke(new BasicStroke(ms));
  21.     g2d.setPaintMode();
  22.     if (TipoObjeto==mLINE){
  23.       g2d.draw(new Line2D.Double(X0, X1, Y0, Y1));
  24.     }
  25.     if (TipoObjeto==mRECT){
  26.       g2d.draw(new Rectangle2D.Double(X0, Y0, X1, Y1));
  27.     }
  28.     g2d.dispose();
  29.   }
  30. }

Uso de la clase:
Hago uso de un vector tipo BufferedImage llamado bImage, así se "comunica" la clase contenedora con la contenida para pasarle el objeto de referencia para dibujar, éste es inicializado en el constructor de la clase Graficador.
Código Java:
Ver original
  1. public class Graficador extends javax.swing.JFrame {
  2.     /** Creates new form Graficador */
  3.   Vector<Object2D> ObjectVector  = new Vector<Object2D>();
  4.   BufferedImage bImage;
  5.   public Graficador() {
  6.     super( "Dibujo de figuras en 2D" );
  7.     setVisible(true);
  8.     initComponents();
  9.     bImage = new  BufferedImage(super.getWidth(),super.getHeight(),BufferedImage.TYPE_INT_RGB);
  10.   }

Solo cambia algunas cosas, ya no hay objeto Graphics como referencia de dibujo, sino BufferedImage, ahhh y también cambié los nombres de algunos métodos..para poder hacer preguntas con mi código en foros ingleses.
Código Java:
Ver original
  1. public void DrawVector(){
  2.     if(!ObjectVector.isEmpty()){
  3.       for (int i = 0; i<ObjectVector.size();i++){
  4.         ObjectVector.elementAt(i).ViewObject2D(bImage);
  5.       }
  6.     }
  7.   }
  8.  
  9.   public void FillVector(){
  10.     ObjectVector.addElement(new Object2D(new Color(0,255,255),Object2D.mLINE,250,300,500,90,0.6f));
  11.     ObjectVector.addElement(new Object2D(new Color(255,255,0),Object2D.mRECT,250,250,150,50,0.1f));
  12.   }

El método paint queda más sencillo, al final todos los cambios hechos se reflejarán al final al llamar g.drawImage(bImage, 0, 0, this);
Código Java:
Ver original
  1. public void paint(Graphics g) {
  2.     super.paint(g);
  3.     DrawVector();
  4.     g.drawImage(bImage, 0, 0, this);
  5.   }

El método que es llamado cuando es presionado el botón jbDibujar
Código Java:
Ver original
  1. private void jbDibujarActionPerformed(java.awt.event.ActionEvent evt) {  
  2.       FillVector();
  3.       repaint();
  4.     }

Espero les sea de ayuda...
__________________
Jose Luis Bernal Zambrano
Please response to:
[email protected]

Última edición por joseluisbz; 23/05/2010 a las 00:15