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

📄 mapeditorpanel.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Yura Mamyrin

package risk.tools.mapeditor;

import risk.engine.*;
import risk.engine.core.*;

import risk.engine.guishared.PicturePanel;

import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.filechooser.*;

import java.util.Map;
import java.util.List;
import java.util.Arrays;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.awt.Rectangle;
import java.net.URI;
import java.awt.AlphaComposite;
import java.awt.image.IndexColorModel;
import java.awt.BasicStroke;

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

/**
 * @author Yura Mamyrin
 */

public class MapEditorPanel extends JPanel implements MouseInputListener,MouseWheelListener {

	public static final int MODE_MOVE = 0;
	public static final int MODE_MOVEALL = 1;
	public static final int MODE_JOIN = 2;
	public static final int MODE_JOIN1WAY = 3;
	public static final int MODE_DISJOIN = 4;
	public static final int MODE_DRAW = 5;

	//private List countries; // every item in this list also has its position+1 stored as the "color" value of it
	//private List continents;
	private RiskGame myMap;
	private BufferedImage pic;
	private BufferedImage map;
	private BufferedImage drawImage;
	private Country selected;
	private Rectangle box;
	private int mode;
	private int brush;
	private float alpha;
	private Point dragpoint;
	private int zoom;

	private MapEditor editor;

	public MapEditorPanel(MapEditor a) {

		editor = a;

        	addMouseMotionListener(this);
		addMouseListener(this);

		addMouseWheelListener(this);

		ToolTipManager.sharedInstance().setDismissDelay(10000);

		mode = MODE_MOVE;

	}

	public BufferedImage getImageMap() {

		return map;

	}


	public BufferedImage getImagePic() {

		return pic;

	}

