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

📄 hexviewerm.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.mapper;
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 HexViewerM extends Panel implements GameViewer{
        private static HexViewerM hexViewer = null;
	private MouseParserM parser;
	private Transform transform = new Transform();
	private ZOrderedList zolist = new ZOrderedList(transform);
	private BufferedImage foreground;
	private Graphics2D grfore;
	public boolean drawguilds = true, drawcolor, drawpass;
        
	private HexViewerM() {
		super();
		parser = new MouseParserM();
		addMouseListener(parser);
		addMouseMotionListener(parser);

		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 HexViewerM getHexViewer() {
		if(hexViewer == null) new HexViewerM();
		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)
                    createBuffer();   // to create a resize event to create the buffers
                
                
                grfore.clearRect(0,0,getWidth(), getHeight());
                HexM.draw(grfore);
		zolist.draw((GameViewer) this);
		g.drawImage(foreground, 0,0,this);
	}
	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 - getWidth(), h - getHeight());
		super.setBounds(x,y,w,h);
                
                createBuffer();
	}
	/** 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); }
	
        public void componentHidden(java.awt.event.ComponentEvent componentEvent) {
        }
        
        public void componentMoved(java.awt.event.ComponentEvent componentEvent) {
        }
        
        public void componentResized(java.awt.event.ComponentEvent e) {
            createBuffer();
        }
        
        public void componentShown(java.awt.event.ComponentEvent componentEvent) {
        }
        private void createBuffer() {
            foreground = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
            grfore = foreground.createGraphics();
            
            grfore.setBackground(Color.black);
            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);

            transform.resize(getWidth() - transform.maxX(), getHeight() - transform.maxY());
            
            System.gc();
        }
        
        public void paintAll(java.awt.Graphics graphics) {
            super.paintAll(graphics);
        }
        
}







⌨️ 快捷键说明

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