Advertisement
pinkikoi

Untitled

Nov 12th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. package pl.wipb.ztp.chess;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.MouseAdapter;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseMotionAdapter;
  14. import java.awt.geom.AffineTransform;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19.  
  20. import javax.imageio.ImageIO;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JButton;
  23. import javax.swing.JFrame;
  24. import javax.swing.JPanel;
  25. import javax.swing.JToolBar;
  26.  
  27. public class Chessboard extends JPanel {
  28. public static final int ZEROX = 23;
  29. static final int ZEROY = 7;
  30. int TILESIZE = 32;
  31. AffineTransform draggedTransform = null;
  32.  
  33. private HashMap<Point, PieceInterface> board = new HashMap<>();
  34.  
  35. public void drop(PieceInterface p, int x, int y) {
  36. repaint();
  37. p.moveTo(x, y);
  38. board.put(new Point(x, y), p); // jeśli na tych współrzędnych
  39. // jest już jakaś figura, znika ona z planszy
  40. // (HashMap nie dopuszcza powtórzeń)
  41. }
  42.  
  43. public PieceInterface take(int x, int y) {
  44. repaint();
  45. return board.remove(new Point(x, y));
  46. }
  47.  
  48. private Image image;
  49. private PieceInterface dragged = null;
  50. private Point mouse = null;
  51.  
  52. @Override
  53. public void paintComponent(Graphics g) {
  54. super.paintComponent(g);
  55. g.drawImage(image, 0, 0, null);
  56. Graphics2D g2d = (Graphics2D) g;
  57. for (Map.Entry<Point, PieceInterface> e : board.entrySet()) {
  58. Point pt = e.getKey();
  59. PieceInterface pc = e.getValue();
  60. pc.draw(g2d);
  61. }
  62. if (mouse != null && dragged != null) {
  63. dragged.draw(g2d);
  64. }
  65. }
  66.  
  67. Chessboard() {
  68. AffineTransform tr = new AffineTransform();
  69. tr.translate(ZEROX, ZEROY);
  70. tr.scale(TILESIZE, TILESIZE);
  71. this.board.put(new Point(0, 2), new AffineDecorator(new Piece(11, 0, 2), tr));
  72. this.board.put(new Point(0, 6), new AffineDecorator(new Piece(0, 0, 6), tr));
  73. this.board.put(new Point(1, 4), new AffineDecorator(new Piece(6, 1, 4), tr));
  74. this.board.put(new Point(1, 5), new AffineDecorator(new Piece(5, 1, 5), tr));
  75. this.board.put(new Point(3, 7), new AffineDecorator(new Piece(1, 3, 7), tr));
  76. this.board.put(new Point(4, 3), new AffineDecorator(new Piece(6, 4, 3), tr));
  77. this.board.put(new Point(4, 4), new AffineDecorator(new Piece(7, 4, 4), tr));
  78. this.board.put(new Point(5, 4), new AffineDecorator(new Piece(6, 5, 4), tr));
  79. this.board.put(new Point(5, 6), new AffineDecorator(new Piece(0, 5, 6), tr));
  80. this.board.put(new Point(6, 5), new AffineDecorator(new Piece(0, 6, 5), tr));
  81. this.board.put(new Point(7, 4), new AffineDecorator(new Piece(0, 7, 4), tr));
  82.  
  83. try {
  84. image = loadImage("/img/board3.png");
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. System.exit(-1);
  88. }
  89. setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
  90.  
  91. this.addMouseListener(new MouseAdapter() {
  92. public void mousePressed(MouseEvent ev) {
  93. Chessboard.this.dragged = Chessboard.this.take((ev.getX() - 23) / 32, (ev.getY() - 7) / 32);
  94. if (Chessboard.this.dragged != null) {
  95. Chessboard.this.draggedTransform = new AffineTransform();
  96. Chessboard.this.dragged = new AffineDecorator(Chessboard.this.dragged, Chessboard.this.draggedTransform);
  97. Chessboard.this.mouse = ev.getPoint();
  98. }
  99. }
  100.  
  101. public void mouseReleased(MouseEvent ev) {
  102. if (Chessboard.this.dragged != null) {
  103. Chessboard.this.drop(Chessboard.this.dragged.getDecorated(), (ev.getX() - 23) / 32, (ev.getY() - 7) / 32);
  104. Chessboard.this.dragged = null;
  105. Chessboard.this.undo.setEnabled(true);
  106. }
  107. }
  108. });
  109. this.addMouseMotionListener(new MouseMotionAdapter() {
  110. public void mouseDragged(MouseEvent ev) {
  111. if (Chessboard.this.draggedTransform != null) {
  112. Chessboard.this.draggedTransform.setToTranslation((double)ev.getX() - Chessboard.this.mouse.getX(), (double)ev.getY() - Chessboard.this.mouse.getY());
  113. Chessboard.this.repaint();
  114. }
  115. }
  116. });
  117. }
  118.  
  119. class UndoButton implements ActionListener {
  120. public void actionPerformed(ActionEvent ev) {
  121. System.out.println("UNDO");
  122. redo.setEnabled(true);
  123. }
  124. }
  125.  
  126. class RedoButton implements ActionListener {
  127. public void actionPerformed(ActionEvent ev) {
  128. System.out.println("REDO");
  129. }
  130. }
  131.  
  132. private JButton undo, redo;
  133.  
  134.  
  135. public static void main(String[] args) {
  136. JFrame frame = new JFrame("Chess");
  137. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  138.  
  139. Chessboard board = new Chessboard();
  140.  
  141. JToolBar bar = new JToolBar();
  142. try {
  143. board.undo = new JButton(new ImageIcon(loadImage("/img/undo.png")));
  144. board.redo = new JButton(new ImageIcon(loadImage("/img/redo.png")));
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. }
  148.  
  149. board.undo.addActionListener(board.new UndoButton());
  150. board.redo.addActionListener(board.new RedoButton());
  151. board.undo.setEnabled(false);
  152. board.redo.setEnabled(false);
  153. bar.add(board.undo);
  154. bar.add(board.redo);
  155.  
  156. frame.add(bar, BorderLayout.PAGE_START);
  157. frame.add(board);
  158.  
  159. frame.pack();
  160. frame.setVisible(true);
  161. }
  162.  
  163. public static Image loadImage(String path) throws IOException {
  164. InputStream fileName = Chessboard.class.getResourceAsStream(path);
  165. return ImageIO.read(fileName);
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement