Advertisement
Guest User

loadingball

a guest
Jun 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. package temp;
  2.  
  3.  
  4.  
  5. import java.awt.*;
  6. import javax.swing.*;
  7.  
  8. class MyBall extends Thread
  9. {
  10. int x;
  11. int y;
  12. int sleeptime;
  13. int str_index;
  14. MyBallWindow mBw;
  15. Color c;
  16. String str, substr;
  17.  
  18. public MyBall(int xx, int yy, int sleepTime, MyBallWindow ssbb, Color cc)
  19. {
  20. str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  21. str_index = 0;
  22. substr = "A";
  23. x = xx;
  24. y = yy;
  25. sleeptime = sleepTime;
  26. mBw = ssbb;
  27. c = cc;
  28. }
  29.  
  30. public void run()
  31. {
  32. while (true)
  33. {
  34. mBw.repaint();;
  35. substr = str.substring(str_index, str_index+1);
  36. str_index++;
  37. if (str_index > 61) str_index = 0;
  38. y = y + 10;
  39. if (y > 550)
  40. y = 0;
  41. try
  42. {
  43. Thread.sleep(sleeptime);
  44. }
  45. catch (Exception e)
  46. {
  47. }
  48. }
  49. }
  50.  
  51. public void draw(Graphics g)
  52. {
  53. g.setColor(Color.white);
  54. g.fillOval(x, y-10, 50, 50);
  55. g.setFont(new Font("Rome", Font.BOLD, 50));
  56. g.drawString(substr, x, y+30);
  57.  
  58. g.setColor(c);
  59. g.fillOval(x, y, 50, 50);
  60.  
  61. g.setColor(Color.black);
  62. g.setFont(new Font("Monospaced", Font.BOLD, 50));
  63. g.drawString(substr, x+10, y+40);
  64. }
  65. }
  66.  
  67.  
  68. class MyBallWindow extends JFrame
  69. {
  70. MyBall myBall[];
  71.  
  72. public MyBallWindow()
  73. {
  74. myBall = new MyBall[2];
  75.  
  76. myBall[0] = new MyBall(100, 0, 300, this, Color.red);
  77. myBall[1] = new MyBall(200, 0, 400, this, Color.blue);
  78.  
  79.  
  80. for (int i = 0; i < 2; i++)
  81. {
  82. myBall[i].start();
  83. }
  84. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85. setSize(800, 800);
  86. setVisible(true);
  87. }
  88.  
  89. public void paint(Graphics g)
  90. {
  91. for (int i = 0; i < 2; i++)
  92. {
  93. myBall[i].draw(g);
  94. }
  95. }
  96. }
  97.  
  98. public class ThreadingBall
  99. {
  100. public static void main(String[] args)
  101. {
  102. new MyBallWindow();
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement