各种曲线运动、弹球、笔记
// 绘制曲线 import java.awt.*; public class BallGame extends Frame { Image img = Toolkit.getDefaultToolkit().getImage("pingpang.jpg"); // 这种写法的问题就是开始时静态图像不会显示,需要最小化或最大化后才能显示 double x = 200; double y = 200; double degree = 0; public void paint(Graphics g) // 自动调用, g相当于画笔 { System.out.println("窗口被画了一次"); g.drawImage(img, (int)x, (int)y, null); // 椭圆 /*x = 250 + 200*Math.cos(degree); y = 250 + 100*Math.sin(degree);*/ // 正弦曲线 x = x + 2; y = 200 + 100*Math.sin(degree); degree = degree + 0.1; // g.drawImage(img, 100, 100, null); /*g.setColor(Color.GREEN); g.drawLine(100, 100, 200, 200); g.drawRect(50, 50, 100, 80); g.drawOval(50, 50, 100, 80); g.setColor(Color.YELLOW); g.drawString("游戏开始!", 80, 80);*/ } void launchFrame() { setSize(500, 300); setLocation(50, 50); setTitle("hallo ball"); setBackground(Color.blue); setVisible(true); new PaintThread().start(); } public static void main(String[] args) { new BallGame().launchFrame(); } class PaintThread extends Thread { public void run() { while(true) { repaint(); // 重画窗口! try { Thread.sleep(40); // 40MS, 1S画25次 } catch(Exception e) { e.printStackTrace(); } } } } } //------------------------------我是分割线--------------------------------------------------------------- 弹球: import java.awt.*; public class BallGame extends Frame { Image img = Toolkit.getDefaultToolkit().getImage("pingpang.png"); // 这种写法的问题就是开始时静态图像不会显示,需要最小化或最大化后才能显示 double x = 100; double y = 100; double degree = 3.14 / 3; public void paint(Graphics g) // 自动调用, g相当于画笔 { System.out.println("窗口被画了一次"); g.drawImage(img, (int)x, (int)y, null); if(y > 300 - 30) { degree = -degree; } else if(y < 30) { degree = - degree; } else if(x > 500 - 30) { degree = 3.14 - degree; } else if(x < 0) { degree = 3.14 - degree; } x = x + 10*Math.cos(degree); y = y + 10*Math.sin(degree); // g.drawImage(img, 100, 100, null); /*g.setColor(Color.GREEN); g.drawLine(100, 100, 200, 200); g.drawRect(50, 50, 100, 80); g.drawOval(50, 50, 100, 80); g.setColor(Color.YELLOW); g.drawString("游戏开始!", 80, 80);*/ } void launchFrame() { setSize(500, 300); setLocation(50, 50); setTitle("hallo ball"); setBackground(Color.blue); setVisible(true); new PaintThread().start(); } public static void main(String[] args) { new BallGame().launchFrame(); } class PaintThread extends Thread { public void run() { while(true) { repaint(); // 重画窗口! try { // Thread.sleep(40); // 40MS, 1S画25次 Thread.sleep(40); } catch(Exception e) { e.printStackTrace(); } } } } } //----------------------------我是分割线-------------------------------------------------- 笔记: 1.JAVA开发环境安装和配置 a)下载JDK(Java Development Kit) b)安装JDK、 JRE(Java Runtime environment) c)配置环境变量: i.path(系统通过path指定的目录寻找相关的可执行文件!) 右键单击我的电脑属性高级环境变量修改path即可! ii.classpath(系统通过classpath寻找类文件) JDK1.5之后,可以不设置! 2.Eclipse的使用(目前使用最多的IDE:integrated development environment集成开发环境) a)下载 b)解压即可 3.新建一个JAVA项目 a)New Java Project只需要输入project name,回车即可! 4.新建一个JAVA类 a)右键单击srcnew Class键入类名:BallGame b)输入代码: public class BallGame { public static void main(String[] args){ System.out.println("我是尚学堂的高淇!"); } } c)运行java程序的三种方式: i.右键单击Run asjava application ii.Alt+shift+X,再按J iii.Ctrl+F11 iv.单击运行按钮 开始开发我们的小游戏 1.增加窗口 import java.awt.*; public class BallGame extends Frame { void launchFrame(){ setSize(500, 300); setLocation(50, 50); setVisible(true); setTitle("尚学堂---张三作品"); setBackground(Color.black); } public static void main(String[] args){ System.out.println("我是尚学堂的高淇!"); new BallGame().launchFrame(); } } 2.加载图片 在项目下新建images文件夹,将sun.jpg拷贝到images下面 import java.awt.*; public class BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的! public void paint(Graphics g){ g.drawImage(sun, 100, 100, null); } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("尚学堂---张三作品"); setBackground(Color.black); setVisible(true); } public static void main(String[] args){ System.out.println("我是尚学堂的高淇!"); new BallGame().launchFrame(); } } 注意:运行时,第一次打开窗口看不到图片。需要将窗口最小化再打开即可看到! 3.学习画各种曲线、形状、字符串: import java.awt.*; public class BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的! public void paint(Graphics g){ g.drawImage(sun, 100, 100, null); g.setColor(Color.blue); g.drawLine(100, 100, 200, 200); g.drawRect(50, 50, 100, 80); g.drawOval(50, 50, 100, 80); g.setColor(Color.yellow); g.drawString("游戏开始啦!!", 80, 80); [暂时跟我们的游戏无关,但是一些基本的编程知识!] } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("尚学堂---张三作品"); setBackground(Color.black); setVisible(true); } public static void main(String[] args){ System.out.println("我是尚学堂的高淇!"); new BallGame().launchFrame(); } } 4.增加动画 import java.awt.*; public class BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的! int x=100; int y=100; public void paint(Graphics g){ System.out.println("窗口被画了一次!"); g.drawImage(sun, x, y, null); x = x+2; } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("尚学堂---张三作品"); setBackground(Color.black); setVisible(true); new PaintThread().start(); } public static void main(String[] args){ System.out.println("我是尚学堂的高淇!"); new BallGame().launchFrame(); } class PaintThread extends Thread { public void run(){ while(true){ repaint(); //重画窗口! try{ Thread.sleep(40); //40ms 1s=1000ms }catch (Exception e) { e.printStackTrace(); } } } } } 5.通过我们掌握的数学函数(抛物线、正弦曲线、椭圆等),控制游戏中物体的运动! import java.awt.*; public class BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的! double x=100; double y=100; double degree = 0; public void paint(Graphics g){ System.out.println("窗口被画了一次!"); g.drawImage(sun, (int)x,(int)y, null); x =150+ 100*Math.cos(degree); y = 150+100*Math.sin(degree); degree = degree + 0.1; } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("尚学堂---张三作品"); setBackground(Color.black); setVisible(true); new PaintThread().start(); } public static void main(String[] args){ System.out.println("我是尚学堂的高淇!"); new BallGame().launchFrame(); } class PaintThread extends Thread { public void run(){ while(true){ repaint(); //重画窗口! try{ Thread.sleep(40); //40ms 1s=1000ms }catch (Exception e) { e.printStackTrace(); } } } } } 6.实现台球的运动! import java.awt.*; public class BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的! double x=100; double y=100; double degree = 3.14/3; public void paint(Graphics g){ System.out.println("窗口被画了一次!"); g.drawImage(sun, (int)x,(int)y, null); x = x+ 10*Math.cos(degree); y = y+10*Math.sin(degree); if(y>300-30){ degree = - degree; } if(x>500-30){ degree = 3.14-degree; } if(x<0){ degree = 3.14-degree; } if(y<30){ degree = -degree; } } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("尚学堂---张三作品"); setBackground(Color.black); setVisible(true); new PaintThread().start(); } public static void main(String[] args){ System.out.println("我是尚学堂的高淇!"); new BallGame().launchFrame(); } class PaintThread extends Thread { public void run(){ while(true){ repaint(); //重画窗口! try{ Thread.sleep(40); //40ms 1s=1000ms }catch (Exception e) { e.printStackTrace(); } } } } } 7.通过键盘来控制物体的运动!
参考:尚学堂高淇游戏视频