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

📄 tilesetmanager.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                // load all mech images        for (Enumeration i = game.getEntities(); i.hasMoreElements();) {            loadImage((Entity) i.nextElement());        }                // load minefield sign        minefieldSign = comp.getToolkit().getImage(Minefield.IMAGE_FILE);                // load night overlay        nightFog = comp.getToolkit().getImage(NIGHT_IMAGE_FILE);                //load artillery targets        artilleryAutohit = comp.getToolkit().getImage(ARTILLERY_AUTOHIT_IMAGE_FILE);        artilleryAdjusted = comp.getToolkit().getImage(ARTILLERY_ADJUSTED_IMAGE_FILE);        artilleryIncoming = comp.getToolkit().getImage(ARTILLERY_INCOMING_IMAGE_FILE);        started = true;    }    /**     * Loads the image(s) for this hex into the tracker.     *     * @param hex the hex to load     */    private synchronized void loadHexImage(IHex hex) {        hexTileset.assignMatch(hex, comp);        hexTileset.trackHexImages(hex, tracker);    }    /**     * Removes the hex images from the cache.     *     * @param hex     */    public void clearHex(IHex hex) {        hexTileset.clearHex(hex);    }    /**     * Waits until a certain hex's images are done loading.     *     * @param hex the hex to wait for     */    public synchronized void waitForHex(IHex hex) {        loadHexImage(hex);        try {            tracker.waitForID(1);        } catch (InterruptedException e) {            e.printStackTrace();        }    }    /**     * Loads all the hex tileset images     */    public synchronized void loadAllHexes() {        hexTileset.loadAllImages(comp, tracker);    }    // Loads a preview image of the unit into the BufferedPanel.    public void loadPreviewImage(Entity entity, Image camo, int tint,                                 JLabel bp) {        Image base = mechTileset.imageFor(entity, comp);        EntityImage entityImage = new EntityImage(base, tint, camo, bp);        Image preview = entityImage.loadPreviewImage();        MediaTracker loadTracker = new MediaTracker(comp);        loadTracker.addImage(preview, 0);        try {            loadTracker.waitForID(0);        } catch (InterruptedException e) {            //should never come here        }        bp.setIcon(new ImageIcon(preview));    }    /**     * Get the camo pattern for the given player.     *     * @param player - the <code>Player</code> whose camo pattern is needed.     * @return The <code>Image</code> of the player's camo pattern.     *         This value will be <code>null</code> if the player has selected     *         no camo pattern or if there was an error loading it.     */    public Image getPlayerCamo(Player player) {        // Return a null if the player has selected no camo file.        if (null == player.getCamoCategory() ||                Player.NO_CAMO.equals(player.getCamoCategory())) {            return null;        }        // Try to get the player's camo file.        Image camo = null;        try {            // Translate the root camo directory name.            String category = player.getCamoCategory();            if (Player.ROOT_CAMO.equals(category)) category = ""; //$NON-NLS-1$            camo = (Image) camos.getItem(category, player.getCamoFileName());        } catch (Exception err) {            err.printStackTrace();        }        return camo;    }    /**     * Load a single entity image     */    public synchronized void loadImage(Entity entity) {        Image base = mechTileset.imageFor(entity, comp);        Image wreck = null;        if (!(entity instanceof Infantry) &&                !(entity instanceof Protomech)) {            wreck = wreckTileset.imageFor(entity, comp);        }        Player player = entity.getOwner();        int tint = PlayerColors.getColorRGB(player.getColorIndex());        Image camo = getPlayerCamo(player);        EntityImage entityImage = null;        // check if we have a duplicate image already loaded        for (Iterator j = mechImageList.iterator(); j.hasNext();) {            EntityImage onList = (EntityImage) j.next();            if (onList.getBase().equals(base) && onList.tint == tint) {                entityImage = onList;                break;            }        }        // if we don't have a cached image, make a new one        if (entityImage == null) {            entityImage = new EntityImage(base, wreck, tint, camo, comp);            mechImageList.add(entityImage);            entityImage.loadFacings();            for (int j = 0; j < 6; j++) {                tracker.addImage(entityImage.getFacing(j), 1);            }        }        // relate this id to this image set        mechImages.put(new Integer(entity.getId()), entityImage);    }    /**     * Resets the started and loaded flags     */    public synchronized void reset() {        loaded = false;        started = false;        tracker = new MediaTracker(comp);        hexTileset.reset();    }    /**     * A class to handle the image permutations for an entity     */    private class EntityImage {        private Image base;        private Image wreck;        private Image icon;        private int tint;        private Image camo;        private Image[] facings = new Image[6];        private Image[] wreckFacings = new Image[6];        private JComponent comp;        private static final int IMG_WIDTH = 84;        private static final int IMG_HEIGHT = 72;        private static final int IMG_SIZE = IMG_WIDTH * IMG_HEIGHT;        public EntityImage(Image base, int tint, Image camo, JComponent comp) {            this(base, null, tint, camo, comp);        }        public EntityImage(Image base, Image wreck, int tint, Image camo, JComponent comp) {            this.base = base;            this.tint = tint;            this.camo = camo;            this.comp = comp;            this.wreck = wreck;        }        public void loadFacings() {            base = applyColor(base);            icon = base.getScaledInstance(56, 48, Image.SCALE_SMOOTH);            for (int i = 0; i < 6; i++) {                ImageProducer rotSource = new FilteredImageSource(base.getSource(), new RotateFilter((Math.PI / 3) * (6 - i)));                facings[i] = comp.createImage(rotSource);            }            if (wreck != null) {                wreck = applyColor(wreck);                for (int i = 0; i < 6; i++) {                    ImageProducer rotSource = new FilteredImageSource(wreck.getSource(), new RotateFilter((Math.PI / 3) * (6 - i)));                    wreckFacings[i] = comp.createImage(rotSource);                }            }        }        public Image loadPreviewImage() {            base = applyColor(base);            return base;        }        public Image getFacing(int facing) {            return facings[facing];        }        public Image getWreckFacing(int facing) {            return wreckFacings[facing];        }        public Image getBase() {            return base;        }        public Image getIcon() {            return icon;        }        private Image applyColor(Image image) {            Image iMech;            boolean useCamo = (camo != null);            iMech = image;            int[] pMech = new int[IMG_SIZE];            int[] pCamo = new int[IMG_SIZE];            PixelGrabber pgMech = new PixelGrabber(iMech, 0, 0, IMG_WIDTH, IMG_HEIGHT, pMech, 0, IMG_WIDTH);            try {                pgMech.grabPixels();            } catch (InterruptedException e) {                System.err.println("EntityImage.applyColor(): Failed to grab pixels for mech image." + e.getMessage()); //$NON-NLS-1$                return image;            }            if ((pgMech.getStatus() & ImageObserver.ABORT) != 0) {                System.err.println("EntityImage.applyColor(): Failed to grab pixels for mech image. ImageObserver aborted."); //$NON-NLS-1$                return image;            }            if (useCamo) {                PixelGrabber pgCamo = new PixelGrabber(camo, 0, 0, IMG_WIDTH, IMG_HEIGHT, pCamo, 0, IMG_WIDTH);                try {                    pgCamo.grabPixels();                } catch (InterruptedException e) {                    System.err.println("EntityImage.applyColor(): Failed to grab pixels for camo image." + e.getMessage()); //$NON-NLS-1$                    return image;                }                if ((pgCamo.getStatus() & ImageObserver.ABORT) != 0) {                    System.err.println("EntityImage.applyColor(): Failed to grab pixels for mech image. ImageObserver aborted."); //$NON-NLS-1$                    return image;                }            }            for (int i = 0; i < IMG_SIZE; i++) {                int pixel = pMech[i];                int alpha = (pixel >> 24) & 0xff;                if (alpha != 0) {                    int pixel1 = useCamo ? pCamo[i] : tint;                    float red1 = ((float) ((pixel1 >> 16) & 0xff)) / 255;                    float green1 = ((float) ((pixel1 >> 8) & 0xff)) / 255;                    float blue1 = ((float) ((pixel1) & 0xff)) / 255;                    float black = ((pMech[i]) & 0xff);                    int red2 = Math.round(red1 * black);                    int green2 = Math.round(green1 * black);                    int blue2 = Math.round(blue1 * black);                    pMech[i] = (alpha << 24) | (red2 << 16) | (green2 << 8) | blue2;                }            }            image = comp.createImage(new MemoryImageSource(IMG_WIDTH, IMG_HEIGHT, pMech, 0, IMG_WIDTH));            return image;        }    }}

⌨️ 快捷键说明

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