Sie sind auf Seite 1von 11

package hilo; import java.awt.Component; import javax.swing.

JFrame; public class RelojAlarma { public static void main(String[] args) { //toda la implementacin en la clase panel aqui se crea el frame y poco mas final JFrame app=new JFrame(); Reloj1 pa=new Reloj1(); app.setTitle("Alarma "); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.add(pa); app.setVisible(true); javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); }

private void createAndShowGUI() { Reloj1 reloj = new Reloj1(); Component add = app.getContentPane().add(reloj); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setSize(600, 302); app.setVisible(true); throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }); } }

package hilo; import java.applet.AudioClip; import java.awt.Image; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import javax.swing.JOptionPane; import java.util.TimerTask; import java.util.Timer; public class Reloj1 extends JPanel implements ActionListener{ private JLabel horaLabel; private JButton botonActivar; private JButton botonAnular; private JLabel ponerAlarmaLabel; private JLabel h; private JLabel m; private JLabel s; public JTextField horaActual; public JTextField horaAlarma; public JTextField minAlarma; public JTextField segAlarma; public int minutoAlarma; public int segundoAlarma;

public int horasAlarma; private Reloj rel; private Timer tm; private Timer tm2; public boolean activo; public boolean snooze; public boolean haSonado=false; private static final int TIMER_UPDATE = 1000; private Image img; private AudioClip sonido; private Calendar calendar = null; public Reloj1(){ setUI(new RelojUI()); calendar = Calendar.getInstance(); javax.swing.Timer timer = new javax.swing.Timer(TIMER_UPDATE, new ActionListener() { public void actionPerformed(ActionEvent e) calendar.add(Calendar.SECOND, 1); repaint(); timer.start(); setLayout(null); horaLabel=new JLabel("HORA ACTUAL: "); horaLabel.setBounds(250,2,180,40); this.add(horaLabel); horaActual=new JTextField(); } });

horaActual.setBounds(250,30,150,20); this.add(horaActual); ponerAlarmaLabel=new JLabel("PROPORCIONE LA HORA DE SU ALARMA:"); ponerAlarmaLabel.setBounds(250,50,220,40); this.add(ponerAlarmaLabel); horaAlarma=new JTextField(); horaAlarma.setBounds(250,100,50,40); String gh=""+Calendar.getInstance().get(Calendar.HOUR_OF_DAY); horaAlarma.setText(gh); this.add(horaAlarma); h=new JLabel("HORA"); h.setBounds(250,150,50,10); this.add(h); minAlarma=new JTextField(); minAlarma.setBounds(330,100,50,40); gh=""+Calendar.getInstance().get(Calendar.MINUTE); minAlarma.setText(gh); this.add(minAlarma); m=new JLabel("MINUTO"); m.setBounds(330,150,50,10); this.add(m); segAlarma=new JTextField(); segAlarma.setBounds(410,100,50,40); gh=""+Calendar.getInstance().get(Calendar.SECOND);

segAlarma.setText(gh); this.add(segAlarma); s=new JLabel("SEGUNDO"); s.setBounds(410,150,70,10); this.add(s); botonActivar=new JButton("GUARDAR"); botonActivar.setBounds(250,200,100,40); botonActivar.addActionListener(this); this.add(botonActivar); botonAnular=new JButton("PARAR"); botonAnular.setBounds(350,200,100,40); botonAnular.addActionListener(this); this.add(botonAnular); rel = new Reloj(); tm=new Timer(); tm.schedule(rel,0,1000);//actualiza la hora y comprueba la alarma } public void actionPerformed(ActionEvent e){ sonido= java.applet.Applet.newAudioClip(getClass().getResource("/Hilo/snd8.wav")); if(e.getSource()==botonActivar){ System.out.println("GUARDAR"); activo=true; horaAlarma.setEnabled(false); minAlarma.setEnabled(false); segAlarma.setEnabled(false); @Override

try{ if(!horaAlarma.getText().contentEquals(null) || !minAlarma.getText().contentEquals(null) || !segAlarma.getText().contentEquals(null)){ JOptionPane.showMessageDialog(null, "ALARMA GUARDADA!!"); }catch(java.lang.NullPointerException o){ System.out.println("Valor nulo en campo de alarma"); else{ if(e.getSource()==botonAnular){ JOptionPane.showMessageDialog(null,"alrma desactivada"); System.out.println("PARAR");sonido.stop(); activo=false; snooze=false; horaAlarma.setEnabled(true); minAlarma.setEnabled(true); segAlarma.setEnabled(true); public class Reloj extends TimerTask{ @Override public void run(){ String s; sonido= java.applet.Applet.newAudioClip(getClass().getResource("/Hilo/snd8.wav")); Calendar cal=Calendar.getInstance(); int hora =cal.get(Calendar.HOUR_OF_DAY); } } } } } }

int min = cal.get(Calendar.MINUTE); int seg = cal.get(Calendar.SECOND); s=hora+":"+min+":"+seg; horaActual.setText(s); horaActual.setEnabled(false); if(!activo){ botonActivar.setEnabled(true); botonAnular.setEnabled(false); sonido.stop(); if(activo){ } try{ horasAlarma=Integer.parseInt(horaAlarma.getText());

minutoAlarma=Integer.parseInt(minAlarma.getText()); segundoAlarma=Integer.parseInt(segAlarma.getText()); }catch(Exception e){ JOptionPane.showMessageDialog(null,"Proporciona numeros antes de activarla!"); activo=false; horaAlarma.setEnabled(true); minAlarma.setEnabled(true); segAlarma.setEnabled(true); botonActivar.setEnabled(false); botonAnular.setEnabled(true); if(hora==horasAlarma && min==minutoAlarma && seg == segundoAlarma){ }

haSonado=true;

System.out.println("Alarm"); try{ sonido.play(); sonido.loop(); } catch (Exception e) { System.out.println("" + e); } public Calendar getCalendar() { return this.calendar; } } } } } } }

