Java开发

java swing 方格地图界面

页面
字体
小树 · 2月21日 · 2014年 ·

使用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);
				bim[i] = ImageIO.read(getClass().getResource(i+".png"));
			}
		} catch (IOException e) {e.printStackTrace();}
		
	}
	
	//游戏进程更新
	private void gameUpdate() {
	}
	
	//游戏界面刷新
	@Override
	public void run() {
		t1 = System.nanoTime();
		while(true){
			gameUpdate();
			repaint();
			t2 = System.nanoTime();
			dt = (t2 - t1) / 1000000L;
			sleepTime = period - dt;
			if(sleepTime <= 0){
				sleepTime = 2;
			}
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {e.printStackTrace();}
			t1 = System.nanoTime();
		}
	}

	public static void main(String[] args){
		MainJFrame mjf = new MainJFrame();
		mjf.jframeInit();
	}

	@Override
	public void paint(Graphics g) {
		Color c = g.getColor();
		for(int i=0;i<_PIC_Y;i++){
			for(int 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, 0, PIC_SIZE, PIC_SIZE, null);
			}
			//System.out.println("");
		}
		g.setColor(c);
	}

	@Override
	public void update(Graphics g) {
		if(offScreenImage==null){
			offScreenImage = this.createImage(FRAME_X,FRAME_Y);
		}
		Graphics goff = offScreenImage.getGraphics();
		paint(goff);
		g.drawImage(offScreenImage,0,0,null);
	}
	

}

转载必须注明来源: 小树技术博客 » java swing 方格地图界面

8 条回应