Advertisement
ArthurDn

Untitled

Oct 15th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.List;
  3. import java.awt.event.*;
  4.  
  5. import javax.swing.*;
  6. import java.util.*;
  7. import java.util.Timer;
  8. public class Board extends JPanel implements ActionListener {
  9.     boolean isFirst = false;
  10.     int ii;
  11.     int jj;
  12.     int iii;
  13.     int jjj;
  14.     WhiteChecker w;
  15.     BlackChecker b;
  16.     public Image img;
  17.     Timer time;
  18. static int xxx;
  19.     public Board() {
  20.         Checker.init();
  21.    
  22.  
  23.         addMouseListener(new MouseAdapter() {
  24.             public void mouseClicked(MouseEvent e) {
  25.                 if (isFirst == false) {
  26.                     for (int j = 0; j < 8; j++) {
  27.                         for (int i = 0; i < 8; i++) {
  28.                             if (Checker.hist[i][j] == clickX(e.getX()))
  29.                                 {ii = i;
  30.                             jj = j;System.out.println(ii+" "+jj);}
  31.                         }
  32.                     }
  33.                     for (int j = 0; j < 8; j++) {
  34.                         for (int i = 0; i < 8; i++) {
  35.                             if (Checker.hist[i][j] == clickY(e.getY()))
  36.                                 {iii = i;
  37.                             jjj = j;System.out.println(iii+"           "+jjj);}
  38.                         }
  39.                     }
  40.                     isFirst = true;
  41.                 } else {
  42.                     Checker.curr[iii][jjj] = clickY(e.getY());
  43.                     Checker.curr[ii][jj] = clickX(e.getX());System.out.println("x="+clickX(e.getX()));System.out.println("y="+clickY(e.getY()));
  44.                     isFirst = false;
  45.                 }
  46.             };
  47.         });
  48.         setFocusable(true);
  49.         ImageIcon i = new ImageIcon("src/1.gif");
  50.         img = i.getImage();
  51.         time = new Timer(5, this);
  52.         time.start();
  53.    
  54.        
  55.     }
  56.  
  57.     public void init() {
  58.                    
  59.        
  60.     }
  61.    
  62.     public void move() {
  63.  
  64.     }
  65.    
  66.     public int clickY(int y) {
  67.         int minY = 0;
  68.         int maxY = 700;
  69.         int ret;
  70.         for (int j = 0; j < 8; j++) {
  71.             for (int i = 0; i < 8; i++) {
  72.                 if (Checker.hist[i][j] > y && (maxY > Checker.hist[i][j])) {
  73.                     maxY = Checker.hist[i][j];
  74.                 }
  75.                 if (Checker.hist[i][j] < y && (minY < Checker.hist[i][j])) {
  76.                     minY = Checker.hist[i][j];
  77.                 }
  78.             }
  79.         }
  80.         if ((maxY - y) > (y - minY)) {
  81.             ret = minY;
  82.         } else {
  83.             ret = maxY;
  84.         }
  85.         return ret;
  86.     }
  87.  
  88.     public int clickX(int x) {
  89.         int minX = 0;
  90.         int maxX = 700;
  91.         int ret;
  92.         for (int j = 0; j < 8; j++) {
  93.             for (int i = 0; i < 8; i++) {
  94.                 if (Checker.hist[i][j] > x && (maxX > Checker.hist[i][j])) {
  95.                     maxX = Checker.hist[i][j];
  96.                 }
  97.                 if (Checker.hist[i][j] < x && (minX < Checker.hist[i][j])) {
  98.                     minX = Checker.hist[i][j];
  99.                 }
  100.             }
  101.         }
  102.         if ((maxX - x) > (x - minX)) {
  103.             ret = minX;
  104.         } else {
  105.             ret = maxX;
  106.         }
  107.         return ret;
  108.     }
  109.  
  110.     public void actionPerformed(ActionEvent e) {
  111.         repaint();
  112.     }
  113.  
  114.     public void paint(Graphics g) {
  115.         Graphics g2d=g;
  116.         g2d.drawImage(img, 0, 0, null);
  117.         for (int j = 0; j < 8; j++) {
  118.             for (int i = 0; i < 8; i++) {
  119.                 if (Checker.hist[i][j]==1) {
  120.                     g2d.drawImage(b.getImage(), 28+i*34,
  121.                             7+j*35, null);
  122.                 }  
  123.             }
  124.         }
  125.         for (int j = 0; j < 8; j++) {
  126.             for (int i = 0; i < 8; i++) {
  127.                 if (Checker.hist[i][j]==2) {
  128.                     g2d.drawImage(w.getImage(), 28+i*34,
  129.                             7+j*35, null);
  130.                 }  
  131.             }
  132.         }
  133.    
  134.        
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement