Java中小球碰撞并使用按鈕控制數(shù)量實(shí)例代碼
剛開(kāi)始實(shí)訓(xùn)第三天,要求java做一個(gè)小球碰撞的小游戲,啥也不會(huì)的我,決定寫(xiě)寫(xiě)啥。
先根據(jù)程序要求寫(xiě)了一個(gè)窗口
package three.day;
import java.awt.event.*;
import javax.swing.*;
public class Windows extends JFrame{
DrowJPs jp=new DrowJPs();
public void init() {
this.setSize(800,500);
this.setLocationRelativeTo(rootPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("天沫丶寒楓");
this.add(jp);
this.setVisible(true);
}
public static void main(String[] args) {
Windows win=new Windows();
win.init();
}
}
然后寫(xiě)一個(gè)畫(huà)圖
package three.day;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrowJPs extends JPanel implements Runnable{
int[] x=new int[1000],y=new int[1000],s=new int[1000],xt=new int[1000],yt=new int[1000];
int[] r=new int[1000],g=new int[1000],b=new int[1000];
int num=5;
public DrowJPs() {
for (int i = 0; i < 1000; i++) {
x[i]=(int)(Math.random()*450);
y[i]=(int)(Math.random()*230);
r[i]=(int)(Math.random()*256);
g[i]=(int)(Math.random()*256);
b[i]=(int)(Math.random()*256);
xt[i]=(int)(Math.random()*4+1);
yt[i]=(int)(Math.random()*4+1);
s[i]=(int)(Math.random()*200+20);
}
Thread t=new Thread(this);
Thread t1=new Thread(this);
t.start();
t1.start();
}
public void paint(Graphics gr) {
super.paint(gr);
setBackground(Color.pink);
for (int i = 0; i < num; i++) {
gr.setColor(new Color(r[i],g[i],b[i]));
gr.fillOval(x[i], y[i], s[i], s[i]);
}
}
public void run() {
while(true) {
for (int i = 0; i < num; i++) {
if(x[i]<=0|x[i]>=(790-s[i]))xt[i]*=-1;
if(y[i]<=0|y[i]>=(465-s[i]))yt[i]*=-1;
x[i]+=xt[i];y[i]+=yt[i];
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}
}
開(kāi)了倆個(gè)線程,一個(gè)數(shù)量大了有點(diǎn)卡
這樣運(yùn)行就ok啦

另外有個(gè)拓展要求
使用鼠標(biāo)控制增加球的數(shù)量
光增加怎么行呢,當(dāng)然也得來(lái)一個(gè)減少
那就再init函數(shù)里加入
JButton btn = new JButton("增加一個(gè)小球");
JButton btn1 = new JButton("減少一個(gè)小球");
btn.setBounds(0, 0, 400, 600);
btn1.setBounds(400, 0, 400, 600);
this.add(btn);
this.add(btn1);
btn.addActionListener(new MyListener());
btn1.addActionListener(new MyListener1());
注意畫(huà)布jp一定要加在按鈕的后面
不然是看不見(jiàn)畫(huà)布的
再寫(xiě)倆個(gè)監(jiān)聽(tīng)就行了
class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
jp.addnum(0);
}
}
class MyListener1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
jp.addnum(1);
}
}
傳01方便畫(huà)布那邊檢測(cè)增減
畫(huà)布那邊簡(jiǎn)簡(jiǎn)單單加個(gè)設(shè)置num的函數(shù)就行
public void addnum(int i) {
if(i==0)num++;
else num--;
}
呼,完成了,就是按鈕不時(shí)地會(huì)閃現(xiàn)出來(lái)有點(diǎn)煩,
還有球減到0畫(huà)布可就沒(méi)了
總結(jié)
到此這篇關(guān)于Java中小球碰撞并使用按鈕控制數(shù)量的文章就介紹到這了,更多相關(guān)Java小球碰撞并控制數(shù)量?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java類(lèi)和對(duì)象習(xí)題及詳細(xì)答案解析
這篇文章主要介紹了Java類(lèi)和對(duì)象的相關(guān)知識(shí),包括局部變量初始化、靜態(tài)方法、靜態(tài)導(dǎo)入、構(gòu)造方法、代碼塊執(zhí)行順序、toString方法重寫(xiě)、類(lèi)變量和靜態(tài)成員變量的訪問(wèn)等,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
解決分頁(yè)插件pagehelper在SpringBoot不起作用的問(wèn)題
這篇文章主要介紹了解決分頁(yè)插件pagehelper在SpringBoot不起作用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
深入聊一聊springboot項(xiàng)目全局異常處理那些事兒
最近在做項(xiàng)目時(shí)需要對(duì)異常進(jìn)行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目全局異常處理那些事兒,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
spring項(xiàng)目自定義全局響應(yīng)處理器統(tǒng)一處理響應(yīng)結(jié)果的實(shí)現(xiàn)步驟
本文詳細(xì)描述了如何通過(guò)@ControllerAdvice和ResponseBodyAdvice在SpringMVC項(xiàng)目中創(chuàng)建自定義響應(yīng)處理器,以及如何使用Wrapper類(lèi)包裝和標(biāo)準(zhǔn)化返回結(jié)果,感興趣的朋友跟隨小編一起看看吧2025-01-01
不到十行實(shí)現(xiàn)javaCV圖片OCR文字識(shí)別
識(shí)別圖片中的文字,會(huì)省很多時(shí)間,本文介紹了javaCV圖片OCR文字識(shí)別,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

