Java Countdowntimer

Only available on StudyMode
  • Topic: Class
  • Pages : 7 (671 words )
  • Download(s) : 45
  • Published : March 3, 2013
Open Document
Text Preview
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;

public class Clock extends JPanel
{
int m=60,s=0;
Graphics2D hsec,hmin;
int []xpos={125,135,145,155,165 ,175,190,205,220,225 ,225,225,225,225,225 ,225,225,225,225,225 ,225,225,220,205,190 ,175,165,155,145,135 ,125,115,105,95,85 ,75,60,45,30,25 ,25,25,25,25,25 ,25,25,25,25,25 ,25,25,30,45,60 ,75,85,95,105,115 ,125};

int []ypos={50,50,50,50,50 ,50,50,50,50,60 ,70,90,105,120,135 ,150,165,180,195,210 ,225,240,250,250,250 ,250,250,250,250,250 ,250,250,250,250,250 ,250,250,250,250,240 ,225,210,195,180,165 ,150,135,120,105,90 ,75,60,50,50,50 ,50,50,50,50,50 ,50};

javax.swing.Timer cdown;
boolean a=false,b=false;
int bit=999;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(25,50,225,225);
g.fillRect(260,50,90,20);

g.setColor(Color.WHITE);
g.drawString(Integer.toBinaryString(s),260,60);
g.fillOval(25,50,200,200);
g.drawString(m+" : "+s,210,230);
g.drawString(xpos[m]+""+ypos[m]+""+xpos[s]+""+ypos[s],170,250);
g.setColor(Color.BLACK);
g.fillOval(50,75,150,150);
g.setColor(Color.WHITE);
g.fillOval(120,145,10,10);

g.setColor(Color.YELLOW);
g.drawLine(125,150,xpos[m],ypos[m]);
hmin=(Graphics2D)g;

g.setColor(Color.RED);
g.drawLine(125,150,xpos[s],ypos[s]);
hsec=(Graphics2D)g;

cdown=new javax.swing.Timer(bit,new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
hsec.drawLine(125,150,xpos[s],ypos[s]);
hmin.drawLine(125,150,xpos[m],ypos[m]);
repaint();
System.out.print(Integer.toBinaryString(s));
if(a==true)
cdown.stop();
else{
if(m>=0)
{
if(s==0)
{
if(m==0)
{
s=0;
cdown.stop();
btnstart.setEnabled(false);
btnpause.setEnabled(false);
btnstop.setEnabled(false);
btnreset.setEnabled(true);
txtmin.setEnabled(false);
}
else
{
m--;
s=59;

}
}
else
s--;
}
}

}
});
}
JButton btnstart,btnpause,btnstop,btnreset;
JTextField txtmin=new JTextField("60",10);
JPanel pnlbutton=new JPanel();
public Clock()
{
btnstart=new JButton("Start");
btnpause=new JButton("Pause");
btnstop=new JButton("Stop");
btnreset=new JButton("Reset");

btnstart.setEnabled(true);
btnpause.setEnabled(false);
btnreset.setEnabled(false);
btnstop.setEnabled(false);

btnstart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(a==false||b==true)
{
try{
m=Integer.parseInt(txtmin.getText());
if(m60)
{
JOptionPane.showMessageDialog(null,"Minimum is 1 Minute and Maximum is 60 Minutes.");
txtmin.setText("60");
}
else if(b==true)
{
m-=1;
s=59;
a=false;
btnstart.setEnabled(false);
btnpause.setEnabled(true);
btnstop.setEnabled(true);
btnreset.setEnabled(false);
txtmin.setEnabled(false);
}
else
{
m-=1;
s=59;
cdown.start();
btnstart.setEnabled(false);
btnpause.setEnabled(true);
btnstop.setEnabled(true);
btnreset.setEnabled(false);
txtmin.setEnabled(false);
}
}
catch(NumberFormatException error)
{
JOptionPane.showMessageDialog(null,"Youre input must be an Integer Value.");
txtmin.setText("60");
}

}
else
{
a=false;
btnstart.setEnabled(false);
btnpause.setEnabled(true);
btnstop.setEnabled(true);
btnreset.setEnabled(false);
txtmin.setEnabled(false);...
tracking img