📄 mapeditorpanel.java
字号:
g.drawString(n.getIdString(), x,y);
g.drawString(String.valueOf(i+1), x,y+10);
g.setPaintMode();
}
}
public boolean contains(int x, int y) {
if (myMap!=null) {
Country mynode = getCountryAt(x/zoom,y/zoom);
if (mynode!=null) {
String show="<html><b>"+mynode.getIdString()+" ("+mynode.getColor()+")</b><br>Location: (x="+mynode.getX()+",y="+mynode.getY()+")<br>" +
"Continent: " + mynode.getContinent();
List ney = mynode.getNeighbours();
for (int j = 0; j < ney.size(); j++) {
Country n1 = (Country)ney.get(j);
show = show + "<br>Neighbour: " + n1.getIdString() +" ("+n1.getColor()+")";
}
show = show + "</html>";
setToolTipText(show);
}
else {
setToolTipText(null);
}
return true; // this is needed so the mouse listoner can use it
}
else {
return false;
}
}
public void drawLine(Point a,Point b,boolean draw) {
if (selected!=null || !draw) {
// this fixes a really odd bug with drawing lines on indexed images
if (a.y>b.y) {
Point z = a;
a = b;
b = z;
}
//@YURA:TODO should not do this each time
Graphics2D g1 = (Graphics2D)drawImage.getGraphics();
Graphics2D g2 = (Graphics2D)map.getGraphics();
BasicStroke bs = new BasicStroke(brush,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g1.setStroke(bs);
g2.setStroke(bs);
if (draw) { g1.setColor(Color.RED); g2.setColor( new Color(selected.getColor(),selected.getColor(),selected.getColor()) ); }
else { g1.setColor(Color.BLACK); g2.setColor(Color.WHITE); }
g1.drawLine(a.x, a.y, b.x, b.y);
g2.drawLine(a.x, a.y, b.x, b.y);
g1.dispose();
g2.dispose();
}
}
public Country getCountryAt(int x,int y) {
Country[] countries = myMap.getCountries();
int size = 4;
Country mynode = null;
for (int i = 0; i < countries.length; i++) {
Country n = countries[i];
int x1 = n.getX();
int y1 = n.getY();
if (x1 >= x - size && y1 >= y - size && x1 <= x + size && y1 <= y + size) {
mynode = n;
break;
}
}
return mynode;
}
public Point getPoint(MouseEvent e) {
return new Point( e.getX()/zoom,e.getY()/zoom );
}
// #############################################################
// ###################### mouse ###########################
// ##################################################
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() < 0) {
editor.zoom(true);
}
else {
editor.zoom(false);
}
}
public void mouseClicked(MouseEvent e) {
if (myMap!=null) {
Point point = getPoint(e);
if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK) {
if (mode != MODE_DRAW) {
setCountry(null);
}
}
else if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) {
if (mode == MODE_JOIN) {
Country mynode = getCountryAt(point.x,point.y);
if (mynode!=null && selected==null) {
setCountry(mynode);
}
else if (mynode!=null && mynode==selected) {
setCountry(null);
}
else if (mynode!=null) {
if (!selected.getNeighbours().contains(mynode)) {
selected.addNeighbour(mynode);
}
if (!mynode.getNeighbours().contains(selected)) {
mynode.addNeighbour(selected);
}
repaint();
}
}
else if (mode == MODE_JOIN1WAY) {
Country mynode = getCountryAt(point.x,point.y);
if (mynode!=null && selected==null) {
setCountry(mynode);
}
else if (mynode!=null && mynode==selected) {
setCountry(null);
}
else if (mynode!=null) {
if (!selected.getNeighbours().contains(mynode)) {
selected.addNeighbour(mynode);
}
repaint();
}
}
else if (mode == MODE_DISJOIN) {
Country mynode = getCountryAt(point.x,point.y);
if (mynode!=null && selected==null) {
setCountry(mynode);
}
else if (mynode!=null && mynode==selected) {
setCountry(null);
}
else if (mynode!=null) {
if (selected.getNeighbours().contains(mynode)) {
selected.getNeighbours().remove(mynode);
}
if (mynode.getNeighbours().contains(selected)) {
mynode.getNeighbours().remove(selected);
}
repaint();
}
}
}
}
}
private boolean xdrag;
public void mousePressed(MouseEvent e) {
if ( myMap!=null && (
( (e.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) ||
( (e.getModifiers() & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK)
)) {
Point point = getPoint(e);
if (mode==MODE_MOVE) {
Country mynode = getCountryAt(point.x,point.y);
if (mynode!=null) {
setCountry(mynode);
xdrag = true;
}
else {
dragpoint = e.getPoint();
xdrag = false;
}
}
else if (mode==MODE_MOVEALL) {
dragpoint = point;
}
else if (mode==MODE_DRAW) {
dragpoint = point;
drawLine(dragpoint,dragpoint, ( (e.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) );
repaint();
}
}
}
public void mouseReleased(MouseEvent e) {
if (mode == MODE_MOVE) {
dragpoint=null;
}
else if (mode == MODE_MOVEALL) {
dragpoint=null;
} // ((JViewport)getParent()).getViewRect()
else if (mode == MODE_DRAW && !getVisibleRect().contains(e.getPoint()) ) {
// if mouse released outside the box
dragpoint=null;
repaint();
}
}
public void mouseDragged(MouseEvent e) {
if (myMap!=null) {
Point point = getPoint(e);
if (mode == MODE_MOVE) {
if (xdrag && box!=null && selected!=null && box.contains(point.x,point.y)) {
selected.setX(point.x);
selected.setY(point.y);
//dragpoint = point;
scrollRectToVisible( new Rectangle(e.getX(), e.getY(), 1, 1) );
repaint();
}
else if (!xdrag && dragpoint!=null) {
Rectangle r = getVisibleRect();
r.translate(dragpoint.x-e.getX(),dragpoint.y-e.getY());
scrollRectToVisible( r );
}
}
else if (mode == MODE_MOVEALL && dragpoint!=null) {
int xdif = point.x - dragpoint.x;
int ydif = point.y - dragpoint.y;
Country[] countries = myMap.getCountries();
for(int i = 0; i < countries.length; i++) {
countries[i].setX( countries[i].getX()+xdif );
countries[i].setY( countries[i].getY()+ydif );
}
dragpoint = point;
repaint();
}
else if (mode == MODE_DRAW && dragpoint!=null) {
Point end = point;
if (
( (e.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) ||
( (e.getModifiers() & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK)
) {
drawLine(dragpoint,end, ( (e.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) );
}
dragpoint = end;
repaint();
}
}
}
public void mouseExited(MouseEvent e) {
if (mode == MODE_DRAW) {
if (
( (e.getModifiers() & MouseEvent.BUTTON1_MASK) != MouseEvent.BUTTON1_MASK) &&
( (e.getModifiers() & MouseEvent.BUTTON3_MASK) != MouseEvent.BUTTON3_MASK)
) {
dragpoint = null;
repaint();
}
}
}
public void mouseMoved(MouseEvent e) {
if (mode == MODE_DRAW) {
dragpoint = getPoint(e);
repaint();
}
}
public void mouseEntered(MouseEvent e) {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -