You are on page 1of 15

Link Log

Una manera de perder el tiempo

Posts Tagged Java Reloj en Java


with 7 comments Bueno, despus de mucho mucho tiempo, aqu traigo de nuevo otro programa en Java, este programa solo es un simple reloj analgico (ya saben, esos que tienen manecillas). Bueno, ac el reloj resultante:

Reloj en Java Ac el cdigo: view source print?


001 import java.awt.*; 002 import java.awt.event.*; 003 import java.awt.image.*;

004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051

import javax.swing.*; import javax.swing.event.*; import java.util.*; public class RelojProyecto extends JFrame{ public static void main(String[] args) { RelojProyecto app = new RelojProyecto(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } Reloj Cara; public RelojProyecto() { super( "Reloj Proyecto" ); setVisible( true ); setResizable( false ); Container content = this.getContentPane(); content.setLayout(new BorderLayout()); Cara = new Reloj(); content.add(Cara, BorderLayout.CENTER); this.pack(); Cara.start(); } } class Reloj extends JPanel { private int horas; private int minutos; private int segundos; private private private private private private private private private static static static static final final final final int espacio = 10; float dosPi = (float)(2.0 * Math.PI); float tresPi = (float)(3.0 * Math.PI); float rad = (float)(Math.PI / 30.0);

int tamano; int xCentro; int yCentro; BufferedImage muestra; javax.swing.Timer t;

public Reloj() { this.setPreferredSize(new Dimension(300,300)); t = new javax.swing.Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) {

052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069

update(); } }); } public void update() { this.repaint(); }

public void start() { t.start(); } public void stop() { t.stop(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 070 RenderingHints.VALUE_ANTIALIAS_ON); 071 072 int ancho = getWidth(); 073 int alto = getHeight(); 074 tamano = ((ancho < alto) ? ancho : alto) - 2*espacio; 075 xCentro = tamano/2 + espacio; 076 yCentro = tamano/2 + espacio; 077 078 if (muestra == null 079 || muestra.getWidth() != ancho 080 || muestra.getHeight() != alto) { 081 082 muestra = (BufferedImage)(this.createImage(ancho, alto)); 083 Graphics2D gc = muestra.createGraphics(); 084 gc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 085 RenderingHints.VALUE_ANTIALIAS_ON); 086 caraReloj(gc); 087 } 088 089 Calendar now = Calendar.getInstance(); 090 horas = now.get(Calendar.HOUR); 091 minutos = now.get(Calendar.MINUTE); 092 segundos = now.get(Calendar.SECOND); 093 094 g2.drawImage(muestra, null, 0, 0); 095 096 Manecillas(g); 097 } 098

099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

private void caraReloj(Graphics g) { g.setColor(new Color(209, 234, 255)); g.fillOval(espacio, espacio, tamano, tamano); g.setColor(Color.black); g.drawOval(espacio, espacio, tamano, tamano); for (int seg = 0; seg<60; seg++) { int inicio; if (seg%5 == 0) { inicio = tamano/2-10; } else { inicio = tamano/2-5; } diseno(g, xCentro, yCentro, rad*seg, inicio , tamano/2); } } private void Manecillas(Graphics g) { int radioSegundero = tamano/2; int radioMinutero = radioSegundero * 3/4; int radioHora = radioSegundero/2;

float fsegundos = segundos; float anguloSegundero = tresPi - (rad * fsegundos); diseno(g, xCentro, yCentro, anguloSegundero, 0, 124 radioSegundero); 125 126 float fminutos = (float)(minutos + fsegundos/60.0); 127 float anguloMinutero = tresPi - (rad * fminutos); 128 diseno(g, xCentro, yCentro, anguloMinutero, 0, radioMinutero); 129 130 float fhours = (float)(horas + fminutos/60.0); 131 float anguloHora = tresPi - (5 * rad * fhours); 132 diseno(g, xCentro, yCentro, anguloHora, 0, radioHora); 133 134 Font font = new Font("Arial", Font.BOLD, 16); 135 g.setFont(font); 136 g.drawString( "12", 140, 40 ); 137 g.drawString( "1", 205, 55 ); 138 g.drawString( "2", 245, 100 ); 139 g.drawString( "3", 265, 155 ); 140 g.drawString( "4", 245, 210 ); 141 g.drawString( "5", 205, 255 ); 142 g.drawString( "6", 145, 270 ); 143 g.drawString( "7", 90, 255 ); 144 g.drawString( "8", 45, 210 ); 145 g.drawString( "9", 25, 155 );

146 147 148 149 150 151 152 153 154

g.drawString( "10", 45, 100 ); g.drawString( "11", 80, 55 ); Font font1 = new Font("Arial", Font.BOLD, 12); g.setFont(font1); g.drawString( "RELOJ", 130, 80 ); g.drawString( "QUARTZ", 125, 220 ); }

private void diseno(Graphics g, int x, int y, double angulo, int minRadius, int maxRadius) { 155 float sine = (float)Math.sin(angulo); 156 float cosine = (float)Math.cos(angulo); 157 158 int dxmin = (int)(minRadius * sine); 159 int dymin = (int)(minRadius * cosine); 160 161 int dxmax = (int)(maxRadius * sine); 162 int dymax = (int)(maxRadius * cosine); 163 g.drawLine( x+dxmin, y+dymin, x+dxmax, y+dymax); 164 } 165 }

Written by Link X May 18, 2009 at 9:09 pm Posted in Java Tagged with Java, Programacin

Cdigo
with one comment Para las personas que llegan aqu buscando cdigo de algn tema en especifico (sobretodo cdigo de Java), les informo que el cdigo publicado en este blog funciona, solo que si lo copian directamente del navegador al editor puede tener problemas al copiarse, sobretodo con las comillas( ya que se copian como ), por lo que es recomendable escribir el cdigo directamente al editor y despus compilarlo, as no habr problemas. No creo que haya problemas, despus de todo ya esta todo resuelto y solo es necesario que lo copien. Aunque si hay algn problema o duda me lo pueden decir. Written by Link X

November 27, 2008 at 1:14 am Posted in General Tagged with codigo, Copiar, Java, Programacin

Java AWT
without comments Aqu esta un ejemplo muy pero muy sencillo de grficos en Java. El programa en si no hace nada, solo se muestra lo que se necesita para crear una ventana con botones (que no hacen nada). view source print?
01 import java.awt.*; 02 public class ejemplo{ 03 public static void main( String args[] ){ 04 Button boton = new Button( "Soy un botn y el de abajo no hace 05 nada" ); 06 Button boton2 = new Button( "El botn de arriba no hace nada" ); 07 08 Frame vent = new Frame( "Ejemplo Java AWT" ); 09 vent.setLayout(new FlowLayout()); 10 vent.add( boton ); 11 vent.add( boton2 ); 12 vent.setSize( 300, 150 ); 13 vent.setVisible( true ); 14 15 vent.addWindowListener(new Conclusion()); 16 } 17 }

Creo que todo el cdigo se entiende, lo nico que se puede llegar a no entender es la ultima linea (vent.addWindowsListener), esta es una linea muy importante, ya que sin ella no cierra la ventana. Written by Link X June 18, 2008 at 11:13 pm Posted in Java

Tagged with awt, graficos, Java, Programacin

Colas en Java
with 10 comments Ya puse unos ejemplos de listas y pilas, pero todava hace falta el ejemplo de colas (no de esas, pero no estara mal poner unos buenos ejemplos). Bueno, lo que hace este programa, es mostrar un men para que el usuario seleccione lo que desee hacer, si insertar, retirar o mostrar la cola (de Java). view source print?
01 import java.util.*; 02 public class Cola { 03 public static void main( String args[] ){ 04 Scanner leer = new Scanner(System.in); 05 06 colagenerica obj = new colagenerica(); 07 08 int op; 09 int num; 10 11 do{ 12 menu(); 13 op = leer.nextInt(); 14 15 switch(op){ 16 case 1: 17 System.out.println( "Numero a insertar" ); 18 num = leer.nextInt(); 19 if(obj.inscola(num)){ System.out.println( "fre"+obj.fre+"fin"+obj.fin 20 +"aux"+obj.max ); System.out.println( "El numero "+num+" se 21 inserto en la cola ["+obj.dret+"]" ); 22 System.out.println(); 23 } 24 else{ 25 System.out.println( "Cola llena" ); 26 } 27 break; 28 case 2: 29 if(obj.retcola()){ System.out.println( "El dato retirado fue: 30 "+obj.dret );

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 }

} else{ System.out.println( "Cola vacia" ); } break; case 3: if(obj.fre==-1 && obj.fin==-1){ System.out.println( "Cola vacia" ); } else{ System.out.println( "Estado de la cola:" ); for(int i=obj.fre; i<=obj.fin; i++){ System.out.print(obj.c[i]+" \t"); } break; } } } while(op != 4); } public static void menu(){ System.out.println( "\t Menu para colas \n" ); System.out.println( "1.- Insertar" ); System.out.println( "2.- Retirar" ); System.out.println( "3.- Estado" ); System.out.println( "4.- Fin" ); System.out.println( "\n Selecciona" ); }

