[Java] 小球撞墙案例2 →→→→→进入此内容的聊天室

来自 , 2020-06-06, 写在 Java, 查看 177 次.
URL http://www.code666.cn/view/e655c771
  1. //1510821001廖益利
  2. //练习三 读懂小球撞壁代码,并完善.txt(已完成100%)
  3. import java.awt.Frame;
  4. import java.awt.Graphics;
  5. import java.awt.Panel;
  6.  
  7.  
  8. public class pp{
  9. public static void  main(String args[]){
  10. Frame w = new Frame();
  11.  
  12. MyPanel mp = new MyPanel();
  13. w.add(mp);
  14.  
  15. Thread th = new Thread(mp);
  16. th.start();
  17.  
  18. w.setSize(300,400);
  19. w.show();
  20.  
  21. }
  22. }
  23.  
  24. class MyPanel extends Panel implements Runnable{
  25.  
  26. int y=30;
  27. int x=30;
  28. int att=0;
  29. public void paint(Graphics g){
  30.  
  31. //while(true){
  32.  
  33. g.fillOval(x, y, 20, 20);
  34.  
  35. //}    
  36.  
  37. }
  38. @Override
  39. public void run() {
  40. // TODO Auto-generated method stub
  41. while(true){
  42. // 定义飞行姿态
  43. if (att == 0) {
  44. x++;
  45. y++;
  46. }else if (att == 1) {
  47. x--;
  48. y++;
  49. }else if (att == 2) {
  50. x--;
  51. y--;
  52. }else if (att == 3) {
  53. x++;
  54. y--;
  55. }
  56.  
  57. // 改变飞行姿态
  58. if (x > 260&&y<360) { //263+20=283,横坐标占用了17个像素,x>263有两种可能,左下或者右上
  59. if(att == 0) att=1;
  60. else att=2;
  61.  
  62.  
  63. } else if (x < 260&&y>360){
  64. if(att==1)
  65. att = 2;
  66. else att=3;
  67.  
  68. } else if (x < 0&&y<360){
  69. if(att==2)
  70. att = 3;
  71. else att=0;
  72.  
  73. }else if (x < 270&&y<0){
  74. if(att==3)
  75. att = 0;
  76. else att=1;
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84. try{
  85. Thread.sleep(8) ;
  86. }catch(Exception e){
  87.  
  88. }
  89. repaint() ;
  90. }
  91.  
  92. }
  93.  
  94.  
  95. }
  96.  

回复 "小球撞墙案例2"

这儿你可以回复上面这条便签

captcha