fork download
  1. import javax.swing.* ;
  2. import java.awt.Color ;
  3. import java.awt.Graphics ;
  4. import java.awt.Point ;
  5. import java.awt.event.* ;
  6.  
  7. public class Timer extends JFrame implements KeyListener, Runnable
  8. {
  9. public enum MoveDirection
  10. {
  11. UP, DOWN, LEFT, RIGHT, INITIAL
  12. }
  13.  
  14. public boolean isRunnung = true ;
  15. public Point head = new Point( 100, 100 ) ;
  16. public Point tail = new Point( 100, 100 ) ;
  17. public MoveDirection currentDirection = MoveDirection.INITIAL ;
  18. public static final int RECT_SIZE = 20 ;
  19. public static final int SLEEP_SEC = 500 ;
  20.  
  21. Thread thread = null ;
  22.  
  23. public Timer()
  24. {
  25. thread = new Thread( this ) ;
  26. thread.start() ;
  27. setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ) ;
  28. setSize( 500, 500 ) ;
  29. SnackPanel snack = new SnackPanel() ;
  30. snack.setBackground( Color.white ) ;
  31. add( snack ) ;
  32. addKeyListener( this ) ;
  33. setVisible( true ) ;
  34. }
  35.  
  36. public static void main( String[] args )
  37. {
  38. new Timer() ;
  39. }
  40.  
  41. public class SnackPanel extends JPanel
  42. {
  43. @Override
  44. public void paintComponent( Graphics graphics )
  45. {
  46.  
  47. super.paintComponent( graphics ) ;
  48. graphics.clearRect( head.x, head.y, RECT_SIZE, RECT_SIZE ) ;
  49.  
  50. if ( currentDirection == MoveDirection.UP )
  51. head.setLocation( head.x, head.y - RECT_SIZE ) ;
  52. else if ( currentDirection == MoveDirection.DOWN )
  53. head.setLocation( head.x, head.y + RECT_SIZE ) ;
  54. else if ( currentDirection == MoveDirection.LEFT )
  55. head.setLocation( head.x - RECT_SIZE, head.y ) ;
  56. else if ( currentDirection == MoveDirection.RIGHT )
  57. head.setLocation( head.x + RECT_SIZE, head.y ) ;
  58.  
  59. graphics.fillRect( head.x, head.y, RECT_SIZE, RECT_SIZE ) ;
  60. }
  61. }
  62.  
  63. @Override
  64. public void keyPressed( KeyEvent e )
  65. {
  66. // TODO Auto-generated method stub
  67.  
  68. }
  69.  
  70. @Override
  71. public void keyReleased( KeyEvent e )
  72. {
  73. // TODO Auto-generated method stub
  74.  
  75. }
  76.  
  77. @Override
  78. public void keyTyped( KeyEvent e )
  79. {
  80. isRunnung = false ;
  81. MoveDirection nextDirection = NextDirection( e ) ;
  82. if ( nextDirection != null )
  83. {
  84. if ( currentDirection != nextDirection )
  85. {
  86. currentDirection = nextDirection ;
  87. repaint() ;
  88. }
  89. }
  90.  
  91. isRunnung = true ;
  92. }
  93.  
  94. public MoveDirection NextDirection( KeyEvent e )
  95. {
  96.  
  97. if ( e.getKeyChar() == '5' )
  98. return MoveDirection.UP ;
  99. else if ( e.getKeyChar() == '2' )
  100. return MoveDirection.DOWN ;
  101. else if ( e.getKeyChar() == '1' )
  102. return MoveDirection.LEFT ;
  103. else if ( e.getKeyChar() == '3' )
  104. return MoveDirection.RIGHT ;
  105. else
  106. return null ;
  107.  
  108. }
  109.  
  110. @Override
  111. public void run()
  112. {
  113. while ( true )
  114. {
  115. if ( isRunnung )
  116. repaint() ;
  117.  
  118. try
  119. {
  120. thread.sleep( SLEEP_SEC ) ;
  121. }
  122. {
  123. e.printStackTrace() ;
  124. }
  125. }
  126. }
  127. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: class Timer is public, should be declared in a file named Timer.java
public class Timer extends JFrame implements KeyListener, Runnable
       ^
1 error
stdout
Standard output is empty