darksantos

prog

Jan 8th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.51 KB | None | 0 0
  1. package java2d_quadcurve2d;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.geom.Line2D;
  10. import java.awt.geom.Point2D;
  11. import java.awt.geom.QuadCurve2D;
  12. import java.text.NumberFormat;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPanel;
  15. /**
  16.  * @web http://jc-mouse.blogspot.com/
  17.  * @author Mouse
  18.  */
  19. public class miPaint extends JPanel {
  20.     private Point2D Punto1;
  21.     private Point2D Punto2;
  22.     private Point2D Punto3;    
  23.     private Point2D Punto;
  24.     private boolean band=true;
  25.     float rango=0.01f;
  26.    
  27.     public miPaint(Dimension d){
  28.       this.setPreferredSize(d);
  29.       this.setSize(d);
  30.       this.setVisible(true);
  31.       this.repaint();      
  32.       //se crean las coordenas al azar /*
  33.      
  34.       this.Punto1 = new Point2D.Double(getCoordenada(),getCoordenada());            
  35.       this.Punto2 = new Point2D.Double(getCoordenada(),getCoordenada());            
  36.       this.Punto3 = new Point2D.Double(getCoordenada(),getCoordenada());
  37.       this.setVisible(false);
  38.      
  39.     }
  40.     //actualiza nuevas coordenadas a los Puntos
  41.     public void setPoint2D(String x1,String y1,String x2,String y2,String x3,String y3, boolean b){
  42.         this.setVisible(true);
  43.         this.Punto1 = new Point2D.Float(Float.valueOf(x1),Float.valueOf(y1));            
  44.         this.Punto2 = new Point2D.Float(Float.valueOf(x3),Float.valueOf(y3));            
  45.         this.Punto3 = new Point2D.Float(Float.valueOf(x2),Float.valueOf(y2));            //control es punto2
  46.         this.band=b;
  47.     }
  48.    
  49.   @Override                  
  50.   public void paintComponent(Graphics g){
  51.         super.paintComponent(g);      
  52.         Graphics2D g2 = (Graphics2D)g;    
  53.         //grosor de pincel
  54.         g2.setStroke(new BasicStroke(2.0f));  
  55.         //color de pincel
  56.         g2.setColor(new Color(0,0,200));
  57.         if(band){
  58.             Dibujar_QuadCurve2D(g2);
  59.         }else{
  60.             Dibujar_Bezier(g2);
  61.         }        
  62.         this.repaint();
  63.         Mostrar_Datos(g2);
  64.    }
  65.  
  66.   //dibuja la curva con la funcion de java2d QuadCurve2D
  67.   public void Dibujar_QuadCurve2D(Graphics2D g2){    
  68.     g2.draw(new QuadCurve2D.Double(Punto1.getX(), Punto1.getY(), Punto2.getX(), Punto2.getY(), Punto3.getX(), Punto3.getY()));
  69.     //g2.drawLine(((int) Punto1.getX()), ((int) Punto1.getY()+200), (int) Punto1.getX(), (int) Punto1.getY());
  70.     //g2.draw(new QuadCurve2D.Double(Punto1.getX(), 81.396, Punto1.getX(), Punto1.getY(), Punto1.getX(), Punto1.getY()));
  71.     g2.drawLine((int)Punto1.getX(),(int) Punto1.getY(),(int)Punto1.getX(),390-79);
  72.     g2.drawLine((int)Punto3.getX(),(int) Punto3.getY(),(int)Punto3.getX(),390-153);
  73.     g2.draw(new QuadCurve2D.Double(Punto1.getX(), 390-79, Punto2.getX()-40, (Punto3.getY()+Punto1.getY())*0.8877, Punto3.getX(), 390-153));
  74.   }
  75.  
  76.   //dibuja la curva con la funcion de la curva cuadratica de Bezier
  77.   //(1-t)^2*p1 + 2t(1-t)p2 + t^2*p3
  78.   public void Dibujar_Bezier(Graphics2D g2){
  79.         float f1;
  80.         float f2;        
  81.         float t=0;          
  82.         while (t<=1.0){  
  83.             //obtiene las coordenadas
  84.             f1 = (float) (((1-t)*(1-t) * Punto1.getX()) + 2 * t * (1 - t) * Punto2.getX() + (t * t) * Punto3.getX());
  85.             f2 = (float) (((1-t)*(1-t) * Punto1.getY()) + 2 * t * (1 - t) * Punto2.getY() + (t * t) * Punto3.getY());            
  86.             //crea un punto con esas coordenadas
  87.             Punto = new Point2D.Float(f1,f2);  
  88.             //dibuja el punto
  89.             g2.draw(new Line2D.Double(Punto.getX(),Punto.getY(),Punto.getX(),Punto.getY()));      
  90.             t = (float) (t + rango);    
  91.         }      
  92.        
  93.   }
  94.  
  95.   //
  96.   public void setRango(String x){
  97.       if((Float.valueOf(x)<=1)&&(Float.valueOf(x)>0.001)){
  98.          this.rango=Float.valueOf(x);  
  99.       }else{
  100.         JOptionPane.showMessageDialog(this,"Solo valores entre [0.001 y 1.0]");            
  101.       }
  102.    
  103.   }
  104.   /* metodos auxiliares */
  105.  
  106.   //muestra lascoordenas de los puntos P1,P2 Y P3
  107.   //dibuja las lineas que estos forman
  108.   private void Mostrar_Datos(Graphics2D g2){
  109.         g2.setStroke(new BasicStroke(1.0f));
  110.         g2.setColor(new Color(0,0,0));
  111.         //g2.draw(new Line2D.Double(Punto1.getX(), Punto1.getY(), Punto2.getX(), Punto2.getY()));          
  112.         //g2.draw(new Line2D.Double(Punto3.getX(), Punto3.getY(), Punto2.getX(), Punto2.getY()));          
  113.         dibuja_coordenada3(g2,Punto1.getX(), Punto1.getY(),"T2");
  114.         //dibuja_coordenada(g2,Punto2.getX(), Punto2.getY(),"Punto de Control");
  115.         dibuja_coordenada3(g2,Punto3.getX(), Punto3.getY(),"T3");
  116.         //g2.draw(new Line2D.Double(Punto3.getX()+80, Punto3.getY(), Punto2.getX(), Punto2.getY()));
  117.        
  118.         g2.drawString("T" ,5,15);
  119.        
  120.        
  121.         dibuja_coordenada3(g2,Punto1.getX(), 390-79,"T1");
  122.         dibuja_coordenada(g2,Punto3.getX(), 390-153,"T4");
  123.        
  124.        
  125.         dibuja_coordenada2(g2,20, 20+5,"1400"); // antes 20
  126.         dibuja_coordenada2(g2,600, 390,"600");
  127.         dibuja_coordenada2(g2,20, 205+5,"700");
  128.         dibuja_coordenada2(g2,20, 390-79+5,"300");
  129.         dibuja_coordenada2(g2,20, 390-264+5,"1000");
  130.         dibuja_coordenada2(g2,20, 390-137+5,"518");
  131.         dibuja_coordenada2(g2,20, 390-137+5,"518");
  132.         dibuja_coordenada2(g2,20, 390-153+5,"578");
  133.        
  134.        
  135.         dibuja_coordenada3(g2,20, 400,"0");
  136.        
  137.        
  138.        
  139.         g2.setStroke(new BasicStroke(2.0f));
  140.        
  141.         g2.drawLine(20,20,20,400);   //LINEA VERTICAL PLANO
  142.         g2.drawLine(10,390,600,390);  //LINEA HORIZONTAL PLANO
  143.        
  144.         g2.setStroke(new BasicStroke(4.5f));
  145.   }
  146.  
  147.    //dibuja las coordenas dadas  un punto P1,P2  
  148. private void dibuja_coordenada(Graphics2D g2,double x, double y,String t){      
  149.     NumberFormat mf = NumberFormat.getInstance();
  150.     mf.setMaximumFractionDigits(2);
  151.     g2.setColor(new Color(255,0,0));
  152.     g2.setFont(new Font("Arial", Font.BOLD, 11));
  153.     g2.drawString(t + " ("+mf.format(x)+","+mf.format(y)+")",(float) (x+4),(float)  (y-10));      
  154.     g2.setStroke(new BasicStroke(4.5f));  
  155.     g2.draw(new Line2D.Double(x,y,x,y));      
  156.    }
  157.  
  158. private void dibuja_coordenada2(Graphics2D g2,double x, double y,String t){      
  159.     NumberFormat mf = NumberFormat.getInstance();
  160.     mf.setMaximumFractionDigits(2);
  161.     g2.setColor(new Color(255,0,0));
  162.     g2.setFont(new Font("Arial", Font.BOLD, 11));
  163.     g2.drawString(t ,(float) (x+4),(float)  (y));      
  164.     g2.setStroke(new BasicStroke(4.5f));  
  165.     //g2.draw(new Line2D.Double(x,y,x,y));      
  166.    }
  167. private void dibuja_coordenada3(Graphics2D g2,double x, double y,String t){      
  168.     NumberFormat mf = NumberFormat.getInstance();
  169.     mf.setMaximumFractionDigits(2);
  170.     g2.setColor(new Color(255,0,0));
  171.     g2.setFont(new Font("Arial", Font.BOLD, 11));
  172.     g2.drawString(t ,(float) (x+4),(float)  (y-15));      
  173.     g2.setStroke(new BasicStroke(4.5f));  
  174.     //g2.draw(new Line2D.Double(x,y,x,y));      
  175.    }
  176. private void dibuja_coordenada4(Graphics2D g2,double x, double y,String t){      
  177.     NumberFormat mf = NumberFormat.getInstance();
  178.     mf.setMaximumFractionDigits(2);
  179.     g2.setColor(new Color(255,0,0));
  180.     g2.setFont(new Font("Arial", Font.BOLD, 11));
  181.     g2.drawString(t ,(float) (x+4),(float)  (y-15));      
  182.     g2.setStroke(new BasicStroke(4.5f));}
  183. //genera un valor al azar
  184. private double getCoordenada(){
  185.     return ((Math.random()*350)+50);
  186. }
  187.  
  188.      
  189. }
Advertisement
Add Comment
Please, Sign In to add comment