一、我的思路
- 创建窗体、设定窗体大小
- 在窗体中添加画布,并将画布添加至线程
- 线程run()方法持续执行,坐标不断变化,画布随之重绘,形成小球下落的效果,并随时判断小球X轴、Y轴坐标,触底后,通过flag值调整坐标加减方向
代码一
1 import java.awt.*; 2 public class BallHitWall{ 3 public static void main(String args[]){ 4 Frame w = new Frame(); 5 w.setSize(1024,750); 6 7 MyPanelBall mpb = new MyPanelBall(); 8 w.add(mpb); 9 10 Thread t = new Thread(mpb); 11 t.start(); 12 13 w.setVisible(true); 14 15 } 16 } 17 18 class MyPanelBall extends Panel implements Runnable{ 19 int x=10,y=10,flag=0; 20 public void paint(Graphics g){ 21 g.setColor(Color.red); 22 g.fillOval(x,y,10,10); 23 } 24 public void run(){ 25 while(true){ 26 switch(flag){ 27 case 0: x++;y++; 28 case 1: x++;y--; 29 case 2: x--;y++; 30 } 31 if(y>750){ 32 flag=1; 33 } 34 if(x>1024){ 35 flag=2; 36 } 37 if(x<2||y<2){ 38 flag=0; 39 } 40 System.out.println(flag); 41 System.out.println("x:"+x); 42 System.out.println("y:"+y); 43 try{ 44 Thread.sleep(10); 45 }catch(Exception e){} 46 repaint(); 47 } 48 } 49 }
执行结果
小球触底后没有反弹,X\Y轴坐标一直停留在751没变化
冥思苦想无果,下班路上走了一圈,以为是flag重新赋值的代码块位置不对,调整至switch前面,运行,结果依旧。。。查百度——switch用错了?
https://m.runoob.com/java/java-switch-case.html
Break,break,break……
改代码如下:
1 import java.awt.*; 2 public class BallHitWall{ 3 public static void main(String args[]){ 4 Frame w = new Frame(); 5 w.setSize(1024,750); 6 7 MyPanelBall mpb = new MyPanelBall(); 8 w.add(mpb); 9 10 Thread t = new Thread(mpb); 11 t.start(); 12 13 w.setVisible(true); 14 15 } 16 } 17 18 class MyPanelBall extends Panel implements Runnable{ 19 int x=10,y=10,flag=0; 20 public void paint(Graphics g){ 21 g.setColor(Color.red); 22 g.fillOval(x,y,10,10); 23 } 24 public void run(){ 25 while(true){ 26 switch(flag){ 27 case 0: x++;y++; 28 case 1: x++;y--; 29 case 2: x--;y++; 30 } 31 if(y>750){ 32 flag=1; 33 } 34 if(x>1024){ 35 flag=2; 36 } 37 if(x<2||y<2){ 38 flag=0; 39 } 40 System.out.println(flag); 41 System.out.println("x:"+x); 42 System.out.println("y:"+y); 43 try{ 44 Thread.sleep(10); 45 }catch(Exception e){} 46 repaint(); 47 } 48 } 49 }
到底是什么问题
1 import java.awt.*; 2 public class BallHitWall{ 3 public static void main(String args[]){ 4 Frame w = new Frame(); 5 w.setSize(400,300); 6 7 MyPanelBall mpb = new MyPanelBall(); 8 w.add(mpb); 9 10 Thread t = new Thread(mpb); 11 t.start(); 12 13 w.setVisible(true); 14 15 } 16 } 17 18 class MyPanelBall extends Panel implements Runnable{ 19 int x=0,y=0,flag=0; 20 public void paint(Graphics g){ 21 g.setColor(Color.red); 22 g.fillOval(x,y,10,10); 23 } 24 public void run(){ 25 while(true){ 26 switch(flag){ 27 case 0: x++;y++;break; 28 case 1: x++;y--;break; 29 case 2: x--;y--;break; 30 case 3: x--;y++; 31 } 32 if(y>=300){ 33 if(x>=300){ 34 flag=1; 35 } 36 37 } 38 if(x>=290&&x<=390&&y>=290){ 39 flag=1; 40 } 41 if(x>=390&&y>=190){ 42 flag=2; 43 } 44 if(y<=1&&x>=200&&x<=390){ 45 flag=3; 46 } 47 if(y<=290&&x<=1){ 48 flag=0; 49 } 50 System.out.println(flag); 51 System.out.println("x:"+x); 52 System.out.println("y:"+y); 53 try{ 54 Thread.sleep(10); 55 }catch(Exception e){} 56 repaint(); 57 } 58 } 59 }
还是没能实现小球绕着窗口四围反复运动,最大的问题是:会出现小球消失在可视窗口的情况。。
标签:case,1.5,mpb,10,王洋,小球,++,flag,public From: https://www.cnblogs.com/shannonredeemed/p/17898930.html