package hilo; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.net.URL; import java.util.Calendar; import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.PanelUI; public class RelojUI extends PanelUI { private BasicStroke handsStroke;

public static ComponentUI createUI(JComponent c) { return new RelojUI(); } public void installUI(JComponent c) { super.installUI(c); c.setOpaque(false); handsStroke = new BasicStroke(2f); } public void paint(Graphics g, JComponent c) { g.setColor(Color.magenta); g.fillRect(0, 0, 600, 320); g.setColor(Color.cyan); g.fillOval(48,78, 167, 164); g.setColor(Color.BLUE); Graphics2D g2d = (Graphics2D) g; Reloj1 hilo = (Reloj1) c; int hour = hilo.getCalendar().get(Calendar.HOUR); int minute = hilo.getCalendar().get(Calendar.MINUTE); int second = hilo.getCalendar().get(Calendar.SECOND); Shape oval = new Ellipse2D.Double(0, 0, 167, 164); int centerX = (int) (oval.getBounds().getWidth() / 2); int centerY = (int) (oval.getBounds().getHeight() / 2); int xh, yh, xm, ym, xs, ys; xs = (int) (Math.cos(second * Math.PI / 30 - Math.PI / 2) * 75 + centerX); ys = (int) (Math.sin(second * Math.PI / 30 - Math.PI / 2) * 75 + centerY); xm = (int) (Math.cos(minute * Math.PI / 30 - Math.PI / 2) * 60 + centerX);

ym = (int) (Math.sin(minute * Math.PI / 30 - Math.PI / 2) * 60 + centerY); xh = (int) (Math.cos((hour * 30 + minute / 2) * Math.PI / 180 - Math.PI / 2) * 45 + centerX); yh = (int) (Math.sin((hour * 30 + minute / 2) * Math.PI / 180 - Math.PI / 2) * 45 + centerY); g.translate(48, 78); g.drawString("9", 10, centerY+5); g.drawString("3", (int) (oval.getBounds().getWidth() - 10), centerY+5); g.drawString("12", centerX-6, 20); (oval.getBounds().getHeight() - 10)); g.drawString("6", centerX-3, (int)

g.drawLine(centerX, centerY, xs, ys); g2d.setStroke(handsStroke); g.drawLine(centerX, centerY, xm, ym); g.drawLine(centerX, centerY, xh, yh); g.translate(-47, -78); }

Das könnte Ihnen auch gefallen