日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java弹球轨迹运动解说,动态弹球的实现 加入了多线程技术-javaSE游戏准备工作

發布時間:2024/9/27 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java弹球轨迹运动解说,动态弹球的实现 加入了多线程技术-javaSE游戏准备工作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

動態彈球的實現 加入了多線程技術--javaSE游戲準備工作

任務描述:實現了動態彈球的功能,對于有彈球功能的SE游戲奠定了基礎。

package 運用線程技術的小球;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;//不清楚這個有什么用

import java.util.*;

import javax.swing.*;

public class Bounces {

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame j = new BounceFrame();

j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

j.setVisible(true);

}

}

class BallRunnable implements Runnable//線程是Thread(Runnable target) 要使用線程 你必須要實現Runnable接口

{

private Ball ball;

private Component component ;

private static final int step = 300000;

private static final int delay = 1;

public BallRunnable(Ball aball,Component acomponent)

{

ball = aball;

component = acomponent;

}

public void run()

{

try{

for(int i = 0 ; i <= step ; i++)

{

ball.move(component.getBounds());

component.repaint();//面板不斷刷新

Thread.sleep(delay);

}

}catch(InterruptedException e){}

}

}

class Ball

{//實現一個小球類 這個小球包含的方法 包括move()

private double x = 0;

private double y = 0 ;

private double dx = 1;

private double dy = 1;

private static final int XSIZE =15;

private static final int YSIZE =15;

public void move(Rectangle2D bounds)

{

x = x + dx;

y = y + dy;

if(x < bounds.getMinX())

{

x = bounds.getMinX();

dx = -dx;

}

if(x+XSIZE>=bounds.getMaxX())

{

x = bounds.getMaxX()-XSIZE;

dx = - dx;

}

if(y < bounds.getMinY())

{

y = bounds.getMinY();

dy = -dy;

}

if(y+YSIZE>=bounds.getMaxY())

{

y = bounds.getMaxY()-YSIZE;

dy = - dy;

}

}//關于小球如何移動

public Ellipse2D getShape()

{

return new Ellipse2D.Double(x,y,XSIZE,YSIZE);

}//返回此時的小球的繪畫位置

}

class BallPanel extends JPanel

{

private ArrayList balls = new ArrayList();

//定義了一個集合 這個集合是Ball類型的存儲 這個知識點很關鍵 管存儲的作用

public void add(Ball b)

{

balls.add(b);//將Ball的對象加載進去

}//這就是重寫JPanel中的add方法 實現集合加入要更新的小球的重要一步

public void paint(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;//轉換成2D的繪圖模式了

for(Ball b :balls)

{

g2.fill(b.getShape());//這時的g2重新繪制小球的全部信息 fill是 專門繪制圖形的方法

}

}

}

class BounceFrame extends JFrame

{

private BallPanel panel;

public BounceFrame()

{

setTitle("小球");

panel = new BallPanel();

panel.setBackground(Color.BLUE);

add(panel,BorderLayout.CENTER);

JPanel buttonPane = new JPanel();

setBounds(200,200,700,500);

addButton(buttonPane,"start",new ActionListener(){

public void actionPerformed(ActionEvent event)

{

addBall();

}

});

addButton(buttonPane,"Close",new ActionListener(){

public void actionPerformed(ActionEvent event)

{

System.exit(0);

}

});

add(buttonPane,BorderLayout.SOUTH);

}

public void addButton(Container c,String title,ActionListener listener)

{

JButton b = new JButton(title);

c.add(b);

b.addActionListener(listener);

}//這個算是變形吧 學習思想

public void addBall()

{

Ball ball = new Ball();

panel.add(ball);

Runnable r = new BallRunnable(ball,panel);

Thread t = new Thread(r);//Thread(Runnable target)

t.start();//啟動線程 實質上是啟動的run()方法

}

}

總結

以上是生活随笔為你收集整理的java弹球轨迹运动解说,动态弹球的实现 加入了多线程技术-javaSE游戏准备工作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。