Package editor.screen

Source Code of editor.screen.MapPanel

package editor.screen;

import beans.core.GeneralConstant;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;
import editor.model.EditorModel;
import java.awt.image.BufferedImage;
import java.util.Map;

/**
*  Vue d'edition de la map
* @author mastersnes
*/
public class MapPanel extends JPanel implements Observer {

    private EditorModel editorModel;

    /**
     * Constructeur de la vue
     * @param editorModel
     * @see EditorModel
     */
    public MapPanel(final EditorModel editorModel) {
        this.editorModel = editorModel;
        editorModel.addObserver(this);
        this.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(final MouseEvent e) {
                final int x = (int) (e.getPoint().getX() / Float.parseFloat(String.valueOf(GeneralConstant.BLOC_WIDTH)));
                final int y = (int) (e.getPoint().getY() / Float.parseFloat(String.valueOf(GeneralConstant.BLOC_HEIGHT)));

                final Point blocPosition = new Point(x, y);
                int modifier = e.getModifiers();
                if (modifier == 16) {
                    editorModel.addBlock(blocPosition);
                } else if (modifier == 4) {
                    editorModel.eraseBlock(blocPosition);
                }
            }

            @Override
            public void mousePressed(final MouseEvent e) {
            }

            @Override
            public void mouseReleased(final MouseEvent e) {
            }

            @Override
            public void mouseEntered(final MouseEvent e) {
            }

            @Override
            public void mouseExited(final MouseEvent e) {
            }
        });

        this.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(final MouseEvent e) {
                final int x = (int) (e.getPoint().getX() / Float.parseFloat(String.valueOf(GeneralConstant.BLOC_WIDTH)));
                final int y = (int) (e.getPoint().getY() / Float.parseFloat(String.valueOf(GeneralConstant.BLOC_HEIGHT)));
                final Point blocPosition = new Point(x, y);

                final int modifier = e.getModifiers();
                if (modifier == 16) {
                    if (editorModel.getCurrentLayer() < 5) {
                        editorModel.addBlock(blocPosition);
                    }
                } else if (modifier == 4) {
                    if (editorModel.getCurrentLayer() < 5) {
                        editorModel.eraseBlock(blocPosition);
                    }
                }
            }

            @Override
            public void mouseMoved(final MouseEvent e) {
            }
        });
    }

    @Override
    public void paintComponent(final Graphics g) {
        final Graphics2D g2 = (Graphics2D) g;

        final Map<Integer, BufferedImage> couches = editorModel.getLayers();
        final Dimension taille = new Dimension(couches.get(1).getWidth(), couches.get(1).getHeight());

        final int currentLayer = editorModel.getCurrentLayer();

        Color mask = new Color(255, 255, 255);
        g2.setColor(mask);


        g2.fillRect(0, 0, taille.width, taille.height);
        g2.drawImage(couches.get(1), 0, 0, this);

        if (currentLayer > 1) {
            mask = new Color(160, 160, 160, 150);
            g2.setColor(mask);
            g2.fillRect(0, 0, taille.width, taille.height);
            g2.drawImage(couches.get(2), 0, 0, this);
        }
        if (currentLayer > 2) {
            mask = new Color(160, 160, 160, 150);
            g2.setColor(mask);
            g2.fillRect(0, 0, taille.width, taille.height);
            g2.drawImage(couches.get(3), 0, 0, this);
        }
        if (currentLayer > 3) {
            mask = new Color(160, 160, 160, 150);
            g2.setColor(mask);
            g2.fillRect(0, 0, taille.width, taille.height);
            g2.drawImage(couches.get(4), 0, 0, this);
        }
        if (currentLayer > 4) {
            final Image grille = (Image) editorModel.getGrille();
            g2.drawImage(grille, 0, 0, this);
            g2.setColor(Color.GREEN);
            for (final Point p : editorModel.getConfig().getEvent().keySet()) {
                g2.fillRect(p.x * GeneralConstant.BLOC_WIDTH, p.y * GeneralConstant.BLOC_HEIGHT, GeneralConstant.BLOC_WIDTH, GeneralConstant.BLOC_HEIGHT);
            }
        }
        g2.dispose();
    }

    @Override
    public void update(final Observable o, final Object arg) {
        editorModel = (EditorModel) o;
        repaint();
    }
}
TOP

Related Classes of editor.screen.MapPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.