弹弹球 ,我的java学习之路

本文详细介绍了如何使用Java编程实现弹球游戏,并通过调整游戏元素如延迟、颜色、速度等来优化游戏体验。文章深入探讨了游戏内部机制,包括球的运动规律、碰撞处理及界面布局设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cn.hncu.thead;


import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;


import javax.swing.Timer;//注意要导这个包


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class BallJFrame extends JFrame implements ChangeListener {
private BallCanvas ball;
private JSpinner spinner;
public BallJFrame(){
super("弹弹球");
Dimension dim=getToolkit().getScreenSize();//获得屏幕尺寸
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置退出
    this.setBounds(dim.width/4, dim.height/4, dim.width/2, dim.height/2);//设置大小尺寸
    Color color[]={ Color.red,Color.green,Color.blue,Color.magenta,Color.cyan};
    ball=new BallCanvas(color,100);
    this.getContentPane().add(ball);
    JPanel p=new JPanel();
    p.add(new JLabel("Delay"));
    spinner=new JSpinner();
    spinner.setValue(50);
    p.add(spinner);
    spinner.addChangeListener(this);
    this.getContentPane().add(p,BorderLayout.SOUTH);
    this.setVisible(true);    
}



public static void main(String[] args) {
new BallJFrame();
    }




@Override
public void stateChanged(ChangeEvent e) {
try {
int values=Integer.parseInt(""+spinner.getValue());
ball.setDelay(values);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "请重新输入");
}

}


}
class BallCanvas extends Canvas implements FocusListener, ActionListener{
private Timer timer;
private Ball balls[];

public BallCanvas(Color[] color, int delay) {
this.balls=new Ball[color.length];
for(int i=0, x=40;i<balls.length;i++,x+=40){
balls[i]=new Ball(x, x, color[i]);
}
this.addFocusListener(this);//焦点
timer=new Timer(delay, this);//定时器
timer.start();
}
@Override
public void paint(Graphics g) {
for(int i=0;i<balls.length;i++){
g.setColor(balls[i].color);

//让每个球的坐标变化一下(球动一下)----x坐标
balls[i].x=balls[i].left?balls[i].x-10:balls[i].x+10;
//当球碰壁时,更改球的方向
if(balls[i].x>this.getWidth()-10||balls[i].x<=10){
balls[i].left=!balls[i].left;//切换
}
balls[i].y=balls[i].up?balls[i].y-10:balls[i].y+10;
//当球碰壁时,更改球的方向
if(balls[i].y>this.getHeight()-10||balls[i].y<=10){
balls[i].up=!balls[i].up;//切换
}
g.fillOval(balls[i].x, balls[i].y, 30, 30);
}
}




public void setDelay(int values) {
timer.setDelay(values);

}







public class Ball{
int x;
int y;
Color color;
boolean up ,left;
public Ball(int x, int y, Color color) {
super();
this.x = x;
this.y = y;
this.color = color;
}

}








@Override
public void focusGained(FocusEvent e) {
timer.restart();

}
@Override
public void focusLost(FocusEvent e) {
timer.stop();

}
@Override
public void actionPerformed(ActionEvent e) {
repaint();

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值