飞机小游戏

飞机小游戏

rgb 全0 黑色   全255白色

 

 

在窗口画图形  字体设置  颜色设置

package MyGame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame  extends Frame{
    private String title="第一个窗口";
    /**
     * 加载窗口
     */
    void run(){
        setSize(500, 500);
        setLocation(200, 100);
        setTitle(title);
        setVisible(true);
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
            System.exit(0);
            }
            
        });
    }
    
    @Override
    public void paint(Graphics g) {
        g.drawLine(100, 100, 200, 200);
        g.drawLine(100, 100, 100, 100);
        g.drawRect(100, 100, 100, 100);
        g.drawOval(100, 100, 100, 100);
        g.fillRect(100, 100, 20,20);
        Color a=g.getColor();
        g.setColor(Color.blue);
        g.fillOval(200, 200, 20, 20);
//        g.setColor(a);
        g.fillRect(80, 80, 20,20);
        Font f=new Font("宋体", Font.BOLD, 100);
        g.setFont(f);
        g.drawString("欢迎光临", 100, 100);
    }

    public static void main(String[] args) {
        MyFrame a=new MyFrame();
        a.run();
    }
}

 

工具类    加载图片类

 

package MyGame;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
/**
 * 游戏开发中常用的工具类   比如图片加载等   工具类中方法一般是static方法
 * @author Administrator
 *
 */
public class GameUtil {
    public static Image getImage(String path){
        URL u=GameUtil.class.getClassLoader().getResource(path);
        BufferedImage img=null;
        try {
            img=ImageIO.read(u);
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return img;
    }
}