This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cronometro; | |
public class Logica implements Runnable{ | |
private Integer msec, secs, mins, hrs; | |
private Vista vis; | |
public Logica(Vista vis){ | |
this.vis=vis; | |
msec=0; | |
secs=0; | |
mins=0; | |
hrs=0; | |
} | |
public void run(){ | |
while(true){ | |
int wea=vis.state; | |
int cos=vis.accion; | |
if(cos==1){ | |
msec=0; | |
secs=0; | |
mins=0; | |
hrs=0; | |
vis.hr.setText("00"); | |
vis.min.setText("00"); | |
vis.sec.setText("00"); | |
vis.msec.setText("00"); | |
vis.accion=0; | |
} | |
if(wea==1){ | |
try{ | |
Thread.sleep(10); | |
} | |
catch(InterruptedException e){ | |
} | |
msec++; | |
if(msec==100){ | |
msec=0; | |
secs++; | |
} | |
if(secs==60){ | |
secs=0; | |
mins++; | |
if(vis.dot1.isVisible()) | |
vis.dot1.setVisible(false); | |
else | |
vis.dot1.setVisible(true); | |
} | |
if(mins==60){ | |
mins=0; | |
hrs++; | |
} | |
if(msec<10) | |
vis.msec.setText("0"+msec.toString()); | |
else | |
vis.msec.setText(msec.toString()); | |
if(secs<10) | |
vis.sec.setText("0"+secs.toString()); | |
else | |
vis.sec.setText(secs.toString()); | |
if(mins<10) | |
vis.min.setText("0"+mins.toString()); | |
else | |
vis.min.setText(mins.toString()); | |
if(hrs<10) | |
vis.hr.setText("0"+hrs.toString()); | |
else | |
vis.hr.setText(hrs.toString()); | |
} | |
} | |
} | |
} |
Y esta es la interfaz gráfica, que por desgracia interactua demasiado con la clase lógica.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cronometro; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class Vista extends JFrame implements ActionListener{ | |
JPanel p1, p2, p3; | |
JLabel hr, min, sec, msec, dot1, dot2; | |
JButton start, restart, stop, resume; | |
Integer state=0, accion=0; | |
public Vista(){ | |
super("Cronometro"); | |
this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
this.setBounds(50, 50, 480, 300); | |
p1=new JPanel(new GridLayout(1,5)); | |
p3=new JPanel(); | |
hr=new JLabel("00"); min=new JLabel("00"); sec=new JLabel("00"); msec=new JLabel("00"); | |
dot1=new JLabel(":"); dot2=new JLabel(":"); | |
hr.setFont(new Font("Arial", Font.BOLD, 70)); | |
min.setFont(new Font("Arial", Font.BOLD, 70)); | |
sec.setFont(new Font("Arial", Font.BOLD, 70)); | |
dot1.setFont(new Font("Arial", Font.BOLD, 70)); | |
dot1.setBounds(140,60,100,100); | |
dot2.setFont(new Font("Arial", Font.BOLD, 70)); | |
dot2.setBounds(280,60,100,100); | |
p1.add(hr); | |
add(dot1); | |
p1.add(min); | |
add(dot2); | |
p1.add(sec); | |
msec.setBounds(420, 85,100,100); | |
add(msec); | |
this.add(p1, BorderLayout.CENTER); | |
p3.setPreferredSize(new Dimension(44,44)); | |
this.add(p3, BorderLayout.WEST); | |
p2=new JPanel(new FlowLayout()); | |
start=new JButton("Iniciar"); start.setPreferredSize(new Dimension(100,20)); | |
start.addActionListener(this); | |
restart=new JButton("Reiniciar"); restart.setPreferredSize(new Dimension(100, 20)); | |
restart.addActionListener(this); | |
stop=new JButton("Detener"); stop.setPreferredSize(new Dimension(100, 20)); | |
stop.addActionListener(this); | |
resume=new JButton("Continuar"); resume.setPreferredSize(new Dimension(100, 20)); | |
resume.addActionListener(this); | |
p2.add(start); p2.add(restart); | |
p2.add(stop); p2.add(resume); | |
this.add(p2, BorderLayout.SOUTH); | |
start.setVisible(true); | |
stop.setVisible(false); | |
restart.setVisible(false); | |
resume.setVisible(false); | |
this.setVisible(true); | |
} | |
public void actionPerformed(ActionEvent e) { | |
if(e.getSource() == start){ | |
state=1; | |
start.setVisible(false); | |
stop.setVisible(true); | |
restart.setVisible(true); | |
} | |
if(e.getSource() == stop){ | |
state=0; | |
stop.setVisible(false); | |
resume.setVisible(true); | |
} | |
if(e.getSource() == restart){ | |
accion=1; | |
} | |
if(e.getSource() == resume){ | |
state=1; | |
resume.setVisible(false); | |
stop.setVisible(true); | |
} | |
} | |
} |
No hay comentarios:
Publicar un comentario