zoukankan      html  css  js  c++  java
  • Java模拟弹球效果

     1 /*
     2  * 不添加事件监听的话最大最小可以用,但关闭不可以
     3  */
     4 import java.awt.Color;
     5 import java.awt.Frame;
     6 import java.awt.Graphics;
     7 import java.awt.Image;
     8 import java.awt.Toolkit;
     9 import java.awt.event.WindowAdapter;
    10 import java.awt.event.WindowEvent;
    11 //coordinate:坐标系;oval椭圆(矩形的内切,不信可以把矩形和椭圆的参数设置的一样)
    12 //计算机画椭圆采用的是参数方程 x = a*cosθ ,y = b*sinθ,不用一般方程;采用弧度
    13 public class BallGame extends Frame {
    14     
    15     /**
    16      * 图片的位置不和src平级就显示不了
    17      */
    18     private static final long serialVersionUID = -4408396034592021515L;//这个是导出jar包时建议的,我也不知道啥意思
    19     Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); 
    20     double x=100;
    21     double y=100;
    22     double degree = Math.PI/3;//初始角度
    23     public void paint(Graphics g){
    24         //System.out.println("窗口被画了一次!");
    25         g.drawImage(sun, (int)x,(int)y, null);
    26         x = x+ 10*Math.cos(degree);//椭圆的中心每次要变化
    27         y = y+10*Math.sin(degree);
    28         
    29         if(y>300-30){
    30             degree = - degree;
    31         }
    32         if(x>500-30){
    33             degree = Math.PI-degree;
    34         }
    35         if(x<0){
    36             degree = Math.PI-degree;
    37         }
    38         if(y<30){//减去标题栏宽度,因为标题栏也算在坐标系内,否则上面小球会进去
    39             degree = -degree;
    40         }
    41     }
    42     
    43     void launchFrame(){
    44         setSize(500, 300);
    45         setLocation(50, 50);
    46         setTitle("火星十一郎");
    47         setBackground(Color.black);
    48         myEvent();
    49         setVisible(true);
    50         new PaintThread().start();
    51     }
    52     
    53     public static void main(String[] args){
    54         new BallGame().launchFrame();
    55     }
    56 
    57     class PaintThread extends Thread {
    58         public void run(){
    59             while(true){
    60                 repaint();  //重画窗口!
    61                 try{
    62                     Thread.sleep(40);   //40ms    1s=1000ms
    63                 }catch (Exception e) {
    64                     e.printStackTrace();
    65                 }
    66             }
    67         }
    68     }
    69     
    70     private void myEvent()
    71     {
    72         /*
    73          * Cannot make a static reference to the non-static method addWindowListener(WindowListener) from the type Window
    74          * 把BallGame改成this就对了
    75          */
    76         this.addWindowListener(new WindowAdapter()//窗口监听
    77         {
    78             public void windowClosing(WindowEvent e)
    79             {
    80                 //System.out.println("窗体执行关闭!");
    81                 System.exit(0);
    82             }
    83         });
    84     }
    85 }

      导出jar包直接用MyEclipse的Export,不过我发现我的jar包没显示图片。

      Roy说这样写就行了(确实对了),并把images放在src目录下。

    1 Image sun = Toolkit.getDefaultToolkit().getImage(getFilePath("/images/sun.jpg"));
    2 
    3 
    4 public URL getFilePath(String path) {
    5         return BallGame.class.getResource(path);
    6     }
  • 相关阅读:
    第11次作业
    第十次实验
    第九次作业
    第八次实验
    第七次作业
    第六次作业
    作业
    JAVA实验三
    JAVA实验二
    JAVA实验一
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2989529.html
Copyright © 2011-2022 走看看