练习目标
- 学会调试错误
- 掌握Java代码基本结构
项目需求
- 球在球桌中按照一定线路和角度移动;遇到边框自动弹回
实现步骤
- 创建项目和窗口
- 加载两张图片
- 实现动画,小桌球沿着水平方向移动并作边界检测
- 实现小球沿着任意角度飞行
代码
1.创建窗口
import java.awt.*;
import javax.swing.*;
public class BallGame extends JFrame {
//创建窗口
void launchFrame(){
setSize(856,501);
setLocation(100,100);//窗口出现位置的坐标
setVisible(true);
}
public static void main(String[] args){
System.out.println("Time to play!");
BallGame game = new BallGame();
game.launchFrame();
}
}
运行结果:
2.加载出图片
import java.awt.*;
import javax.swing.*;
public class BallGame extends JFrame {
Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");
//绘制窗口
public void paint(Graphics g){
System.out.println("窗体被画了一次!");
g.drawImage(desk,0,0,null);
g.drawImage(ball,200,200,null);
}
//创建窗口
void launchFrame(){
setSize(856,501);
setLocation(100,100);//窗口出现位置的坐标
setVisible(true);
}
public static void main(String[] args){
System.out.println("Time to play!");
BallGame game = new BallGame();
game.launchFrame();
}
}
运行结果:
注意: 由于懒加载问题,有可能出现第一次加载图片无效的情况;请最小化窗口再打开即可。
3.实现动画,小球沿着水平方向移动并做边界检测
import java.awt.*;
import javax.swing.*;
public class BallGame extends JFrame {
Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");
double x = 200;
double y = 200;
boolean right = true; //控制小球的水平位移方向
//绘制窗口
public void paint(Graphics g){
System.out.println("窗体被画了一次!");
g.drawImage(desk,0,0,null);
g.drawImage(ball,(int) x,(int)y,null);
if(right){
x=x+10;
}else {
x=x-10;
}
//边界检测
if(x>856-30-40){
right = false;
}
if(x<0+40){
right = true;
}
}
//创建窗口
void launchFrame(){
setSize(856,501);
setLocation(100,100);//窗口出现位置的坐标
setVisible(true);
//实现动画,每秒绘制窗口25次
while(true){
repaint();//重新绘制窗口
try {
Thread.sleep(40); //1s = 1000ms;大约1秒绘制1000/40 = 25次
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args){
System.out.println("Time to play!");
BallGame game = new BallGame();
game.launchFrame();
}
}
4.实现小球沿着任意角度飞行
import java.awt.*;
import javax.swing.*;
public class BallGame extends JFrame {
Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");
double x = 200;
double y = 200;
double degree = 3.14/3; //弧度,3.14=180°,degree=60°
//绘制窗口
public void paint(Graphics g){
System.out.println("窗体被画了一次!");
g.drawImage(desk,0,0,null);
g.drawImage(ball,(int) x,(int)y,null);
x = x + 10*Math.cos(degree);
y = y + 10*Math.sin(degree);
//碰到上下边界
if(y>501-40-30||y<0+40+40){
degree = -degree;
}
//碰到左右边界
if(x>856-40-30||x<0+40){
degree = 3.14-degree;
}
}
//创建窗口
void launchFrame(){
setSize(856,501);
setLocation(100,100);//窗口出现位置的坐标
setVisible(true);
//实现动画,每秒绘制窗口25次
while(true){
repaint();//重新绘制窗口
try {
Thread.sleep(40); //1s = 1000ms;大约1秒绘制1000/40 = 25次
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args){
System.out.println("Time to play!");
BallGame game = new BallGame();
game.launchFrame();
}
}