国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Java實(shí)力彈彈球?qū)崿F(xiàn)代碼

2019-11-26 13:57:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

先看看效果圖:

直接上代碼了。
微調(diào)按鈕加畫(huà)布畫(huà)幾個(gè)圓,再實(shí)現(xiàn)監(jiān)聽(tīng)。。。

package cn.hncu.threadDemo.thread2;import java.awt.Canvas;import java.awt.Color;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JSpinner;import javax.swing.Timer;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;public class BallsJFrame extends JFrame implements ChangeListener{  private BallsCanvas ball;  private JSpinner spinner;  public BallsJFrame(){    super("彈彈球");    this.setBounds(300, 200, 400, 300);    this.setDefaultCloseOperation(EXIT_ON_CLOSE);    Color colors[] = {Color.red,Color.green,Color.blue,Color.magenta,Color.cyan};    ball = new BallsCanvas(colors,100);    this.getContentPane().add(ball);//默認(rèn)是CENTER位置    JPanel panel = new JPanel();    this.getContentPane().add(panel,"South");    panel.add(new JLabel("Delay"));    spinner = new JSpinner();    spinner.setValue(100);    panel.add(spinner);    spinner.addChangeListener(this);    this.setVisible(true);  }  @Override  public void stateChanged(ChangeEvent e) {    int value = Integer.parseInt(""+spinner.getValue());    ball.setDelay(value);  }  public static void main(String[] args) {    new BallsJFrame();  }}class BallsCanvas extends Canvas implements ActionListener, FocusListener{  private Ball balls[];//存放所有的球  private Timer timer;//javax.swing.Timer  public BallsCanvas(Color colors[] ,int delay){    this.balls = new Ball[colors.length];    for(int i=0,x=40;i<colors.length;i++,x+=20){      this.balls[i] = new Ball(x,x,colors[i]);    }    //讓當(dāng)前畫(huà)布監(jiān)聽(tīng) 焦點(diǎn)事件    this.addFocusListener(this);    timer = new Timer(delay,this);    timer.start();  }  public void setDelay(int delay){    timer.setDelay(delay);  }  @Override  public void paint(Graphics g) {    for(int i=0;i<this.balls.length;i++){      g.setColor(balls[i].color);      //讓每個(gè)球的坐標(biāo)變化一下---(x坐標(biāo))      balls[i].x = balls[i].left ? balls[i].x-10:balls[i].x+10;      //當(dāng)球碰壁時(shí),更改球的方向      if(balls[i].x<=0||balls[i].x>=this.getWidth()-24){        balls[i].left = !balls[i].left;//切換方向      }      //讓每個(gè)球的坐標(biāo)變化一下---(y坐標(biāo))      balls[i].y = balls[i].up ? balls[i].y-10:balls[i].y+10;      //當(dāng)球碰壁時(shí),更改球的方向      if(balls[i].y<=0||balls[i].y>=this.getHeight()-22){        balls[i].up = !balls[i].up;//切換方向      }      g.fillOval(balls[i].x, balls[i].y, 20, 20);    }  }  @Override  public void actionPerformed(ActionEvent e) {    //System.out.println("aaa");    repaint();//刷新畫(huà)布.調(diào)用paint(Graphics g)  }  @Override  public void focusGained(FocusEvent e) {    timer.stop();  }  @Override  public void focusLost(FocusEvent e) {    timer.restart();  }  private static class Ball{    int x,y;    boolean up,left;    Color color;    public Ball(int x, int y, Color color) {      this.x = x;      this.y = y;      this.color = color;      up = left = false;    }  }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 婺源县| 湘西| 美姑县| 都安| 乌兰察布市| 碌曲县| 临海市| 讷河市| 仪征市| 大化| 崇州市| 开阳县| 财经| 昂仁县| 谢通门县| 巴塘县| 樟树市| 通榆县| 高雄市| 郁南县| 三都| 雅安市| 方城县| 海口市| 汉沽区| 黎城县| 通海县| 绥滨县| 海南省| 丹棱县| 瓦房店市| 湘潭市| 新泰市| 瑞昌市| 吉林省| 宁远县| 陇西县| 同江市| 大埔县| 册亨县| 左云县|