Guest User

Untitled

a guest
Jun 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. //<applet code="Circle" height="600" width="600"> </applet>
  5.  
  6. public class Circle extends Applet implements Runnable,KeyListener
  7. {
  8. int x=50,y=50;
  9. String str="Hi";
  10. int direction=6;
  11.  
  12.  
  13. Thread t=new Thread(this);
  14. public void init()
  15. {
  16. addKeyListener(this);
  17.  
  18. setBackground(Color.yellow);
  19. setForeground(Color.red);
  20. t.start();
  21. }
  22.  
  23. public void run()
  24. {
  25. for(;;)
  26. {
  27. //System.out.println("run"+i);
  28. repaint();
  29.  
  30. switchDir();
  31.  
  32.  
  33. try
  34. {
  35. Thread.sleep(100);
  36. }
  37. catch(InterruptedException e)
  38. {
  39. e.printStackTrace();
  40. }
  41.  
  42. }
  43.  
  44.  
  45. }
  46.  
  47. void switchDir()
  48. {
  49. if(x<1)
  50. {
  51. direction=6;
  52. return;
  53. }
  54. if(x+55>bounds().width)
  55. {
  56. direction=4;
  57. return;
  58. }
  59. if(y<1)
  60. {
  61. direction=2;
  62. return;
  63. }
  64. if(y+55>bounds().height)
  65. {
  66. direction=8;
  67. return;
  68. }
  69.  
  70. }
  71.  
  72. public void paint(Graphics g)
  73. {
  74. switch(direction)
  75. {
  76. case 6:
  77. x+=10;
  78. break;
  79. case 8:
  80. y-=10;
  81. break;
  82. case 2:
  83. y+=10;
  84. break;
  85. case 4:
  86. x-=10;
  87. break;
  88. }
  89. //g.drawString(str,x,y);
  90. //g.setForeground(Color.red);
  91. g.fillOval(x,y,50,50);
  92.  
  93.  
  94.  
  95.  
  96. }
  97.  
  98. public void keyPressed( KeyEvent e )
  99. {
  100. switch(e.getKeyCode())
  101. {
  102. case KeyEvent.VK_UP: direction=8; break;
  103. case KeyEvent.VK_DOWN: direction=2; break;
  104. case KeyEvent.VK_LEFT: direction=4; break;
  105. case KeyEvent.VK_RIGHT: direction=6; break;
  106. case KeyEvent.VK_E: t.stop(); break;
  107. \
  108. }
  109.  
  110. }
  111. public void keyReleased( KeyEvent e )
  112. {
  113. }
  114.  
  115. public void keyTyped( KeyEvent e )
  116. {
  117. }
  118. }
Add Comment
Please, Sign In to add comment