El programa usa mtodos de la clase colagenerica, que puedes ver haciendo clic en mas Read the rest of this entry Written by Link X June 5, 2008 at 7:19 pm Posted in Java Tagged with colas, Java, Programacin

Listas en Java (LinkedList)


with 2 comments

De nuevo las listas, pero esta va vez con LinkedList, lo que permite que se agreguen o eliminen elementos en la lista al inicio o al final. view source print?
01 import java.util.*; 02 public class ListaLigada { 03 04 public static void main (String args[]) { 05 Scanner leer = new Scanner(System.in); 06 07 int num; 08 int op; 09 10 LinkedList lista = new LinkedList(); 11 do{ 12 System.out.println( "\t Men \t" ); 13 System.out.println( "Operaciones con listas" ); 14 System.out.println( "1.- Insertar al principio" ); 15 System.out.println( "2.- Insertar al final" ); 16 System.out.println( "3.- Borrar al principio" ); 17 System.out.println( "4.- Borrar al final" ); 18 System.out.println( "5.- Mostrar la lista" ); 19 System.out.println( "6.- Borrar toda la lista" ); 20 System.out.println( "7.- Salir" ); 21 System.out.println( "\n" ); 22 System.out.println( "Elija la operacin que desee" ); 23 24 op = leer.nextInt(); 25 26 switch(op){ 27 case 1: 28 System.out.println( "Inserte numero" ); 29 num = leer.nextInt(); 30 lista.addFirst(num); 31 break; 32 case 2: 33 System.out.println( "Inserte numero" ); 34 num = leer.nextInt(); 35 lista.addLast(num); 36 break; 37 case 3: System.out.println( "Se borrara el primer nodo" 38 ); 39 lista.removeFirst(); 40 break; 41 case 4: 42 System.out.println( "Se borrara el nodo final" );

43 44 45 46 47 48 49 50 51 52 53

lista.removeLast(); break; case 5: System.out.println( "La lista es la siguiente" ); List lista2 = new ArrayList(lista); Iterator it = lista2.iterator(); while (it.hasNext()){ System.out.println(it.next()+""); } break; case 6: System.out.println( "Se borraran todos los 54 elementos de la lista" ); 55 lista.clear(); 56 break; 57 case 7: 58 System.out.println( "Al rato" ); 59 break; 60 } 61 } 62 63 while( op != 7 ); 64 } 65 }

Written by Link X June 1, 2008 at 10:08 pm Posted in Java Tagged with Java, LinkedList, listas, Programacin

Listas en Java
with one comment Bueno, este programa hace lo mismo que el anterior, solo que esta hecho de la manera tradicional, es decir, sin usar ArrayList o LinkedList. Se podra decir que la lista esta hecha de forma manual. Clase Nodo view source print?
1 public class NLista1{ 2 String nom;

3 4 5 6 7 8 9}

int calif1; int calif2; int calif3; double prom; NLista1 liga;

Programa view source print?


01 import java.util.*; 02 public class Lista1 { 03 04 public static void main (String args[]) { 05 Scanner leer = new Scanner(System.in); 06 07 NLista1 inicio = null; 08 NLista1 fin = null; 09 NLista1 nuevo = null; 10 NLista1 aux = null; 11 12 int op; 13 14 do{ 15 nuevo = new NLista1(); 16 System.out.println( "Ingrese el nombre del estudiante" ); 17 nuevo.nom = leer.next(); 18 System.out.println( "Ingrese la primer calificacin" ); 19 nuevo.calif1 = leer.nextInt(); 20 System.out.println( "Ingrese la segunda calificacin" ); 21 nuevo.calif2 = leer.nextInt(); 22 System.out.println( "Ingrese la tercer calificacin" ); 23 nuevo.calif3 = leer.nextInt(); 24 25 if(inicio == null){ 26 inicio = nuevo; 27 nuevo.liga = null; 28 } 29 else{ 30 fin.liga = nuevo; 31 nuevo.liga = null; 32 } 33 34 fin = nuevo; 35 Promedio(nuevo);

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 }

System.out.println( "Desea ingresar otro estudiante?" ); System.out.println( "1.-Si \t 2.-No" ); op = leer.nextInt(); } while(op != 2); aux = inicio; while(aux != null){ System.out.println( "Nombre \t Promedio" ); System.out.println( aux.nom + "\t" + aux.prom ); aux = aux.liga; } } public static double Promedio(NLista1 nuevo){ int suma = nuevo.calif1 + nuevo.calif2 + nuevo.calif3; nuevo.prom = suma/3; return nuevo.prom; }

