A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

package com.Joney.thread;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;
public class BallGame extends Frame{
int WIDTH=600,HEIGHT=400;
Ball ball =new Ball(10,30,3,2,false,false,Color.BLUE,this);
Ball ball2 =new Ball(20,60,5,2,false,false,Color.ORANGE,this);
//存放小球的集合,使用泛型
java.util.List<Ball> balls =new ArrayList<Ball>();
Label label =new Label();
public BallGame(){
  setSize(WIDTH,HEIGHT);
  setTitle("点击F键添加小球");
  balls.add(ball);
  balls.add(ball2);
  //匿名内部类,实现关闭窗体
  this.addWindowListener(new WindowAdapter(){
   @Override
   public void windowClosing(WindowEvent arg0) {
    System.exit(0);
   }
  });
  this.addKeyListener(new MyKeyListener());
  label.setText("目前小球个数:"+balls.size());
  add(label,BorderLayout.SOUTH);
  setVisible(true);
}
public static void main(String[] args) {
  BallGame bg =new BallGame();
  Thread bt=new BallThread(bg);
  bt.start();
}
@Override
public void paint(Graphics g) {
  //增强for循环,遍历小球集合,将每个小球都绘制出来
  for(Ball b:balls){
   b.draw(g);
  }
}
class MyKeyListener extends KeyAdapter{
  Random r =new Random();
  @Override
  public void keyPressed(KeyEvent arg0) {
   //如果点击“F”键,则相应
   if(arg0.getKeyCode()==KeyEvent.VK_F){
    createNewball();
   }
  }
  public synchronized void createNewball(){
   //随即出现的位置
   int x=r.nextInt(WIDTH-50);
   int y=r.nextInt(HEIGHT-50);
   //随即速度
   int xs=r.nextInt(5)+1;
   int ys=r.nextInt(5);
   //随即颜色
   Color c =new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
   Ball b =new Ball(x,y,xs,ys,false,false,c,BallGame.this);
   balls.add(b);
   label.setText("目前小球个数:"+balls.size());
  }
}
}
class BallThread extends Thread{
BallGame map;
public BallThread(BallGame bg){
  map=bg;
}
@Override
public void run() {
  while (true){
   //增强for循环,遍历小球集合,将每个小球都移动一次
   for(Ball b:map.balls){
    b.move();
   }
   map.repaint();
   try {
    Thread.sleep(50);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
}

}/*===========================================================================================================*/

package com.Joney.thread;
import java.awt.Color;
import java.awt.Graphics;

public class Ball {
int x,y;//x y坐标
int xs,ys;//x轴y轴速度
boolean xb,yb;//是否到地图边缘
Color color;//颜色
BallGame map;//小球所 在 地图

public Ball(){}
public Ball(int x, int y, int xs, int ys, boolean xb, boolean yb,
   Color color,BallGame map) {
  super();
  this.x = x;
  this.y = y;
  this.xs = xs;
  this.ys = ys;
  this.xb = xb;
  this.yb = yb;
  this.color = color;
  this.map=map;
}
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 getXs() {
  return xs;
}
public void setXs(int xs) {
  this.xs = xs;
}
public int getYs() {
  return ys;
}
public void setYs(int ys) {
  this.ys = ys;
}
public boolean isXb() {
  return xb;
}
public void setXb(boolean xb) {
  this.xb = xb;
}
public boolean isYb() {
  return yb;
}
public void setYb(boolean yb) {
  this.yb = yb;
}
public Color getColor() {
  return color;
}
public void setColor(Color color) {
  this.color = color;
}
public void move(){
  //判断是否到达边缘
  if(x>(map.WIDTH-10))xb=true;
  if(x<5)xb=false;
  if(y>(map.HEIGHT-35))yb=true;
  if(y<10)yb=false;
  //运动
  if(!xb)x+=xs;//x坐标为x方向的速度
  else x-=xs;
  if(!yb)y+=ys;//x坐标为x方向的速度
  else y-=ys;
  
}
public void draw(Graphics g){
  Color c =g.getColor();
  g.setColor(getColor());
  g.fillArc(x, y, 10, 10, 0, 360);
  g.setColor(c);
}


}


QQ截图20131014181145.png (34.39 KB, 下载次数: 20)

QQ截图20131014181145.png

评分

参与人数 2技术分 +1 黑马币 +30 收起 理由
zhou5852 + 30 很给力!
黄文伯 + 1 赞一个!

查看全部评分

4 个回复

倒序浏览
用下发言中的code那个东西把代码修改一下 ,不好复制,选中的时候都是图片,很好奇额,因为我看那些关于图形的类都是抽象类,所以就没自己用过,很好  我得试试~~
回复 使用道具 举报
很不错  我花了半个小时把那代码整理出来 郁闷啊

  1. import java.awt.*;
  2. import java.awt.event.KeyAdapter;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Random;
  10. public class BallGame extends Frame{
  11. int WIDTH=600,HEIGHT=400;
  12. Ball ball =new Ball(10,30,3,2,false,false,Color.BLUE,this);
  13. Ball ball2 =new Ball(20,60,5,2,false,false,Color.ORANGE,this);
  14. //存放小球的集合,使用泛型
  15. List<Ball> balls =new ArrayList<Ball>();
  16. Label label =new Label();
  17. public BallGame(){
  18.   setSize(WIDTH,HEIGHT);
  19.   setTitle("点击F键添加小球");
  20.   balls.add(ball);
  21.   balls.add(ball2);
  22.   //匿名内部类,实现关闭窗体
  23.   this.addWindowListener(new WindowAdapter(){
  24.    @Override
  25.    public void windowClosing(WindowEvent arg0) {
  26.     System.exit(0);
  27.    }
  28.   });
  29.   
  30.   this.addKeyListener(new MyKeyListener());
  31.   label.setText("目前小球个数:"+balls.size());
  32.   add(label,BorderLayout.SOUTH);
  33.   setVisible(true);
  34. }

  35. public static void main(String[] args) {
  36.   BallGame bg =new BallGame();
  37.   Thread bt=new BallThread(bg);
  38.   bt.start();
  39. }
  40. @Override
  41. public void paint(Graphics g) {
  42.   //增强for循环,遍历小球集合,将每个小球都绘制出来
  43.   for(Ball b:balls){
  44.    b.draw(g);
  45.   }
  46. }
  47. class MyKeyListener extends KeyAdapter{
  48.   Random r =new Random();
  49.   @Override
  50.   public void keyPressed(KeyEvent arg0) {
  51.    //如果点击“F”键,则相应
  52.    if(arg0.getKeyCode()==KeyEvent.VK_F){
  53.     createNewball();
  54.    }
  55.   }

  56.   public synchronized void createNewball(){
  57.    //随即出现的位置
  58.    int x=r.nextInt(WIDTH-50);
  59.    int y=r.nextInt(HEIGHT-50);
  60.    //随即速度
  61.    int xs=r.nextInt(5)+1;
  62.    int ys=r.nextInt(5);
  63.    //随即颜色
  64.    Color c =new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
  65.    Ball b =new Ball(x,y,xs,ys,false,false,c,BallGame.this);
  66.    balls.add(b);
  67.    label.setText("目前小球个数:"+balls.size());
  68.   }
  69. }
  70. }



复制代码
第二个类
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. public class Ball {
  4. int x,y;//x y坐标
  5. int xs,ys;//x轴y轴速度
  6. boolean xb,yb;//是否到地图边缘
  7. Color color;//颜色
  8. BallGame map;//小球所 在 地图

  9. public Ball(){}
  10. public Ball(int x, int y, int xs, int ys, boolean xb, boolean yb,
  11.    Color color,BallGame map) {
  12.   super();
  13.   this.x = x;
  14.   this.y = y;
  15.   this.xs = xs;
  16.   this.ys = ys;
  17.   this.xb = xb;
  18.   this.yb = yb;
  19.   this.color = color;
  20.   this.map=map;
  21. }
  22. public int getX() {
  23.   return x;
  24. }
  25. public void setX(int x) {
  26.   this.x = x;
  27. }
  28. public int getY() {
  29.   return y;
  30. }
  31. public void setY(int y) {
  32.   this.y = y;
  33. }
  34. public int getXs() {
  35.   return xs;
  36. }
  37. public void setXs(int xs) {
  38.   this.xs = xs;
  39. }
  40. public int getYs() {
  41.   return ys;
  42. }
  43. public void setYs(int ys) {
  44.   this.ys = ys;
  45. }
  46. public boolean isXb() {
  47.   return xb;
  48. }
  49. public void setXb(boolean xb) {
  50.   this.xb = xb;
  51. }
  52. public boolean isYb() {
  53.   return yb;
  54. }
  55. public void setYb(boolean yb) {
  56.   this.yb = yb;
  57. }
  58. public Color getColor() {
  59.   return color;
  60. }
  61. public void setColor(Color color) {
  62.   this.color = color;
  63. }
  64. public void move(){
  65.   //判断是否到达边缘
  66.   if(x>(map.WIDTH-10))xb=true;
  67.   if(x<5)xb=false;
  68.   if(y>(map.HEIGHT-35))yb=true;
  69.   if(y<10)yb=false;
  70.   //运动
  71.   if(!xb)x+=xs;//x坐标为x方向的速度
  72.   else x-=xs;
  73.   if(!yb)y+=ys;//x坐标为x方向的速度
  74.   else y-=ys;
  75.   
  76. }
  77. public void draw(Graphics g){
  78.   Color c =g.getColor();
  79.   g.setColor(getColor());
  80.   g.fillArc(x, y, 10, 10, 0, 360);
  81.   g.setColor(c);
  82. }


  83. }
复制代码
第三个类

  1. public class BallThread extends Thread{
  2. BallGame map;
  3. public BallThread(BallGame bg){
  4.   map=bg;
  5. }

  6. public void run() {
  7.   while (true){
  8.    //增强for循环,遍历小球集合,将每个小球都移动一次
  9.    for(Ball b:map.balls){
  10.     b.move();
  11.    }
  12.    map.repaint();
  13.    try {
  14.     Thread.sleep(50);
  15.    } catch (InterruptedException e) {
  16.     // TODO Auto-generated catch block
  17.     e.printStackTrace();
  18.    }
  19.   }
  20. }

  21. }/*===========================================================================================================*/
复制代码
回复 使用道具 举报
zhou5852 发表于 2013-10-15 20:27
很不错  我花了半个小时把那代码整理出来 郁闷啊
看第二个类第三个类

不好意思,粘贴下还有那么麻烦?下回我写代码里就是了,呵呵,文兄昨天教的,才知道
回复 使用道具 举报
这个程序不用三个类两个类就行,放在同一个包中即可第一个类

  1. package com.Joney.thread;
  2. import java.awt.*;
  3. import java.awt.event.KeyAdapter;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10. public class BallGame extends Frame{
  11. int WIDTH=600,HEIGHT=400;
  12. Ball ball =new Ball(10,30,3,2,false,false,Color.BLUE,this);
  13. Ball ball2 =new Ball(20,60,5,2,false,false,Color.ORANGE,this);
  14. //存放小球的集合,使用泛型
  15. java.util.List<Ball> balls =new ArrayList<Ball>();
  16. Label label =new Label();
  17. public BallGame(){
  18.   setSize(WIDTH,HEIGHT);
  19.   setTitle("点击F键添加小球");
  20.   balls.add(ball);
  21.   balls.add(ball2);
  22.   //匿名内部类,实现关闭窗体
  23.   this.addWindowListener(new WindowAdapter(){
  24.    @Override
  25.    public void windowClosing(WindowEvent arg0) {
  26.     System.exit(0);
  27.    }
  28.   });
  29.   this.addKeyListener(new MyKeyListener());
  30.   label.setText("目前小球个数:"+balls.size());
  31.   add(label,BorderLayout.SOUTH);
  32.   setVisible(true);
  33. }
  34. public static void main(String[] args) {
  35.   BallGame bg =new BallGame();
  36.   Thread bt=new BallThread(bg);
  37.   bt.start();
  38. }
  39. @Override
  40. public void paint(Graphics g) {
  41.   //增强for循环,遍历小球集合,将每个小球都绘制出来
  42.   for(Ball b:balls){
  43.    b.draw(g);
  44.   }
  45. }
  46. class MyKeyListener extends KeyAdapter{
  47.   Random r =new Random();
  48.   @Override
  49.   public void keyPressed(KeyEvent arg0) {
  50.    //如果点击“F”键,则相应
  51.    if(arg0.getKeyCode()==KeyEvent.VK_F){
  52.     createNewball();
  53.    }
  54.   }
  55.   public synchronized void createNewball(){
  56.    //随即出现的位置
  57.    int x=r.nextInt(WIDTH-50);
  58.    int y=r.nextInt(HEIGHT-50);
  59.    //随即速度
  60.    int xs=r.nextInt(5)+1;
  61.    int ys=r.nextInt(5);
  62.    //随即颜色
  63.    Color c =new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
  64.    Ball b =new Ball(x,y,xs,ys,false,false,c,BallGame.this);
  65.    balls.add(b);
  66.    label.setText("目前小球个数:"+balls.size());
  67.   }
  68. }
  69. }
  70. class BallThread extends Thread{
  71. BallGame map;
  72. public BallThread(BallGame bg){
  73.   map=bg;
  74. }
  75. @Override
  76. public void run() {
  77.   while (true){
  78.    //增强for循环,遍历小球集合,将每个小球都移动一次
  79.    for(Ball b:map.balls){
  80.     b.move();
  81.    }
  82.    map.repaint();
  83.    try {
  84.     Thread.sleep(50);
  85.    } catch (InterruptedException e) {
  86.     // TODO Auto-generated catch block
  87.     e.printStackTrace();
  88.    }
  89.   }
  90. }

  91. }
复制代码
第二个类
  1. package com.Joney.thread;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. public class Ball {
  5. int x,y;//x y坐标
  6. int xs,ys;//x轴y轴速度
  7. boolean xb,yb;//是否到地图边缘
  8. Color color;//颜色
  9. BallGame map;//小球所 在 地图

  10. public Ball(){}
  11. public Ball(int x, int y, int xs, int ys, boolean xb, boolean yb,
  12.    Color color,BallGame map) {
  13.   super();
  14.   this.x = x;
  15.   this.y = y;
  16.   this.xs = xs;
  17.   this.ys = ys;
  18.   this.xb = xb;
  19.   this.yb = yb;
  20.   this.color = color;
  21.   this.map=map;
  22. }
  23. public int getX() {
  24.   return x;
  25. }
  26. public void setX(int x) {
  27.   this.x = x;
  28. }
  29. public int getY() {
  30.   return y;
  31. }
  32. public void setY(int y) {
  33.   this.y = y;
  34. }
  35. public int getXs() {
  36.   return xs;
  37. }
  38. public void setXs(int xs) {
  39.   this.xs = xs;
  40. }
  41. public int getYs() {
  42.   return ys;
  43. }
  44. public void setYs(int ys) {
  45.   this.ys = ys;
  46. }
  47. public boolean isXb() {
  48.   return xb;
  49. }
  50. public void setXb(boolean xb) {
  51.   this.xb = xb;
  52. }
  53. public boolean isYb() {
  54.   return yb;
  55. }
  56. public void setYb(boolean yb) {
  57.   this.yb = yb;
  58. }
  59. public Color getColor() {
  60.   return color;
  61. }
  62. public void setColor(Color color) {
  63.   this.color = color;
  64. }
  65. public void move(){
  66.   //判断是否到达边缘
  67.   if(x>(map.WIDTH-10))xb=true;
  68.   if(x<5)xb=false;
  69.   if(y>(map.HEIGHT-35))yb=true;
  70.   if(y<10)yb=false;
  71.   //运动
  72.   if(!xb)x+=xs;//x坐标为x方向的速度
  73.   else x-=xs;
  74.   if(!yb)y+=ys;//x坐标为x方向的速度
  75.   else y-=ys;
  76.   
  77. }
  78. public void draw(Graphics g){
  79.   Color c =g.getColor();
  80.   g.setColor(getColor());
  81.   g.fillArc(x, y, 10, 10, 0, 360);
  82.   g.setColor(c);
  83. }


  84. }
复制代码
在第一个类中运行,其实真没那么麻烦,没必要半小时整理,和大家共同进步!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马