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

📄 objectcomponent.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
字号:
/*
 * TileComponent2.java
 *
 * Created on March 4, 2002, 2:22 AM
 */

package netwar.mapper;
import netwar.game.*;
import netwar.gui.UnitDisplayer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import netwar.utils.*;

/** Extends AWT Component to draw a GameObject on the component
 * @author Kyle Kakligian
 */
public class ObjectComponent extends Component implements MouseListener {
    /** currently selected GameObject
     */    
    public static ObjectComponent curr;
    public GameObject gameObject;
    public String fileName;
    public UnitDisplayer uDisp;
    
    /** Loads a new GameObject
     * @param filename path to the gameobject
     */    
    public ObjectComponent(String filename) {
        try {gameObject = (GameObject)(Class.forName(filename).newInstance());
        } catch(Exception e) {
            System.out.println("Exception caught in ObjectComponent.ObjectComponent(). Probably a bad unit name.");
            e.printStackTrace();
            System.exit(1);
        };
        fileName = filename;

        GameObject.newGameObjectMnohex(gameObject,3);
        
        addMouseListener(this);
    }

    /** creates a new GameObject of the same type as <CODE>curr</CODE>.
     * @return new GameObject
     */    
    public static GameObject newSelectedObject() {
        GameObject ret = null;
        
        try {ret = (GameObject)(Class.forName(curr.fileName).newInstance());
        } catch(Exception e) {
            // don't worry, either a bad file name or no 'curr'. return null
        };
        return ret;
    }
    public Dimension getMinimumSize() {return getPreferredSize();}    public java.awt.Dimension getMaximumSize() {return getPreferredSize();} public java.awt.Dimension getPreferredSize() {return new Dimension(100, 100);}
    
    public void paint(java.awt.Graphics g) {
        if(uDisp == null) {
            uDisp = new UnitDisplayer(this, getWidth(), getHeight());
            gameObject.center(uDisp.getTransform());
            gameObject.draw(uDisp);
        }

        //gameObject.center(uDisp.getTransform());
        //gameObject.draw(uDisp);
        uDisp.copyImage(g);
        
        if(this == curr) {
            g.setColor(Color.blue);
            g.drawRect(0,0,getWidth(),getHeight());
            g.drawRect(1,1,getWidth()-2,getHeight()-2);
            g.drawRect(2,2,getWidth()-4,getHeight()-4);
        }
    }
    
    public void setBounds(int x, int y, int w, int h) {
        if(w>getPreferredSize().width)
            w = getPreferredSize().width;
        if(h>getPreferredSize().height)
            h = getPreferredSize().height;
        super.setBounds(x,y,w,h);
    }
	/** Required by MouseListener, but not currently used.*/
	public void mouseClicked(MouseEvent e) {
	}
	/** Catches and interprets a mouse click.
	 * @param e The MouseEvent containing all the relevant click data.
	 */
	public void mouseReleased(MouseEvent e) {
            curr = this;
            netwar.Mapper.rightPanel.getList().updateUI(); // ugly hack
        }
	/** Required by MouseListener, but not currently used.*/
	public void mousePressed(MouseEvent e) {
	}
	/** Required by MouseListener, but not currently used.*/
	public void mouseExited(MouseEvent e) {
	}
	/** Required by MouseListener, but not currently used.*/
	public void mouseEntered(MouseEvent e) {
	}
        
        public void update(java.awt.Graphics graphics) {
            paint(graphics);
        }
        
}

⌨️ 快捷键说明

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