今天学习了多线程,总体来说,先从进程入手。
什么是进程呢? 简单来说就是正在运行的程序,例如打开资源管理器,你将看到里面的进程,有些是你运行的程序创建出来的,有些是后台运行的程序创建的。
什么又是线程呢?就是独立的运行单位。一个进程至少有一个线程。进程是程序的一次执行,那么线程可以理解为进程中执行的一段程序片段。
而我们在学习线程之前,所写的程序都是单线程的,那么编译器便是逐行扫描代码并逐行执行。尽管所学的线程不是那么好控制,在精确度要求极高的行业很难执行,如金融什么的,但是它能够让资源最大化的利用起来,提升应用程序的效果。
这一次的课后练习便是做小球,要求是:
1、小球不能超出边框
2、小球相互碰撞,进行反弹
package 线程; import java.awt.Color; import java.awt.Graphics; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; public class BallMove extends JFrame{ private static final Graphics g = null; public static void main(String [] args){ BallMove ball = new BallMove(); ball.initUI(); } private void initUI() { this.setTitle("小球移动"); this.setSize(800,500); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); this.setResizable(false); /*这个地方是打算添加一个按钮,并加上监听器,按钮每按一下就增加一个小球, 目前功能还未实现*/ // this.setLayout(null); // JButton but = new JButton("添加"); // but.setBounds(20, 20, 80, 30); // this.add(but); this.setVisible(true); Graphics g = this.getGraphics(); ball1.start(); ball2.start(); // ball3.start(); // ball4.start(); } /* 事实上这里可以不必每添加一个小球就要实例化一个,可以修改为数组*/ Ball ball1 = new Ball(0,20,50,this,g); Ball ball2 = new Ball(100,180,50,this,g); // Ball ball3 = new Ball(Color.pink,300,400,50,50,this,g); // Ball ball4 = new Ball(Color.YELLOW,200,30,70,70,this,g); /*这里实现的是画布的重绘*/ public void paint(Graphics g){ super.paint(g); Random random = new Random();//随机 Color color=new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255));//颜色 g.setColor(color); g.fillOval(ball1.getX(), ball1.getY(), ball1.getR(), ball1.getR()); g.setColor(color.BLACK); g.fillOval(ball2.getX(), ball2.getY(), ball2.getR(), ball2.getR()); if(Math.sqrt((ball1.getX()-ball2.getX())*(ball1.getX()-ball2.getX())+ (ball1.getY()-ball2.getY())*(ball1.getY()-ball2.getY())) <= ball1.getR()){ int tempxdirection = ball2.getXdirection(); // int tempydirection = ball2.getXdirection(); // int tempxspeed = ball2.getXspeed(); // int tempyspeed = ball2.getYspeed(); /*这里发生了很神奇的事情,我本来的想法是当两个小球相撞时,两个小球应该交换运动方向,那么x、y的方向都应该改变,可是都改变后两小球便会平行行走,而只改变小球的x方向,则可以实现我想要达到的效果,这个地方我就没想明白!!!*/ ball2.setXdirection(ball1.getXdirection()); // ball2.setYdirection(ball1.getYdirection()); // ball2.setXspeed(ball1.getXspeed()); // ball2.setYspeed(ball1.getYspeed()); ball1.setXdirection(tempxdirection); // ball1.setYdirection(tempydirection); // ball1.setXspeed(tempxspeed); // ball1.setYspeed(tempyspeed); System.out.println("方向交换了....."); } // g.setColor(ball3.getC()); // g.fillOval(ball3.getX(), ball3.getY(), ball3.getWidth(), ball3.getHeight()); // g.setColor(ball4.getC()); // g.fillOval(ball4.getX(), ball4.getY(), ball4.getWidth(), ball4.getHeight()); } }
package 线程; import java.awt.Graphics; import java.util.Random; import javax.swing.JFrame; public class Ball extends Thread{ private Ball ball;//表示当前小球 private JFrame jf;//声明一个窗体 public Ball getBall() { return ball; } public void setBall(Ball ball) { this.ball = ball; } private Graphics g;//声明一个画板 private int x,y;//小球的初始位置x、y private int r;//小球的半径 //用四个整型数据表示四个方向 int NORTH = 1; int SOURTH = 2; int WEST = 3; int EAST = 4; //创建一个随机数对象 Random random = new Random();//随机 private int xspeed,yspeed;//定义x、y方向上的速度 private int xdirection = WEST,ydirection = NORTH;//定义x、y的方向 public Ball(int x,int y,int r,JFrame jf,Graphics g){ this.x = x; this.y = y; this.jf = jf; this.g = g; this.r = r; this.ball = ball; } public void run(){ while(true){ try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } onHitwall(); move(); jf.repaint(); } } public void move(){ xspeed = 5+random.nextInt(10);//x方向速度 yspeed = 5+random.nextInt(10);//y方向速度 switch(xdirection){//对于x方向 case 3:x = x - xspeed;break;//如果小球的运动方向向西,x值减小 case 4:x = x + xspeed;break;//如果小球的运动方向向东,x值增大 } switch(ydirection){//对于y方向 case 1:y = y - yspeed;break;//如果小球的运动方向向北,y值减小 case 2:y = y + yspeed;break;//如果小球的运动方向向难,y值增大 } } public void onHitwall(){ //如果碰到边界,方向改变 if(x>jf.getWidth()-r){ xdirection = 3; } if(x<0){ xdirection = 4; } if(y>jf.getHeight()-r){ ydirection = 1; } if(y<0+r/2){ ydirection = 2; } } // public void onHitball(){ // if(this-ball.getX()){ // // } // } public int getXspeed() { return xspeed; } public void setXspeed(int xspeed) { this.xspeed = xspeed; } public int getYspeed() { return yspeed; } public void setYspeed(int yspeed) { this.yspeed = yspeed; } public int getXdirection() { return xdirection; } public void setXdirection(int xdirection) { this.xdirection = xdirection; } public int getYdirection() { return ydirection; } public void setYdirection(int ydirection) { this.ydirection = ydirection; } public JFrame getJf() { return jf; } public void setJf(JFrame jf) { this.jf = jf; } public Graphics getG() { return g; } public void setG(Graphics g) { this.g = g; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getR() { return r; } public void setR(int r) { this.r = r; } }
相关推荐
关于c#数字飞来飞去的代码,用数组做的。对于一些大学生会有用。
这是一款 SWF源码,效果是一条金黄色的龙飞来飞去 总共大小413KB 开发与SHTML CS5 工具 需要的朋友那去吧
代码片段: var solveBirdMove = function(bird) { ['x', 'y', 'z'].forEach(function(key) { ... if (Math.abs(bird.move[key]) > 0.003) { ... bird.move.x = speed * (mouse.x - bird.pos.x bird....
飞来飞趣旅行MCN商业计划书.pdf
有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar有小球飞来的链接.rar
箭头飞来飞去flash动画是一款白色箭头不停的飞动画素材下载。
【短视频商业BP】飞来飞趣旅行MCN商业计划书.pdf
这是一款关于小球的经典游戏代码,小球由于重力作用在天空中飞来飞去。
Bmail联系人飞来飞去效果
Atom-linter-pylama.zip,在飞舞的原子中使用pylama的lint python皮亚马,atom是一个用web技术构建的开源文本编辑器。
飞机 该项目模拟了一架飞机,因为它在类似太空的环境中飞行。 特征 当前旋转但不移动的平面 一个奇怪颜色的“地板”盒子,用于空间中的一些参考。 飞机引擎的粒子效果
一种实用的Delphi图片打开飞入特效..rar
unity3D(2018.3)实现弹开小球游戏,游戏运行时,点击鼠标左键,生成绿色小球,并朝红色方块飞来。点击鼠标右键,红色方块跳起。当红色方块与绿色小球相撞时,绿色小球被弹开。
绕当前轴 360 度飞行并... % movieAz360:通过飞来飞去从当前窗口制作电影% % [M] = movieAz360(N,filename,q,el); % N:360/N 步长的帧数% 文件名:如果为'',则不生成文件% q:质量默认值=75; % el:用户指定的海拔
大雁飞来了阅读答案 [不南飞的大雁,阅读答案] .docx
桌面会飞的小鸟 中文绿色版桌面上有小鸟飞来飞去,很有意思
Unity_Spinny_Rocket 这场比赛是关于一个火箭,它旋转并飞来飞去到达目标。
一种户外第一人称射击游戏,具有受“魔毯”游戏启发的实时策略元素。 在岛上飞来飞去,施展咒语,召唤怪物,收集法力值并建造城堡。
德飞来STM32例程,必须精品,欢迎交流