RGB(红绿蓝)0-255 全0就是黑色,全是最高255就是白色。 //第一步类 package cn.bjsxt.test; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * 游戏基本知识测试类 * @author dell * */ public class GameFrame extends Frame {//GUI编程:AWT,swing等,AWT是落后了的,AWT不用学不是重点。java不擅长写桌面(windows)软件,擅长写手机软件和服务器端软件。 Image img = GameUtil.getImage("images/sun.jpg"); /** * 加载窗口 */ public void launchFrame(){ setSize(1000, 500);//画窗口要定义宽度和高度, setLocation(100, 100);//定义位置,窗口的左上角的坐标(相对于window桌面的左上角) setVisible(true);//窗口可见了 new PaintThread().start(); //启动重画的线程 //增加一个窗口监听 addWindowListener(new WindowAdapter() {//WindowAdapter是匿名内部类,申明和使用在一起。 @Override public void windowClosing(WindowEvent e) {//匿名内部类里面重写方法 System.exit(0);//结束当前运行虚拟机 } }); } private double x=100,y=100; @Override public void paint(Graphics g) {//在窗口画东西要重写父类的方法,g就是一支笔一样。 g.drawLine(100, 100, 200, 200);//画线,起点和终点坐标(这是以窗口的左上角为起点的,而这个窗口的坐标是以window桌面的左上角为起点的) g.drawRect(100, 100, 200, 200);//画矩形 g.drawOval(100, 100, 200, 200);//画圆 Font f =new Font("楷体",Font.BOLD,100); g.setFont(f); g.drawString("我是尚学堂高琪", 200, 200);//画一个字符串 g.fillRect(100, 100, 20, 20);//画一个实心矩形 Color c = g.getColor();//保存原来的颜色 g.setColor(Color.red);//改画笔的颜色 g.fillOval(300, 300, 20, 20); g.setColor(c);//改完之后又回复之前的颜色 g.drawImage(img, (int)x, (int)y, null);//图图片,图片画的坐标。 x += 3; y += 3; } /** * 定义一个重画窗口的线程类,是一个内部类 * @author dell * */ class PaintThread extends Thread { public void run(){//重写父类的方法 while(true){//一直画,直到程序关掉 repaint(); try { Thread.sleep(10); //1s = 1000ms,歇一会,不然cpu撑不住。 } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { GameFrame gf = new GameFrame(); gf.launchFrame(); } } package cn.bjsxt.test; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; public class GameUtil { private GameUtil(){} //工具类最好将构造器私有,里面的方法是静态方法。这样就不会new出一个对象,而是直接调用静态方法。 public static Image getImage(String path){ BufferedImage bi=null; try { URL u = GameUtil.class.getClassLoader().getResource(path);//返回url对象 System.out.println(u); bi = javax.imageio.ImageIO.read(u); } catch (IOException e) { e.printStackTrace(); } return bi;//BufferedImage是Image的子类,所以可以返回。 } }
package cn.bjsxt.solar; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import cn.bjsxt.util.GameUtil; public class Planet extends Star { //除了图片,坐标。 行星沿着某个椭圆运行:长轴、短轴、速度、角度。 绕着某个Star飞。 double longAxis; //椭圆的长轴 double shortAxis; //椭圆的短轴 double speed; //飞行的速度 double degree; Star center; boolean satellite; public void draw(Graphics g){ super.draw(g); move(); if(!satellite){ drawTrace(g); } } public void drawTrace(Graphics g){ double ovalX,ovalY,ovalWidth,ovalHeight; ovalWidth = longAxis*2; ovalHeight = shortAxis*2; ovalX = (center.x+center.width/2)-longAxis; ovalY = (center.y+center.height/2)-shortAxis; Color c =g.getColor(); g.setColor(Color.blue); g.drawOval((int)ovalX, (int)ovalY, (int)ovalWidth, (int)ovalHeight); g.setColor(c); } public void move(){ //沿着椭圆轨迹飞行 x = (center.x+center.width/2) + longAxis*Math.cos(degree); y = (center.y+center.height/2)+ shortAxis*Math.sin(degree); degree += speed; } public Planet(Star center,String imgpath, double longAxis, double shortAxis, double speed) { super(GameUtil.getImage(imgpath)); this.center = center; this.x = center.x + longAxis; this.y = center.y; this.longAxis = longAxis; this.shortAxis = shortAxis; this.speed = speed; this.width = img.getWidth(null); this.height = img.getHeight(null); } public Planet(Star center,String imgpath, double longAxis, double shortAxis, double speed,boolean satellite) { this(center, imgpath, longAxis, shortAxis, speed); this.satellite = satellite; } public Planet(Image img, double x, double y) { super(img, x, y); } public Planet(String imgpath, double x, double y) { super(imgpath, x, y); } } package cn.bjsxt.solar; import java.awt.Graphics; import java.awt.Image; import cn.bjsxt.util.Constant; import cn.bjsxt.util.GameUtil; import cn.bjsxt.util.MyFrame; /** * 太阳系的主窗口 * @author dell * */ public class SolarFrame extends MyFrame { Image bg = GameUtil.getImage("images/bg.jpg"); Star sun = new Star("images/sun.jpg", Constant.GAME_WIDTH/2, Constant.GAME_HEIGHT/2); Planet earth = new Planet(sun, "images/earth.jpg", 150, 100, 0.1); Planet moon = new Planet(earth, "images/moon.jpg", 30, 20, 0.3,true); Planet mars = new Planet(sun, "images/Mars.jpg", 200, 130, 0.2); public void paint(Graphics g){ g.drawImage(bg, 0, 0, null); sun.draw(g); earth.draw(g); mars.draw(g); moon.draw(g); } public static void main(String[] args) { new SolarFrame().launchFrame(); } } package cn.bjsxt.solar; import java.awt.Graphics; import java.awt.Image; import cn.bjsxt.util.GameUtil; public class Star { Image img; double x,y; int width,height; public void draw(Graphics g){ g.drawImage(img, (int)x, (int)y, null); } public Star(){ } public Star(Image img){ this.img = img; this.width = img.getWidth(null); this.height = img.getHeight(null); } public Star(Image img,double x,double y){ this(img); this.x = x; this.y = y; } public Star(String imgpath,double x,double y){ this(GameUtil.getImage(imgpath), x, y); //通过this调用另一个构造方法 } } package cn.bjsxt.util; /** * 游戏项目中用到的常量 * @author dell * */ public class Constant { public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; } package cn.bjsxt.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; public class GameUtil { private GameUtil(){} //工具类最好将构造器私有了。 public static Image getImage(String path){ BufferedImage bi=null; try { URL u = GameUtil.class.getClassLoader().getResource(path); System.out.println(u); bi = javax.imageio.ImageIO.read(u); } catch (IOException e) { e.printStackTrace(); } return bi; } } package cn.bjsxt.util; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { /** * 加载窗口 */ public void launchFrame(){ setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT); setLocation(100, 100); setVisible(true); new PaintThread().start(); //启动重画线程 addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } /** * 定义一个重画窗口的线程类,是一个内部类 * @author dell * */ class PaintThread extends Thread { public void run(){ while(true){ repaint(); try { Thread.sleep(40); //1s = 1000ms } catch (InterruptedException e) { e.printStackTrace(); } } } } }
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4833545.html,如需转载请自行联系原作者