java swing 方格地图界面

使用java swing来模拟游戏中方格地图的效果

----------------------------------------------------------------
MainJFrame类
----------------------------------------------------------------
package com.qsuron.map;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import com.qsuron.Ribbon;

public class MainJFrame extends JFrame implements Runnable{

    private static final long serialVersionUID = 5753479529328043063L;
    private static final int FPS = 1000;                    //FPS
    private static final int FRAME_X = 860;                //窗口宽度
    private static final int FRAME_Y = 370;                //窗口高度
    private static final int PIC_SIZE = 71;                //地图格子大小
    private long t1, t2, dt, sleepTime;                    //画面刷新FPS计时用
    private long period = 1000 / FPS;                    //画面刷新FPS计时用

    public Ribbon ribbon = null;
    private Container contentPanel;
    private BufferedImage[] bim = {null,null,null,null,null,null,null,null,null,null};
    public Image offScreenImage = null;                    //缓冲图像
    private int[][] arr = {
            {0,0,7,8,8,9,0,0,0,0,0,0},
            {0,0,1,2,2,3,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0},
            {9,0,0,0,0,0,0,0,7,8,8,9},
            {5,8,9,0,0,7,8,8,5,5,5,6},
    };
    private static final int PIC_X = 12;
    private static final int PIC_Y = 5;

    //界面初始化
    private void jframeInit(){
        this.setTitle("Swing 游戏学习1");
        this.setSize(FRAME_X,FRAME_Y);
        this.setLocation(200,100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPanel = this.getContentPane();
        contentPanel.setLayout(null);
        this.setVisible(true);
        this.gameInit();
        Thread gameThread = new Thread(this);
        gameThread.start();
    }

    //游戏初始化
    private void gameInit() {
        try {
            for(int i=1;i<10;i++){ bim[i]="new" bufferedimage(pic_size,pic_size,bufferedimage.type_int_bgr); } catch (ioexception e) {e.printstacktrace();} 游戏进程更新 private void gameupdate() { 游戏界面刷新 @override public run() t1="System.nanoTime();" while(true){ gameupdate(); repaint(); t2="System.nanoTime();" dt="(t2" - t1) 1000000l; sleeptime="period" dt; if(sleeptime <="0){" try thread.sleep(sleeptime); (interruptedexception static main(string[] args){ mainjframe mjf="new" mainjframe(); mjf.jframeinit(); paint(graphics g) color c="g.getColor();" for(int i="0;i<_PIC_Y;i++){" j="0;j<_PIC_X;j++){" int num="arr[i][j];" system.out.print(num+"|"); if(num="=0" || bim[num]="=null){" continue; g.drawimage(bim[num], j*pic_size, i*pic_size, (j+1)*pic_size, (i+1)*pic_size, 0, pic_size, null); system.out.println(""); g.setcolor(c); update(graphics if(offscreenimage="=null){" offscreenimage="this.createImage(FRAME_X,FRAME_Y);" graphics goff="offScreenImage.getGraphics();" paint(goff); g.drawimage(offscreenimage,0,0,null); pre>
0%