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

📄 hexviewer.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 netwar.game.Hex;
import netwar.game.Projectile;
import netwar.game.GameViewer;
import netwar.utils.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.ComponentListener;
import netwar.utils.vectorgraphics.*;

/** This is the main game view panel which displays what is happening in the game.
 * It uses a MouseParser to allow a point and click interface into the game.
 * @author Group N2 - Project Netwar
 * @author Daniel Grund, Kyle Kakligian, and Jason Komutrattananon
 */
public class HexViewer extends Panel implements /*ImageObserver -panel imlements this ,*/ GameViewer{
        // worthless. use getWidth() ...
	// public static final int maxWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
	// public static final int maxHeight = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
	private static HexViewer hexViewer = null;
	MouseParser parser;
	private Transform transform = new Transform();
	private ZOrderedList zolist = new ZOrderedList(transform);
	private BufferedImage foreground;
        private BufferedImage background;
	private Graphics2D grfore;
	private Graphics2D grback;
	private int framesToDisplayMessage = 0;
	private String messageToDisplay;
        
	private HexViewer() {
		super();
		parser = new MouseParser();
		addMouseListener(parser);
		addMouseMotionListener(parser);
		addKeyListener(KeyParser.getKeyParser());	// HexViewer now listens for Keys pressed

		if(hexViewer == null)
			hexViewer = this;
	}
	/** Returns the one and only HexViewer. Creates it if it doesn't exist.
	 * @return The one and only HexViewer.
	 */
	public static HexViewer getHexViewer() {
		if(hexViewer == null) new HexViewer();
		return hexViewer;
	}
	/** Overrides default update, to prevent flicker.
	 * The default update clears the object, then calls paint.
	 * This just calls paint.
	 * Clearing is unnecessary, and would merely cause flicker,
	 * because paint will draw over the entire screen.
	 * @param g A handle to the graphics object to paint on.
	 * @see paint(Graphics)
	 */
	public void update(Graphics g) {
		paint(g);
	}
	/** Requests all visible aspects of the game to draw in the appropriate order.
	 * This order is as follows:
	 * <BR> Copies background. (Terrain tiles.)
	 * <BR> Lower portion of selection box.
	 * <BR> All GameObjects.
	 * <BR> Upper portion of selection box.
	 * <BR> All Projectiles.
	 * <BR> FPS indicator. (This will be removed soon, and incorporated into the NetwarPanel.
	 */
	public void paint(Graphics g) {
		if(foreground == null)
                    createBuffers();   // to create a resize event to create the buffers
		grfore.drawImage(background,0,0,null); //  clearRect(0,0,maxWidth,maxHeight);
		parser.drawLow();
		zolist.draw((GameViewer) this);
		parser.drawHigh();
                
		grfore.setColor(Color.white);
		FPS.incG();  // we're drawing the screen, FPS needs to know this
		//FPS.draw(grfore);  // draw the numbers onto the BMP/screen
		grfore.drawString(String.valueOf(netwar.game.Player.getLocal().getCash()), getWidth() - 100, 20);
		//put stuff here
		if(framesToDisplayMessage>0){
		    grfore.drawString(messageToDisplay, 10, 60);
		    framesToDisplayMessage--;
		}
		g.drawImage(foreground, 0,0,this);
	}
        public void updateBackground() {
            grback.clearRect(0,0,getWidth(), getHeight());
            Hex.draw(grback);
        }
	public void setColor(Color c) {
		grfore.setColor(c);
	}
	public void drawLine(Point3D v1, Point3D v2) {
		Point2D p1 = transform.getPoint2D(v1);
		Point2D p2 = transform.getPoint2D(v2);

		grfore.drawLine( p1.getIntx(), p1.getInty(), p2.getIntx(), p2.getInty());
	}
	public void drawTriangle(Point3D v1, Point3D v2, Point3D v3)
	{
		int x[] = new int[3];
		int y[] = new int[3];
        
		Point2D temp;
	        temp = transform.getPoint2D(v1);
		x[0] = temp.getIntx();
		y[0] = temp.getInty();
	        temp = transform.getPoint2D(v2);
		x[1] = temp.getIntx();
		y[1] = temp.getInty();
	        temp = transform.getPoint2D(v3);
		x[2] = temp.getIntx();
		y[2] = temp.getInty();
        
		grfore.fillPolygon(x,y,3);
	}
	public void drawImage(Point3D vr, Image img) {
		Point2D pt = transform.getPoint2D(vr);
		int x = pt.getIntx();
		int y = pt.getInty();
		x -= img.getWidth(this) / 2;
		y -= img.getHeight(this) / 2;
		grfore.drawImage(img, x, y, this);
	}

	public Dimension getPreferredSize() {
		return new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()); }
	public Dimension getMinimumSize() {
		return new Dimension(100, 100); }
	/** Captures the new size of the HexViewer, so that the view can be recentered.
	 * This is necessary both for immediate adjustment, and for future adjustments.
	 * Also, this may eventually be used for image drawing optimization,
	 * by not rendering/blitting graphical data which is outside the visible region.
	 * @param x Used only in superclass.
	 * @param y Used only in superclass.
	 * @param w The new width, in pixels, of the HexViewer.
	 * @param h The new height, in pixels, of the HexViewer.
	 */
	public void setBounds(int x, int y, int w, int h) {
		transform.resize(w - transform.maxX(), h - transform.maxY());
		super.setBounds(x,y,w,h);
                
                createBuffers();
	}
	/** This method returns the Transform which defines the game-space to screen-space conversion.
	 * This is used for both Z-Ordering and XY box checking.
	 */
	public Transform getTransform() { return transform; }
	/** This method adds a GraphicThing into the list used by this GameViewer */
	public void add(GraphicThing thing) { zolist.add(thing); }
	
	/**
	 * Writes a string to the HexViewer panel.
	 * @param theString the string to be displayed on screen.
	 */
	public void displayString(String theString){
	    messageToDisplay = theString;
	    framesToDisplayMessage=30;
	}
        public void componentHidden(java.awt.event.ComponentEvent componentEvent) {
        }
        
        public void componentMoved(java.awt.event.ComponentEvent componentEvent) {
        }
        
        public void componentResized(java.awt.event.ComponentEvent e) {
            createBuffers();
        }
        
        public void componentShown(java.awt.event.ComponentEvent componentEvent) {
        }
        private void createBuffers() {
            foreground = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
            background = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
            grfore = foreground.createGraphics();
            grback = background.createGraphics();

            grback.setBackground(Color.black);
            grback.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
            grback.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
            grback.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED);
            grfore.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
            grfore.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
            grfore.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED);

            updateBackground();
            System.gc();
        }
}







⌨️ 快捷键说明

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