Advertisement
Guest User

chess_final_model

a guest
Dec 13th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.07 KB | None | 0 0
  1.  
  2. package chess;
  3.  
  4. import java.awt.Color;
  5. import java.awt.Image;
  6. import java.awt.Point;
  7. import javax.swing.BorderFactory;
  8. import javax.swing.Icon;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12.  
  13. public final class Model
  14. {
  15. private int Edge;
  16. private Colors color;
  17. private Army army;
  18. private Icons icon;
  19. private Points point;
  20. private Labels label;
  21.  
  22. public Model()
  23. {
  24. init();
  25. }
  26.  
  27. public Model(int Edge, JPanel panel)
  28. {
  29. this.Edge = Edge;
  30. init();
  31. setContainer(panel);
  32. }
  33.  
  34. private void init()
  35. {
  36. this.Edge = 70;
  37. army = Army.White;
  38. color = new Colors();
  39. icon = new Icons();
  40. point = new Points();
  41. label = new Labels();
  42. }
  43.  
  44. public void setEdge(int Edge)
  45. {
  46. this.Edge = Edge;
  47. icon.drawImages();
  48. label.drawSquares();
  49. }
  50.  
  51. public int getEdge()
  52. {
  53. return Edge;
  54. }
  55.  
  56. public void setContainer(JPanel panel)
  57. {
  58. label.setContainer(panel);
  59. }
  60.  
  61. public void mouseDown(int x, int y)
  62. {
  63. point.mouseDown.x = x / Edge;
  64. point.mouseDown.y = y / Edge;
  65. if(point.isNone(point.mouseDown) == true || point.isMyArmy(point.mouseDown) == false) return;
  66.  
  67. point.isMouseDown = true;
  68. point.mouseDownImg.x = x - point.mouseDown.x * Edge;
  69. point.mouseDownImg.y = y - point.mouseDown.y * Edge;
  70.  
  71. Point location = label.squares[point.mouseDown.x][point.mouseDown.y].getLocation();
  72. Color color = label.squares[point.mouseDown.x][point.mouseDown.y].getBackground();
  73. String name = label.squares[point.mouseDown.x][point.mouseDown.y].getName();
  74. Icon icon = label.squares[point.mouseDown.x][point.mouseDown.y].getIcon();
  75. label.set6Properties(label.squareCur, location, true, false, color, name, icon);
  76. label.set3Properties(label.squares[point.mouseDown.x][point.mouseDown.y], this.color.Movable, Army.None.sName(), null);
  77.  
  78. location.x = point.mouseDown.x;
  79. location.y = point.mouseDown.y;
  80. point.movable(name, location);
  81. this.color.movable();
  82. }
  83.  
  84. public void mouseMove(int x, int y)
  85. {
  86. if(point.isMouseDown == false)
  87. {
  88. return;
  89. }
  90.  
  91. //label.squareCur.setBorder(null);
  92. label.squareCur.setLocation(x - point.mouseDownImg.x, y - point.mouseDownImg.y);
  93. }
  94.  
  95. public void mouseUp(int x, int y)
  96. {
  97. if(point.isMouseDown == false)
  98. {
  99. return;
  100. }
  101. point.isMouseDown = false;
  102. label.squareCur.setVisible(false);
  103.  
  104. point.mouseUp.x = x / Edge;
  105. point.mouseUp.y = y / Edge;
  106. //safeMyKing();
  107. color.autoLight();
  108. }
  109.  
  110. protected enum Army
  111. {
  112. White("White"),
  113. Dark("Dark"),
  114. None("None");
  115.  
  116. private String army;
  117. private Army(String army)
  118. {
  119. this.army = army;
  120. }
  121.  
  122. public String sName()
  123. {
  124. return army;
  125. }
  126.  
  127. public String sArmy(String army)
  128. {
  129. if(army.contains(White.sName())) return White.sName();
  130. if(army.contains(Dark.sName())) return Dark.sName();
  131. return None.sName();
  132. }
  133. }
  134.  
  135. protected enum Name
  136. {
  137. None("None"),
  138.  
  139. WhiteKing("WhiteKing"),
  140. WhiteQueen("WhiteQueen"),
  141. WhiteBishop("WhiteBishop"),
  142. WhiteKnight("WhiteKnight"),
  143. WhiteRook("WhiteRook"),
  144. WhitePawn("WhitePawn"),
  145.  
  146. DarkKing("DarkKing"),
  147. DarkQueen("DarkQueen"),
  148. DarkBishop("DarkBishop"),
  149. DarkKnight("DarkKnight"),
  150. DarkRook("DarkRook"),
  151. DarkPawn("DarkPawn");
  152.  
  153. private String name;
  154. private Name(String name)
  155. {
  156. this.name = name;
  157. }
  158.  
  159. public String sName()
  160. {
  161. return name;
  162. }
  163. }
  164.  
  165. public final class Points
  166. {
  167. public int movableNumber;
  168. public Point[] movable;
  169.  
  170. public boolean isMouseDown;
  171. public Point mouseDown;
  172. public Point mouseDownImg;
  173. public Point mouseUp;
  174.  
  175. public Point lastWhite1;
  176. public Point lastWhite2;
  177. public Point lastDark1;
  178. public Point lastDark2;
  179.  
  180. public Points()
  181. {
  182. init();
  183. }
  184.  
  185. private void init()
  186. {
  187. isMouseDown = false;
  188. movableNumber = 30;
  189. movable = new Point[movableNumber];
  190. int i;
  191. for(i = 0; i < movableNumber; i++)
  192. {
  193. movable[i] = new Point();
  194. }
  195. resetMovable();
  196.  
  197. mouseDown = new Point();
  198. mouseDown.x = -1;
  199. mouseDown.y = -1;
  200. mouseUp = new Point();
  201. mouseUp.x = -1;
  202. mouseUp.y = -1;
  203. mouseDownImg = new Point();
  204. mouseDownImg.x = -1;
  205. mouseDownImg.y = -1;
  206.  
  207. lastWhite1 = new Point();
  208. lastWhite1.x = -1;
  209. lastWhite1.y = -1;
  210. lastWhite2 = new Point();
  211. lastWhite2.x = -1;
  212. lastWhite2.y = -1;
  213. lastDark1 = new Point();
  214. lastDark1.x = -1;
  215. lastDark1.y = -1;
  216. lastDark2 = new Point();
  217. lastDark2.x = -1;
  218. lastDark2.y = -1;
  219. }
  220.  
  221. public void movable(String name, Point pNow)
  222. {
  223. if(name == Model.Name.WhiteKing.sName() || name == Model.Name.DarkKing.sName()) king(pNow);
  224. else if(name == Model.Name.WhiteQueen.sName() || name == Model.Name.DarkQueen.sName()) queen(pNow);
  225. else if(name == Model.Name.WhiteBishop.sName() || name == Model.Name.DarkBishop.sName()) bishop(pNow, 0);
  226. else if(name == Model.Name.WhiteKnight.sName() || name == Model.Name.DarkKnight.sName()) knight(pNow);
  227. else if(name == Model.Name.WhiteRook.sName() || name == Model.Name.DarkRook.sName()) rook(pNow, 0);
  228. else if(name == Model.Name.WhitePawn.sName() || name == Model.Name.DarkPawn.sName()) pawn(pNow);
  229. }
  230.  
  231. public void resetMovable()
  232. {
  233. int i;
  234. for(i = 0; i < movableNumber; i++)
  235. {
  236. movable[i].x = -1;
  237. movable[i].y = -1;
  238. }
  239. }
  240.  
  241. public void safeMyArmy()
  242. {
  243. int i;
  244. for(i = 0; i < movableNumber; i++)
  245. {
  246. if(isMyArmy(movable[i]) == true)
  247. {
  248. movable[i].x = -1;
  249. movable[i].y = -1;
  250. }
  251. }
  252. }
  253.  
  254. public boolean isSafe(Point pCheck)
  255. {
  256. return isSafe(pCheck.x, pCheck.y);
  257. }
  258.  
  259. public boolean isSafe(int x, int y)
  260. {
  261. if(x < 0 || x > 7 || y < 0 || y > 7) return false;
  262. return true;
  263. }
  264.  
  265. public boolean isMovable(Point pCheck)
  266. {
  267. int i;
  268. for(i = 0; i < movableNumber; i++)
  269. {
  270. if(pCheck.x == movable[i].x && pCheck.y == movable[i].y)
  271. {
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277.  
  278. public boolean isAttacked(Point pCheck)
  279. {
  280. if(isSafe(pCheck) == true)
  281. {
  282. if(isNone(pCheck) == false && isMyArmy(pCheck) == false)
  283. {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289.  
  290. public boolean isMyArmy(Point pCheck)
  291. {
  292. if(isSafe(pCheck) == false) return false;
  293. if(army.sArmy(label.squares[pCheck.x][pCheck.y].getName()) == army.sName()) return true;
  294. return false;
  295. }
  296.  
  297. public boolean isNone(Point pCheck)
  298. {
  299. return isNone(pCheck.x, pCheck.y);
  300. }
  301.  
  302. public boolean isNone(int x, int y)
  303. {
  304. // isSafe = false => None
  305. if(point.isSafe(x, y) == false) return true;
  306.  
  307. if(army.sArmy(label.squares[x][y].getName()) == Model.Army.None.sName())
  308. {
  309. return true;
  310. }
  311.  
  312. return false;
  313. }
  314.  
  315. private void verticalHigh(Point pNow)
  316. {
  317.  
  318. }
  319.  
  320. private void verticalLow(Point pNow)
  321. {
  322.  
  323. }
  324.  
  325. private void horizontalLeft(Point pNow)
  326. {
  327.  
  328. }
  329.  
  330. private void horizontalRight(Point pNow)
  331. {
  332.  
  333. }
  334.  
  335. // Diagonal Main: \
  336. private void diagonalMainHigh(Point pNow)
  337. {
  338.  
  339. }
  340.  
  341. private void diagonalMainLow(Point pNow)
  342. {
  343.  
  344. }
  345.  
  346. // Diagonal Mirror: /
  347. private void diagonalMirrorHigh(Point pNow)
  348. {
  349.  
  350. }
  351.  
  352. private void diagonalMirrorLow(Point pNow)
  353. {
  354.  
  355.  
  356. }
  357.  
  358. private void king(Point pNow)
  359. {
  360. movableNumber = 8;
  361. movable[0].x = pNow.x - 1;
  362. movable[0].y = pNow.y - 1;
  363.  
  364. movable[1].x = pNow.x;
  365. movable[1].y = pNow.y - 1;
  366.  
  367. movable[2].x = pNow.x + 1;
  368. movable[2].y = pNow.y - 1;
  369.  
  370. movable[3].x = pNow.x + 1;
  371. movable[3].y = pNow.y;
  372.  
  373. movable[4].x = pNow.x + 1;
  374. movable[4].y = pNow.y + 1;
  375.  
  376. movable[5].x = pNow.x;
  377. movable[5].y = pNow.y + 1;
  378.  
  379. movable[6].x = pNow.x - 1;
  380. movable[6].y = pNow.y + 1;
  381.  
  382. movable[7].x = pNow.x - 1;
  383. movable[7].y = pNow.y;
  384.  
  385. safeMyArmy();
  386. }
  387.  
  388. private void castling(Point pNow)
  389. {
  390.  
  391. }
  392.  
  393. private void queen(Point pNow)
  394. {
  395. rook(pNow, 0);
  396. bishop(pNow, movableNumber);
  397. }
  398.  
  399. private void rook(Point pNow, int index)
  400. {
  401. int x;
  402. int y;
  403. int k = index;
  404. for(x = pNow.x; x >= 0; x--)
  405. {
  406. movable[k].x = x;
  407. movable[k].y = pNow.y;
  408. if(isNone(movable[k]) == false) break;
  409. k = k + 1;
  410. }
  411. if(isMyArmy(movable[k]) == false) k = k + 1;
  412.  
  413. for(x = pNow.x + 1; x < 8; x++)
  414. {
  415. movable[k].x = x;
  416. movable[k].y = pNow.y;
  417. if(isNone(movable[k]) == false) break;
  418. k = k + 1;
  419. }
  420. if(isMyArmy(movable[k]) == false) k = k + 1;
  421.  
  422. for(y = pNow.y; y >= 0; y--)
  423. {
  424. movable[k].x = pNow.x;
  425. movable[k].y = y;
  426. if(isNone(movable[k]) == false) break;
  427. k = k + 1;
  428. }
  429. if(isMyArmy(movable[k]) == false) k = k + 1;
  430.  
  431. for(y = pNow.y + 1; y < 8; y++)
  432. {
  433. movable[k].x = pNow.x;
  434. movable[k].y = y;
  435. if(isNone(movable[k]) == false) break;
  436. k = k + 1;
  437. }
  438. if(isMyArmy(movable[k]) == false) k = k + 1;
  439.  
  440. movable[k].x = -1;
  441. movable[k].y = -1;
  442. movableNumber = k;
  443. safeMyArmy();
  444. }
  445.  
  446. private void bishop(Point pNow, int index)
  447. {
  448. int x = pNow.x;
  449. int y = pNow.y;
  450. int k = index;
  451. while(true)
  452. {
  453. x = x - 1;
  454. y = y + 1;
  455. movable[k].x = x;
  456. movable[k].y = y;
  457. if(isSafe(x, y) == false || isNone(movable[k]) == false) break;
  458. k = k + 1;
  459. }
  460. if(isMyArmy(movable[k]) == false) k = k + 1;
  461.  
  462. x = pNow.x;
  463. y = pNow.y;
  464. while(true)
  465. {
  466. x = x + 1;
  467. y = y - 1;
  468. movable[k].x = x;
  469. movable[k].y = y;
  470. if(isSafe(x, y) == false || isNone(movable[k]) == false) break;
  471. k = k + 1;
  472. }
  473. if(isMyArmy(movable[k]) == false) k = k + 1;
  474.  
  475. x = pNow.x;
  476. y = pNow.y;
  477. while(true)
  478. {
  479. x = x - 1;
  480. y = y - 1;
  481. movable[k].x = x;
  482. movable[k].y = y;
  483. if(isSafe(x, y) == false || isNone(movable[k]) == false) break;
  484. k = k + 1;
  485. }
  486. if(isMyArmy(point.movable[k]) == false) k = k + 1;
  487.  
  488. x = pNow.x;
  489. y = pNow.y;
  490. while(true)
  491. {
  492. x = x + 1;
  493. y = y + 1;
  494. movable[k].x = x;
  495. movable[k].y = y;
  496. if(isSafe(x, y) == false || isNone(movable[k]) == false) break;
  497.  
  498. k = k + 1;
  499. }
  500. if(point.isMyArmy(movable[k]) == false) k = k + 1;
  501. movable[k].x = -1;
  502. movable[k].y = -1;
  503. movableNumber = k;
  504. safeMyArmy();
  505. }
  506.  
  507. private void knight(Point pNow)
  508. {
  509. movableNumber = 8;
  510. movable[0].x = pNow.x + 1;
  511. movable[0].y = pNow.y - 2;
  512.  
  513. movable[1].x = pNow.x + 2;
  514. movable[1].y = pNow.y - 1;
  515.  
  516. movable[2].x = pNow.x + 2;
  517. movable[2].y = pNow.y + 1;
  518.  
  519. movable[3].x = pNow.x + 1;
  520. movable[3].y = pNow.y + 2;
  521.  
  522. movable[4].x = pNow.x - 1;
  523. movable[4].y = pNow.y + 2;
  524.  
  525. movable[5].x = pNow.x - 2;
  526. movable[5].y = pNow.y + 1;
  527.  
  528. movable[6].x = pNow.x - 2;
  529. movable[6].y = pNow.y - 1;
  530.  
  531. movable[7].x = pNow.x - 1;
  532. movable[7].y = pNow.y - 2;
  533.  
  534. safeMyArmy();
  535. }
  536.  
  537. private void pawn(Point pNow)
  538. {
  539. movableNumber = 4;
  540. int space1 = 0;
  541. int space2 = 0;
  542. if(label.squareCur.getName() == Model.Name.WhitePawn.sName())
  543. {
  544. space1 = 1;
  545. if(mouseDown.y == 6) space2 = 2;
  546. }
  547. else if(label.squareCur.getName() == Model.Name.DarkPawn.sName())
  548. {
  549. space1 = -1;
  550. if(mouseDown.y == 1) space2 = -2;
  551. }
  552.  
  553. movable[0].x = pNow.x;
  554. movable[0].y = pNow.y - space1;
  555. if(isNone(movable[0]) == false)
  556. {
  557. movable[0].x = -1;
  558. movable[0].y = -1;
  559. movable[3].x = -1;
  560. movable[3].y = -1;
  561. }
  562. else
  563. {
  564. movable[3].x = pNow.x;
  565. movable[3].y = pNow.y - space2;
  566. if(isNone(movable[3]) == false)
  567. {
  568. movable[3].x = -1;
  569. movable[3].y = -1;
  570. }
  571. }
  572.  
  573. movable[1].x = pNow.x - space1;
  574. movable[1].y = pNow.y - space1;
  575. if(isNone(movable[1]) == true)
  576. {
  577. movable[1].x = -1;
  578. movable[1].y = -1;
  579. }
  580.  
  581. movable[2].x = pNow.x + space1;
  582. movable[2].y = pNow.y - space1;
  583. if(isNone(movable[2]) == true)
  584. {
  585. movable[2].x = -1;
  586. movable[2].y = -1;
  587. }
  588.  
  589. safeMyArmy();
  590. }
  591. }
  592.  
  593. public final class Labels
  594. {
  595. public JLabel[][] squares;
  596. public JLabel squareCur;
  597.  
  598. public Labels()
  599. {
  600. init();
  601. }
  602.  
  603. private void init()
  604. {
  605. squareCur = new JLabel();
  606. squares = new JLabel[8][8];
  607. int x;
  608. int y;
  609. for(y = 0; y < 8; y++)
  610. {
  611. for(x = 0; x < 8; x++)
  612. {
  613. squares[x][y] = new JLabel();
  614. }
  615. }
  616. drawSquares();
  617. }
  618.  
  619. public void drawSquares()
  620. {
  621. int x = 0;
  622. int y = 0;
  623.  
  624. Point location = new Point();
  625. location.x = x;
  626. location.y = y;
  627. set8Properties(squareCur, false, Edge, location, true, true, Name.None.sName(), null, null);
  628. for(y = 0; y < 8; y++)
  629. {
  630. for(x = 0; x < 8; x++)
  631. {
  632. location.x = x * Edge;
  633. location.y = y * Edge;
  634. set8Properties(squares[x][y], true, Edge, location, true, true, Name.None.sName(), color.get(x, y), null);
  635. }
  636. }
  637.  
  638. // White chess Properties
  639. set2Properties(squares[0][7], Name.WhiteRook.sName(), icon.WhiteRook);
  640. set2Properties(squares[1][7], Name.WhiteKnight.sName(), icon.WhiteKnight);
  641. set2Properties(squares[2][7], Name.WhiteBishop.sName(), icon.WhiteBishop);
  642. set2Properties(squares[3][7], Name.WhiteQueen.sName(), icon.WhiteQueen);
  643. set2Properties(squares[4][7], Name.WhiteKing.sName(), icon.WhiteKing);
  644. set2Properties(squares[5][7], Name.WhiteBishop.sName(), icon.WhiteBishop);
  645. set2Properties(squares[6][7], Name.WhiteKnight.sName(), icon.WhiteKnight);
  646. set2Properties(squares[7][7], Name.WhiteRook.sName(), icon.WhiteRook);
  647.  
  648. for(x = 0; x < 8; x++)
  649. {
  650. set2Properties(squares[x][6], Name.WhitePawn.sName(), icon.WhitePawn);
  651. }
  652.  
  653. // Dark chess Properties
  654. set2Properties(squares[0][0], Name.DarkRook.sName(), icon.DarkRook);
  655. set2Properties(squares[1][0], Name.DarkKnight.sName(), icon.DarkKnight);
  656. set2Properties(squares[2][0], Name.DarkBishop.sName(), icon.DarkBishop);
  657. set2Properties(squares[3][0], Name.DarkQueen.sName(), icon.DarkQueen);
  658. set2Properties(squares[4][0], Name.DarkKing.sName(), icon.DarkKing);
  659. set2Properties(squares[5][0], Name.DarkBishop.sName(), icon.DarkBishop);
  660. set2Properties(squares[6][0], Name.DarkKnight.sName(), icon.DarkKnight);
  661. set2Properties(squares[7][0], Name.DarkRook.sName(), icon.DarkRook);
  662.  
  663. for(x = 0; x < 8; x++)
  664. {
  665. set2Properties(squares[x][1], Name.DarkPawn.sName(), icon.DarkPawn);
  666. }
  667. }
  668.  
  669. public void set8Properties(JLabel label, boolean opaque, int size, Point location, boolean border, boolean visible, String name, Color color, Icon icon)
  670. {
  671. label.setOpaque(opaque);
  672. label.setSize(size, size);
  673.  
  674. set6Properties(label, location, border, visible, color, name, icon);
  675. }
  676.  
  677. public void set6Properties(JLabel label, Point location, boolean visible, boolean border, Color color, String name, Icon icon)
  678. {
  679. label.setLocation(location);
  680.  
  681. if(border == true) label.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  682. else label.setBorder(null);
  683.  
  684. set3Properties(label, color, name, icon);
  685. label.setVisible(visible);
  686. }
  687.  
  688. public void set3Properties(JLabel label, Color color, String name,Icon icon)
  689. {
  690. label.setBackground(color);
  691. set2Properties(label, name, icon);
  692. }
  693.  
  694. public void set2Properties(JLabel label, String name, Icon icon)
  695. {
  696. label.setName(name);
  697. label.setIcon(icon);
  698. }
  699.  
  700. public void setContainer(JPanel container)
  701. {
  702. if(container == null) return;
  703. try
  704. {
  705. int x;
  706. int y;
  707. for(y = 0; y < 8; y++)
  708. {
  709. for(x = 0; x < 8; x++)
  710. {
  711. container.add(label.squares[x][y]);
  712. }
  713. }
  714.  
  715. container.add(label.squareCur);
  716. container.setComponentZOrder(label.squareCur, 0);
  717. container.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  718. container.setPreferredSize(new java.awt.Dimension(Edge * 8, Edge * 8));
  719. // group layout
  720. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(container);
  721. container.setLayout(layout);
  722. } catch(Exception e) {}
  723. }
  724. }
  725.  
  726. public class Colors
  727. {
  728. public Color Default = null;
  729. public Color White = Color.LIGHT_GRAY;
  730. public Color Dark = Color.GRAY;
  731. public Color Movable = new Color(0, 176, 240);
  732. public Color Attacked = new Color(50, 200, 50);
  733. public Color WhiteWhite = Color.YELLOW;
  734. public Color WhiteDark = Color.ORANGE;
  735. public Color DarkWhite = Color.PINK;
  736. public Color DarkDark = Color.MAGENTA;
  737.  
  738. public Color get(int x, int y)
  739. {
  740. if((x + y) % 2 == 0) return White;
  741. return Dark;
  742. }
  743.  
  744. public Color get(int x, int y, Color white, Color dark)
  745. {
  746. if((x + y) % 2 == 0) return white;
  747. return dark;
  748. }
  749.  
  750. public void movable()
  751. {
  752. Point pMove = new Point();
  753. int i;
  754. for(i = 0; i < point.movableNumber; i++)
  755. {
  756. pMove.x = point.movable[i].x;
  757. pMove.y = point.movable[i].y;
  758. if(point.isSafe(point.movable[i]) == false) continue;
  759. if(point.isAttacked(pMove) == true) label.squares[pMove.x][pMove.y].setBackground(color.Attacked);
  760. else label.squares[pMove.x][pMove.y].setBackground(color.Movable);
  761. }
  762. }
  763.  
  764. public void autoLight()
  765. {
  766. int x;
  767. int y;
  768. int i;
  769. for(i = 0; i < point.movableNumber; i++)
  770. {
  771. x = point.movable[i].x;
  772. y = point.movable[i].y;
  773. if(point.isSafe(point.movable[i]) == false) continue;
  774. label.squares[x][y].setBackground(color.get(x, y));
  775. }
  776.  
  777. undo4Last();
  778.  
  779. if(point.isMovable(point.mouseUp) == false)
  780. {
  781. point.mouseUp.x = point.mouseDown.x;
  782. point.mouseUp.y = point.mouseDown.y;
  783. }
  784. label.set3Properties(label.squares[point.mouseUp.x][point.mouseUp.y], label.squareCur.getBackground(), label.squareCur.getName(), label.squareCur.getIcon());
  785.  
  786. point.resetMovable();
  787. if(point.mouseDown.x == point.mouseUp.x && point.mouseDown.y == point.mouseUp.y)
  788. {
  789. return;
  790. }
  791.  
  792. if(army == Army.White)
  793. {
  794. army = Army.Dark;
  795. compute(color.WhiteWhite, color.WhiteDark, point.lastWhite1, point.lastWhite2, point.lastDark1, point.lastDark2);
  796. }
  797. else if(army == Army.Dark)
  798. {
  799. army = Army.White;
  800. compute(color.DarkWhite, color.DarkDark, point.lastDark1, point.lastDark2, point.lastWhite1, point.lastWhite2);
  801. }
  802. }
  803.  
  804. private void undo4Last()
  805. {
  806. int x = point.lastWhite1.x;
  807. int y = point.lastWhite1.y;
  808. if(point.isSafe(x, y) == true)
  809. {
  810. label.squares[x][y].setBackground(color.get(x, y, color.WhiteWhite, color.WhiteDark));
  811. }
  812.  
  813. x = point.lastDark1.x;
  814. y = point.lastDark1.y;
  815. if(point.isSafe(x, y) == true)
  816. {
  817. label.squares[x][y].setBackground(color.get(x, y, color.DarkWhite, color.DarkDark));
  818. }
  819.  
  820. x = point.lastWhite2.x;
  821. y = point.lastWhite2.y;
  822. if(point.isSafe(x, y) == true)
  823. {
  824. label.squares[x][y].setBackground(color.get(x, y, color.WhiteWhite, color.WhiteDark));
  825. }
  826.  
  827. x = point.lastDark2.x;
  828. y = point.lastDark2.y;
  829. if(point.isSafe(x, y) == true)
  830. {
  831. label.squares[x][y].setBackground(color.get(x, y, color.DarkWhite, color.DarkDark));
  832. }
  833. }
  834.  
  835. private void compute(java.awt.Color white, java.awt.Color dark, Point me1, Point me2, Point rival1, Point rival2)
  836. {
  837. boolean flag = true;
  838. if(point.isSafe(me1) == false) flag = false;
  839. if(me1.x == rival2.x && me1.y == rival2.y) flag = false;
  840. if(flag == true)
  841. {
  842. label.squares[me1.x][me1.y].setBackground(color.get(me1.x, me1.y));
  843. }
  844.  
  845. flag = true;
  846. if(point.isSafe(rival1) == false || point.isSafe(rival2) == false) flag = false;
  847. if(point.mouseUp.x != rival2.x || point.mouseUp.y != rival2.y) flag = false;
  848. if(flag == true)
  849. {
  850. label.squares[rival1.x][rival1.y].setBackground(color.get(rival1.x, rival1.y));
  851. label.squares[rival2.x][rival2.y].setBackground(color.get(rival2.x, rival2.y));
  852.  
  853. rival1.x = -1;
  854. rival1.y = -1;
  855. rival2.x = -1;
  856. rival2.y = -1;
  857. }
  858.  
  859. flag = true;
  860. if(point.isSafe(me2) == false) flag = false;
  861. if(me2.x == rival2.x && me2.y == rival2.y) flag = false;
  862. if(flag == true)
  863. {
  864. label.squares[me2.x][me2.y].setBackground(color.get(me2.x, me2.y));
  865. }
  866.  
  867. if(point.isSafe(point.mouseDown) == true)
  868. {
  869. label.squares[point.mouseDown.x][point.mouseDown.y].setBackground(color.get(point.mouseDown.x, point.mouseDown.y, white, dark));
  870. }
  871. if(point.isSafe(point.mouseUp) == true)
  872. {
  873. label.squares[point.mouseUp.x][point.mouseUp.y].setBackground(color.get(point.mouseUp.x, point.mouseUp.y, white, dark));
  874. }
  875.  
  876. me1.x = point.mouseDown.x;
  877. me1.y = point.mouseDown.y;
  878. me2.x = point.mouseUp.x;
  879. me2.y = point.mouseUp.y;
  880. }
  881. }
  882.  
  883. public final class Icons
  884. {
  885. public ImageIcon WhiteKing;
  886. public ImageIcon WhiteQueen;
  887. public ImageIcon WhiteBishop;
  888. public ImageIcon WhiteKnight;
  889. public ImageIcon WhiteRook;
  890. public ImageIcon WhitePawn;
  891. public ImageIcon DarkKing;
  892. public ImageIcon DarkQueen;
  893. public ImageIcon DarkBishop;
  894. public ImageIcon DarkKnight;
  895. public ImageIcon DarkRook;
  896. public ImageIcon DarkPawn;
  897.  
  898. public Icons()
  899. {
  900. init();
  901. }
  902.  
  903. private void init()
  904. {
  905. WhiteKing = new ImageIcon();
  906. WhiteQueen = new ImageIcon();
  907. WhiteBishop = new ImageIcon();
  908. WhiteKnight = new ImageIcon();
  909. WhiteRook = new ImageIcon();
  910. WhitePawn = new ImageIcon();
  911.  
  912. DarkKing = new ImageIcon();
  913. DarkQueen = new ImageIcon();
  914. DarkBishop = new ImageIcon();
  915. DarkKnight = new ImageIcon();
  916. DarkRook = new ImageIcon();
  917. DarkPawn = new ImageIcon();
  918.  
  919. drawImages();
  920. }
  921.  
  922. public void drawImages()
  923. {
  924. String path = "E:\\ChessImage\\alpha\\";
  925. WhiteKing.setImage(newImage(path +"WhiteKing.png", Edge, Edge));
  926. WhiteQueen.setImage(newImage(path +"WhiteQueen.png", Edge, Edge));
  927. WhiteBishop.setImage(newImage(path +"WhiteBishop.png", Edge, Edge));
  928. WhiteKnight.setImage(newImage(path +"WhiteKnight.png", Edge, Edge));
  929. WhiteRook.setImage(newImage(path +"WhiteRook.png", Edge, Edge));
  930. WhitePawn.setImage(newImage(path +"WhitePawn.png", Edge, Edge));
  931.  
  932. DarkKing.setImage(newImage(path +"DarkKing.png", Edge, Edge));
  933. DarkQueen.setImage(newImage(path +"DarkQueen.png", Edge, Edge));
  934. DarkBishop.setImage(newImage(path +"DarkBishop.png", Edge, Edge));
  935. DarkKnight.setImage(newImage(path +"DarkKnight.png", Edge, Edge));
  936. DarkRook.setImage(newImage(path +"DarkRook.png", Edge, Edge));
  937. DarkPawn.setImage(newImage(path +"DarkPawn.png", Edge, Edge));
  938. }
  939.  
  940. private Image newImage(String path, int width, int height)
  941. {
  942. Image image = new ImageIcon(path).getImage().getScaledInstance(width , height, Image.SCALE_SMOOTH);
  943. return image;
  944. }
  945. }
  946. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement