⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minimapviewer.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
字号:
/*
	Netwar
	Copyright (C) 2002  Daniel Grund, Kyle Kakligian, Jason Komutrattananon, & Brian Hibler.
 
	This file is part of Netwar.
 
	Netwar is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
 
	Netwar is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
 
	You should have received a copy of the GNU General Public License
	along with Netwar; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package netwar.gui;
import java.awt.image.BufferedImage;
import netwar.utils.vectorgraphics.*;
import netwar.game.*;
import java.awt.*;
/**
 *
 * @author  Kyle Kakligian
 */
public class MinimapViewer extends javax.swing.JComponent {
    private static boolean inNetBeans = Hex.getRadius() == 0;
    private BufferedImage bmpTiles;
    private BufferedImage bmpObjects;
    boolean validTiles  = false;
    
    public java.awt.Dimension getPreferredSize() {
        if(inNetBeans) return new Dimension(100,100);
        return new Dimension(2 * (Hex.getRadius() * 2 - 1), // width in pixels
                             2 * (Hex.getRadius() * 2 - 1));// height in pixels
    }
    public java.awt.Dimension getMinimumSize() {return getPreferredSize(); }
    public java.awt.Dimension getMaximumSize() {return new Dimension(getPreferredSize().width,10000); }
    
    public void update(java.awt.Graphics g) {
        paint(g);
    }
    
    public void paint(java.awt.Graphics g) {
        if(inNetBeans) {
            g.drawString("Minimap!",5,20);
            return;
        }
        if(!validTiles) drawTiles();
        drawObjects();
        
        g.drawImage(bmpTiles,0,0,null);
        g.drawImage(bmpObjects,0,0,null);
    }
    
    public void setBounds(int x, int y, int w, int h) {
        super.setBounds(x,y,w,h);
        if(inNetBeans) return;
        createBuffers();
    }
    
    private void createBuffers() {
        bmpTiles = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
        bmpObjects = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        
        validTiles = false;
        
        System.gc();
    }
    private static final double sqrt3 = Math.sqrt(3.0);
    private static final Color clear = new Color(0,0,0,64);
    private void drawTiles() {
        int radius = Hex.getRadius();
        Graphics2D g = bmpTiles.createGraphics();
        g.setBackground(Color.black);
        g.clearRect(0,0,getWidth(),getHeight());
        
        for(int i = 0; i < radius; i++) {
            for(int j = radius + i - 1; j >= 0; j--) {
                drawTilesHelper(g, radius, i,j);
            }
        }
        for(int i = radius; i < 2 * radius - 1; i++) {
            for(int j = 2 * (radius - 1); j >= i - (radius - 1); j--) {
                drawTilesHelper(g, radius, i,j);
            }
        }
        validTiles = true;
    }
    private void drawObjects() {
        int radius = Hex.getRadius();
        Graphics2D g = bmpObjects.createGraphics();
        g.setBackground(clear);
        g.clearRect(0,0,getWidth(),getHeight());
        
        for(int i = 0; i < radius; i++) {
            for(int j = radius + i - 1; j >= 0; j--) {
                drawObjectsHelper(g, radius, i,j);
            }
        }
        for(int i = radius; i < 2 * radius - 1; i++) {
            for(int j = 2 * (radius - 1); j >= i - (radius - 1); j--) {
                drawObjectsHelper(g, radius, i,j);
            }
        }
    }
    private void drawObjectsHelper(Graphics2D g, int r, int i, int j) {
        if(Hex.getHex(i,j).getOccupant() != null) {
            int x = 2*i;
            // (center of bmp)-(half height of hex)+(heigth of hex - hex's y pos)+(rotate 45 degrees)
            // (getHeight()/2)-(2r-1              )+(4r-2          - 2j         )+(i-r)
            int y = r - 1 - 2*j + i + getHeight()/2;
            g.setColor(Hex.getHex(i,j).getOccupant().getMinimapColor());
            g.drawRect(x,y,0,0);
        }
    }
    private void drawTilesHelper(Graphics2D g, int r, int i, int j) {
        int x = 2*i;
        // (center of bmp)-(half height of hex)+(heigth of hex - hex's y pos)+(rotate 45 degrees)
        // (getHeight()/2)-(2r-1              )+(4r-2          - 2j         )+(i-r)
        int y = r - 1 - 2*j + i + getHeight()/2;
        g.setColor(Hex.getHex(i,j).getMinimapColor());
        g.drawRect(x,y,1,1);
    }

    public void updateTiles() {
        validTiles = false;
    }
    /** Creates a new instance of MinimapViewer */
    public MinimapViewer() {
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -