hey guys,
i have a simple code that simply asks the user to input a number a number and than where you press you mouse it draws a circle you can draw as many circles as you have specified in the beginning of the app. well there is also a time running in the bottom that is what i am after. i have the code and im not able to extract that timer all i need is have a simple class that runs just the time lets say a count down from 30 seconds i hope somebody could help me and show me what bits are unnecessary for me.
here is the code
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
public class CirclesWithTimer2005
{
public static void main( String args[ ] )
{
MainWindow mainWnd = new MainWindow() ;
}
}
class MainWindow extends Frame
{
public final static int ONE_SECOND = 1000 ;
private boolean m_bMouse ;
private boolean m_bStart ;
private boolean m_bPing ;
private int m_nWndWidth ;
private int m_nWndHeight ;
private int m_nCirclesToDraw ;
private int m_nCirclesDrawn ;
private int m_nSeconds ;
private Timer m_timer ;
private Font m_WndFont ;
private Font m_timeFont ;
private Rectangle[ ] m_rectCircleArray ;
MainWindow()
{
setTitle( "Circles with a Timer" ) ;
m_bMouse = false ;
m_bStart = true ;
m_bPing = false ;
m_nCirclesToDraw = 0 ;
m_nCirclesDrawn = 0 ;
m_nSeconds = 0 ;
m_WndFont = new Font( "Helvetica", Font . PLAIN, 20 ) ;
m_timeFont = new Font( "Courier", Font . ITALIC, 70 ) ;
m_rectCircleArray = new Rectangle[ 9 ] ;
setBackground( Color.pink ) ;
Toolkit thisScreen = Toolkit . getDefaultToolkit() ;
Dimension thisScreenSize = thisScreen . getScreenSize() ;
m_nWndWidth = 2 * thisScreenSize . width / 3 ;
m_nWndHeight = 2 * thisScreenSize . height / 3 ;
setSize( m_nWndWidth, m_nWndHeight ) ;
addKeyListener( new KeyCatcher() ) ;
addMouseListener( new MouseCatcher() ) ;
addWindowListener( new WindowCatcher() ) ;
setVisible( true ) ;
}
public void paint( Graphics gc )
{
Dimension winSize = getSize() ;
if ( m_bStart )
{
m_timer = new Timer( ONE_SECOND, new TimerCatcher() ) ;
m_timer . start() ;
m_bStart = false ;
}
gc . setFont( m_WndFont ) ;
if ( m_bPing )
{
gc.setFont( m_timeFont ) ;
gc . drawString( "" + m_nSeconds, 6*winSize.width/8, 7*winSize.height/8 ) ;
m_bPing = false ;
}
if ( m_nCirclesToDraw == 0 )
{
gc . setFont( m_WndFont ) ;
gc . drawString( "How many circles do you want to draw?: ", 20, 50 ) ;
gc . drawLine( 0, 60, winSize.width, 60 ) ;
requestFocus() ;
}
else
{
gc . setFont( m_WndFont ) ;
gc . drawString( "Number of circles to draw: " + m_nCirclesToDraw, 20, 50 ) ;
gc . drawLine( 0, 60, winSize.width, 60 ) ;
}
gc . setFont( m_timeFont ) ;
if ( m_bMouse )
{
gc . setColor( Color . blue ) ;
for ( int i = 0; i < m_nCirclesDrawn; i++ )
{
gc . fillOval( m_rectCircleArray[i].x, m_rectCircleArray[i].y,
m_rectCircleArray[i].width, m_rectCircleArray[i].height ) ;
}
}
}
class KeyCatcher extends KeyAdapter
{
public void keyPressed( KeyEvent evt )
{
int temp = evt . getKeyCode() ;
if ( m_nCirclesToDraw == 0 )
{
if (( temp >= KeyEvent . VK_0 ) && ( temp <= KeyEvent . VK_9 ))
{
m_nCirclesToDraw = temp - KeyEvent . VK_0 ;
repaint() ;
}
}
}
}
class MouseCatcher extends MouseAdapter
{
public void mousePressed( MouseEvent evt )
{
Point ptMouse = new Point() ;
ptMouse = evt . getPoint() ;
if ( m_nCirclesDrawn < m_nCirclesToDraw )
{
m_rectCircleArray[m_nCirclesDrawn]
= new Rectangle( ptMouse.x, ptMouse.y, 40, 40 ) ;
m_nCirclesDrawn++ ;
m_bMouse = true ;
repaint() ;
}
if ( (m_nCirclesDrawn == m_nCirclesToDraw) && (m_nCirclesToDraw != 0) )
{
int option = JOptionPane.showConfirmDialog( null,
"Try again?", "Finished", JOptionPane.YES_NO_OPTION ) ;
if( option == JOptionPane.YES_OPTION )
{
m_nCirclesDrawn = 0 ;
m_nCirclesToDraw = 0 ;
m_nSeconds = 0 ;
m_bStart = true ;
m_bPing = false ;
repaint() ;
}
else
{
System.exit(0) ;
}
}
}
}
class TimerCatcher implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
m_nSeconds++ ;
m_bPing = true ;
repaint() ;
}
}
class WindowCatcher extends WindowAdapter
{
public void windowClosing( WindowEvent evt )
{
m_timer . stop() ;
( evt . getWindow() ) . dispose() ;
System . exit( 0 ) ;
}
}
}
thanks