	public void zoom(int a) {

	    zoom = a;

	    if (pic!=null) {

		Dimension size = new Dimension(pic.getWidth()*zoom, pic.getHeight()*zoom);

		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);

		revalidate();
		repaint();

	    }
	}

	public void setImagePic(BufferedImage a) {

		if (a.getWidth()!=PicturePanel.PP_X || a.getHeight()!=PicturePanel.PP_Y) {

			JOptionPane.showMessageDialog(this,"Only Risk 1.0.9.5+ supports any size maps!\nfor older version use: width="+PicturePanel.PP_X+" height="+PicturePanel.PP_Y);
		}

		pic = a;

		zoom(zoom);

	}

	public void setImageMap(BufferedImage a) {

		if (a.getWidth() != pic.getWidth() || a.getHeight() != pic.getHeight() ) {

			JOptionPane.showMessageDialog(this,"ImageMap does not match ImagePic size!\nThey should match for the game to work!");

		}

		map = a;

		// some error happens when done like this?????
		//map = new BufferedImage( a.getWidth() , a.getHeight() , BufferedImage.TYPE_USHORT_GRAY);
		//Graphics g = map.getGraphics();
		//g.drawImage(a,0,0,this);
		//g.dispose();

		drawImage = new BufferedImage(a.getWidth(),a.getHeight(),BufferedImage.TYPE_BYTE_BINARY,

			new IndexColorModel(1, 2, new byte[] { 0, (byte)0xff }, new byte[] { 0, 0 }, new byte[] { 0, 0 }, 0)

		);

		box = new Rectangle( new Dimension(a.getWidth(), a.getHeight()) );

	}



	public void setMap(RiskGame a) {

		myMap = a;

		//countries = Arrays.asList( myMap.getCountries() );
		//continents = Arrays.asList( myMap.getContinents() );

	}

	public void update(Map a) {

		int width = map.getWidth();
		int height =  map.getHeight();

		// have to make new image coz if we reuse the old 1 we get 0 values for some reason		// cant use this as get 0 values

		// cant make a new image, coz ither removing stops working or drawing draws in a wrong color
		//BufferedImage newImageMap = new BufferedImage( width, height, BufferedImage.TYPE_BYTE_INDEXED ); //  TYPE_BYTE_GRAY

		int[] pixels = map.getRGB(0,0,width,height,null,0,width);

		int oldcolor,newcolor;

		for (int c=0;c<pixels.length;c++) {

			oldcolor = pixels[c] & 0xff;

//if (a.get( new Integer(oldcolor) ) == null) {
//System.out.println(oldcolor+" goes to "+ a.get( new Integer(oldcolor) ) );
//}
			Object obj = a.get( new Integer(oldcolor) );

			if (obj != null) {

				newcolor = ((Integer)obj).intValue();

			}
			else {

				newcolor = oldcolor;

				System.out.println("bad color: "+oldcolor);

			}

//if (newcolor == 0) {
//System.out.println( oldcolor+" goes to 0!!!" );
//}


			pixels[c] = ((newcolor & 0xFF) << 16) | ((newcolor & 0xFF) << 8) | ((newcolor & 0xFF) << 0);

		}

		//newImageMap.
		map.setRGB(0,0,width,height,pixels,0,width);
		//map = newImageMap;

		repaint();

	}

	public void setCountry(Country a) {

	    if (selected != a) {

		selected = a;


		int width = map.getWidth();
		int height =  map.getHeight();

		int[] pixels1 = map.getRGB(0,0,width,height,null,0,width);
		int[] pixels2 = drawImage.getRGB(0,0,width,height,null,0,width);

		for (int c=0;c<pixels1.length;c++) {

			if (selected!=null && selected.getColor() == (pixels1[c]&0xff) ) {

				pixels2[c] = -65536; // Color.RED.getRGB()

			}
			else {

				pixels2[c] = 0;

			}
		}

		drawImage.setRGB(0,0,width,height,pixels2,0,width);

		repaint();

	    }

	}

	public void setAlpha(int a) {

		alpha = a/100F;

	}

	public void setBrush(int a) {

		brush = (a==0)?1:a;

	}

	public void setMode(int a) {

		mode = a;

		dragpoint = null;

		repaint();

	}

    public void paintComponent(Graphics g) {

	super.paintComponent(g);

	Graphics2D g0 = (Graphics2D)g;
	g0.scale(zoom,zoom);


	if (myMap!=null) {

	    //System.out.println(alpha);

	    if (alpha!=1) {

            	g.drawImage(pic,0,0,this);

	    	drawCountries(g);
	    }

	    if (alpha!=0) {

		Graphics2D g2 = (Graphics2D)g.create();
		AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
		g2.setComposite(ac);

		g2.drawImage(map,0,0,this);

		//if (mode == MODE_DRAW) {

			g2.drawImage(drawImage,0,0,this);

		//}

	    }



	    if (mode == MODE_DRAW && dragpoint!=null) {

		g.setXORMode(Color.WHITE);

		g.setColor(Color.BLACK);

		g.drawOval(dragpoint.x-(brush/2),dragpoint.y-(brush/2),brush,brush);

		g.setPaintMode();

	    }



	}
	else {

		super.paintComponent(g);

	}

    }

    private void drawCountries(Graphics g) {

	    int r=10;

	    int width = pic.getWidth();

	    Country[] countries = myMap.getCountries();

            for (int i = 0; i < countries.length; i++) {

                Country n = countries[i];
                int x = n.getX();
                int y = n.getY();

		g.setColor( n.getContinent().getColor() );
		g.fillOval( x-r , y-r, (r*2), (r*2) );

                List ney = n.getNeighbours();
                for (int j = 0; j < ney.size(); j++) {

                    Country n1 = (Country)ney.get(j);
                    int x1 = n1.getX();
                    int y1 = n1.getY();


                    if (n1.getNeighbours().contains(n)) {
                        g.setColor(Color.BLUE);
		    }
                    else {
                        g.setColor(Color.GREEN);
		    }



			if ( Math.abs( x - x1 ) > ( width  / 2) ) {


				if ( x > (width / 2) ) { // ie "n" is on the right
					g.drawLine( x, y, x1+width, y1);
					g.drawLine( x-width, y, x1, y1);

				}
				else { // the attacker is on the left
					g.drawLine( x, y, x1-width, y1);
					g.drawLine( x+width, y, x1, y1);
				}

			}
			else {

                    		g.drawLine(x,y,x1,y1);

			}

		}





                if (selected == n) {
                    g.setColor(Color.RED);
		}
		else {
                    g.setColor(Color.BLUE);
		}
                g.drawRect(x-2,y-2,4,4);
                g.drawRect(x-3,y-3,6,6);


		g.setXORMode(Color.WHITE);
		g.setColor(Color.BLACK);

⌨️ 快捷键说明

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