图片准备
请将以下两张图片拷贝到代码对应路径下
代码示例
package *;
import javax.swing.*;
import java.awt.*;
/**
* @program: simple_tools
* @description: Java游戏台球
* @author: ChenWenLong
* @create: 2019-11-05 14:15
**/
public class BallGame extends JFrame {
Image ball = Toolkit.getDefaultToolkit().getImage("D:\\ball.png");
Image desk = Toolkit.getDefaultToolkit().getImage("D:\\desk.jpg");
double x=100; //小球的横坐标
double y=100; //小球的纵坐标
//画窗口的方法
public void paint(Graphics g){
g.drawImage(desk, 0, 0, null);
g.drawImage(ball, (int)x, (int)y, null);
}
//窗口加载
void launchFrame(){
setSize(856,500);
setLocation(50,50);
setVisible(true);
}
//main方法是程序执行的入口
public static void main(String[] args) throws InterruptedException {
BallGame game = new BallGame();
game.launchFrame();
Thread.sleep(3000);
System.exit(0);
}
}