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

📄 unitdisplayer.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
字号:
package netwar.gui;
import java.awt.*;
import java.awt.image.BufferedImage;
import netwar.utils.vectorgraphics.*;

/** This class is a GameViewer to be attached to a component.
 * The component it is attached to will be used to display whatever GraphicThings are
 * drawn onto it. This does not support adding GraphicThings, as it does not use a
 * Z-Ordered list. UnitDisplayer has its own image as a buffer to keep the display.
 * Call recycle() to display the buffer onto the Component and clear the buffer.
 */
public class UnitDisplayer implements netwar.game.GameViewer {
	Transform t = new Transform();
	Component myComp;
	BufferedImage image;
	Graphics2D g;
	/** Creates a new instance of UnitDisplayer.
	 * @param comp The Component which the UnitDisplayer will draw upon.
	 * @param maxX The width of the drawing area.
	 * @param maxY The height of the drawing area.
	 */
	public UnitDisplayer(Component comp, int maxX, int maxY) {
		myComp = comp;
		t.resize(maxX - t.maxX(), maxY - t.maxY());
		image = new BufferedImage(maxX,maxY, BufferedImage.TYPE_INT_ARGB);
		g = (Graphics2D)image.createGraphics();
                g.setBackground(new Color(0,0,0,128));
	}
	/** Called once per cycle. Dumps the image to the screen, and clears the buffer */
	public void recycle() {
		Graphics gr = myComp.getGraphics();
		gr.drawImage(image, 0, 0, myComp);
		g.setColor(Color.lightGray);
		g.fillRect(0,0,t.maxX(), t.maxY());
	}	
	public void copyImage(Graphics newG) {
		newG.drawImage(image, 0, 0, myComp);
        }
	
	/** This method calls an Assert and crashes the program.
	 * It is defined in GameViewer, but not really implemented in UnitDisplayer.
	 * A future version may support this feature, if a Z-Ordered list is included.
	 */
	public void add(netwar.utils.vectorgraphics.GraphicThing thing) {
		netwar.utils.Assert.notFalse(false, "This method should not be called: add() in UnitDisplayer(). Use draw() methods instead.");
	}
	public void drawLine(Point3D v1, Point3D v2) {
		Point2D p1 = t.getPoint2D(v1);
		Point2D p2 = t.getPoint2D(v2);
		
		g.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 = t.getPoint2D(v1);
		x[0] = temp.getIntx();
		y[0] = temp.getInty();
		temp = t.getPoint2D(v2);
		x[1] = temp.getIntx();
		y[1] = temp.getInty();
		temp = t.getPoint2D(v3);
		x[2] = temp.getIntx();
		y[2] = temp.getInty();
		
		g.fillPolygon(x,y,3);
	}
	public void drawImage(Point3D vr, Image img) {
		Point2D pt = t.getPoint2D(vr);
		int x = pt.getIntx();
		int y = pt.getInty();
		x -= img.getWidth(myComp) / 2;
		y -= img.getHeight(myComp) / 2;
		
		g.drawImage(img, x, y, myComp);
	}
	public netwar.utils.vectorgraphics.Transform getTransform() {
		return t;
	}
	public void setColor(Color c) {
		g.setColor(c);
	}
	
}

⌨️ 快捷键说明

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