• Viernes 19 de Abril de 2024, 13:52

Autor Tema:  reloj componente, grafico, javabeans  (Leído 2076 veces)

sanlegas2000

  • Nuevo Miembro
  • *
  • Mensajes: 17
    • Ver Perfil
reloj componente, grafico, javabeans
« en: Miércoles 25 de Abril de 2012, 23:20 »
0
Bueno amigos el caso es que me dejaro hacer un reloj, para ser honesto el codigo me lo pasaron, la cuestion es que tengo que hacer un componente con el, pasarlo a javabeans, ya trate de hacerlo por mi cuenta y no he avanzado mucho no se si me puedan ayudar ustedes Gracias
Código: Java(TM) 2 Platform Standard Edition 5.0
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. public class Reloj1 extends JFrame{
  6.        
  7.         private Point puntoCentro;
  8.         private JFrame ventana;
  9.         private JButton boton;
  10.         private Container contenido;
  11.         private Graphics objeto;
  12.         private boolean ban;
  13. public Reloj1(int x,int y,int a,int b)
  14.     {
  15.         contenido=getContentPane();
  16.         contenido.setBackground(Color.white);
  17.         setSize(a-90,b-35);
  18.         setLocation(x,y);
  19.         setVisible(true);
  20.        
  21.         PuntoCentro(new Point(x,y));
  22.        
  23.         contenido.setLayout(null);
  24.         objeto=contenido.getGraphics();
  25.         ban=true;
  26.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  27.         while(ban==true)
  28.         {
  29.          try{
  30.                   Thread.sleep(1000);
  31.                   traza();
  32.               manecillas();
  33.                 }catch(Exception e){};
  34.         }
  35.        
  36.        
  37.        
  38.     }
  39.    
  40.    
  41.     public void PuntoCentro(Point punto)
  42.     {
  43.         puntoCentro=punto;
  44.     }
  45.    
  46.     public void traza()
  47.     {  
  48.         objeto.setColor(Color.WHITE);
  49.         objeto.fillRect(puntoCentro.x-100,puntoCentro.y-100,200,200);
  50.         objeto.setColor(Color.RED);
  51.         objeto.fillOval(puntoCentro.x-2,puntoCentro.y-2,5,5);
  52.         objeto.setColor(Color.BLUE);
  53.         int hora;
  54.         Calendar calendario = new GregorianCalendar();
  55.         hora =calendario.get(Calendar.HOUR_OF_DAY);
  56.         for(int i=0;i<12;i++)
  57.         {
  58.             double ang=(AnguloHora(i,60));
  59.             int x2=(int)(90*(Math.cos(ang)));
  60.             int y2=(int)(90*(Math.sin(ang)));
  61.             objeto.drawString(""+(i+1),((puntoCentro.x)+(x2))-2,((puntoCentro.y)-(y2))+5);
  62.         }
  63.     }
  64.    
  65.     public void manecillas()
  66.     {
  67.         int hora,minutos,segundos;
  68.         Calendar calendario = new GregorianCalendar();
  69.         hora =calendario.get(Calendar.HOUR_OF_DAY);
  70.         minutos=calendario.get(Calendar.MINUTE);
  71.         segundos=calendario.get(Calendar.SECOND);
  72.         double an=(AnguloMin(minutos));
  73.         double ang=(AnguloHora(hora,minutos));
  74.         int x2=(int)(90*(Math.cos(an)));
  75.         int y2=(int)(90*(Math.sin(an)));
  76.         objeto.setColor(Color.RED);
  77.        
  78.         objeto.fillOval(((puntoCentro.x)+(x2))-2,((puntoCentro.y)-(y2))-2,5,5);
  79.         objeto.drawLine(puntoCentro.x,puntoCentro.y,(puntoCentro.x)+(x2),(puntoCentro.y)-(y2));
  80.        
  81.        
  82.         an=(AnguloMin(segundos));
  83.         x2=(int)(90*(Math.cos(an)));
  84.         y2=(int)(90*(Math.sin(an)));
  85.         objeto.fillOval(((puntoCentro.x)+(x2))-2,((puntoCentro.y)-(y2))-2,5,5);
  86.         objeto.drawLine(puntoCentro.x,puntoCentro.y,(puntoCentro.x)+(x2),(puntoCentro.y)-(y2));
  87.        
  88.         x2=(int)(60*(Math.cos(ang)));
  89.         y2=(int)(60*(Math.sin(ang)));
  90.         objeto.fillOval(((puntoCentro.x)+(x2))-2,((puntoCentro.y)-(y2))-2,5,5);
  91.         objeto.drawLine(puntoCentro.x,puntoCentro.y,(puntoCentro.x)+(x2),(puntoCentro.y)-(y2));
  92.        
  93.         String h="",m="",s="";
  94.         if(hora<10)
  95.         {
  96.                 h="0";
  97.         }
  98.         if(minutos<10)
  99.         {
  100.                 m="0";
  101.         }
  102.         if(segundos<10)
  103.         {
  104.                 s="0";
  105.         }
  106.         objeto.setColor(Color.WHITE);
  107.         objeto.fillRect(puntoCentro.x-20,(puntoCentro.y)+(puntoCentro.y),80,30);
  108.         objeto.setColor(Color.BLUE);
  109.         objeto.drawString(h+hora+":"+m+minutos+":"+s+segundos,puntoCentro.x-20,(puntoCentro.y)+(puntoCentro.y)+20);
  110.     }
  111.    
  112.     public double AnguloMin(int min)
  113.     {
  114.         double minuto;
  115.         minuto=((90-(min*6.0))*(Math.PI/180));
  116.         return minuto;
  117.     }
  118.    
  119.     public double AnguloHora(int hor,int min)
  120.     {
  121.         double hora;
  122.         hora=(((90-(hor+(min/60.0))*30.0))*((Math.PI)/180));
  123.         return hora;
  124.     }
  125.    
  126.    
  127.     public static void main(String[] args)
  128.     {
  129.         Reloj1 reloj= new Reloj1(100,100,300,300);
  130.         reloj.setVisible(true);
  131.     }
  132.    
  133. }
  134.