Written by Link X May 31, 2008 at 2:01 am Posted in Java Tagged with Java, listas, Programacin

Listas en Java (ArrayList)


with 19 comments Aqu esta un ejemplo de listas en Java utilizando ArrayList: Lo que hace este programa, es que pide el nombre del alumno y tres calificaciones para luego calcular su promedio. Se puede agregar cualquier cantidad de elementos a la lista. view source print?

1 public class NodoLista4{ 2 String nom; 3 int calif1; 4 int calif2; 5 int calif3; 6}

view source print?


01 import java.util.*; 02 public class ListaAlumnos{ 03 04 static double prom; 05 public static void main( String args[] ){ 06 Scanner leer = new Scanner(System.in); 07 08 NodoLista4 nodo = new NodoLista4(); 09 int op; 10 11 ArrayList lista = new ArrayList(); 12 do{ 13 System.out.println( "Ingrese el nombre del alumno:" ); 14 nodo.nom = leer.next(); 15 System.out.println( "Ingrese la primera calificacin:" ); 16 nodo.calif1 = leer.nextInt(); 17 System.out.println( "Ingrese la segunda calificacin:" ); 18 nodo.calif2 = leer.nextInt(); 19 System.out.println( "Ingrese la tercera calificacin:" ); 20 nodo.calif3 = leer.nextInt(); 21 22 lista.add("Nombre del alumno:\n"+nodo.nom); 23 lista.add("Calificacin 1:\n"+nodo.calif1); 24 lista.add("Calificacin 2:\n"+nodo.calif2); 25 lista.add("Calificacin 3\n"+nodo.calif3); 26 27 promedio(nodo.calif1, nodo.calif2, nodo.calif3); 28 29 lista.add("Su promedio es:\n"+prom); 30 31 System.out.println( "Desea ingresar otro alumno?" ); 32 System.out.println( "1.-Si\t 2.-No" ); 33 op = leer.nextInt(); 34 } 35 while(op != 2); 36 List lista2 = new ArrayList(lista); 37 Iterator it = lista2.iterator(); 38 while (it.hasNext()){ 39 System.out.println(it.next()+"");

40 41 42 43 44 45 46 47 48 }

} } private static double promedio(int calif1, int calif2, int calif3){ int suma = calif1 + calif2 + calif3; prom = suma/3; return prom; }

P.D.: No es necesario hacer el NodoLista4, esas variables se pueden crear en el programa, pero me acostumbre a hacer eso Written by Link X May 27, 2008 at 12:28 pm Posted in Java Tagged with arraylist, Java, listas, Programacin

Java Infijo a Posfijo


with 2 comments Bueno, ya conociendo el algoritmo, se puede hacer en Java: view source print?
01 class converpostultima{ 02 public static void main (String args[]) 03 { 04 05 String expr = new String(""); 06 String exprpost = new String(""); 07 char ch; 08 int max; 09 System.out.print("Dame la Expresion en Infijo: "); 10 expr =Leer.dato(); 11 max=expr.length(); 12 operapilaschar obj1 = new operapilaschar(max); 13 System.out.println(); 14 System.out.println(); 15 System.out.println("La Expresion en Postfijo es :"); 16 obj1.push('('); // inserta '(' a la PILA 17 expr+=')'; // inserta ')' al final de Q

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

for (int i=0;i=precedencia(ch) &amp;&amp; obj1.pila[obj1.tope]! ='(')) { obj1.pop(); exprpost+=obj1.dret; } obj1.push(ch); break; case ')': while (obj1.pila[obj1.tope] != '(') { obj1.pop(); exprpost+=obj1.dret; } obj1.pop(); break; default : exprpost+=ch; } } while (!(obj1.pila_Vacia(obj1.tope))) { obj1.pop(); if (obj1.dret!= '(') exprpost+=obj1.dret; } System.out.println(exprpost); } public static int precedencia(char ch) { int aux = 0; switch (ch) { case '^' : aux = 4; break; case '*' : case '/' : aux = 3; break; case '+' : case '-' : aux = 2; break; case '(' : aux = 1; break; } return aux; } }

You might also like