Haifisch7734

goovno

Jun 1st, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. package klient.warcaby.wykonanie;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.awt.image.BufferedImage;
  10. import java.io.BufferedReader;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14.  
  15. import javax.imageio.ImageIO;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. import javax.swing.SwingUtilities;
  19.  
  20. import klient.warcaby.deklaracje.Pionek;
  21. import klient.warcaby.deklaracje.Pole;
  22. import klient.warcaby.deklaracje.Ruch;
  23. import klient.warcaby.deklaracje.Szachownica;
  24.  
  25. public class Gra extends JPanel implements MouseListener {
  26.  
  27.     Komunikator komunikator = new Komunikator();
  28.     PrintWriter wy;
  29.     BufferedReader we;
  30.     Szachownica szach;
  31.     boolean koniec = false;
  32.     boolean zmiana = true;
  33.     private int x, y;
  34.     static JFrame frame = new JFrame("Warcaby");
  35.     static Gra g = new Gra();
  36.     boolean aktywny, cpionek, cpole;
  37.     char kolor;
  38.     Pole pole = null;
  39.     Pionek pionek = null;
  40.    
  41.    
  42.     public Gra() {
  43.         szach = new Szachownica();
  44.         addMouseListener(this);
  45.         setPreferredSize(new Dimension(601, 624));
  46.     }
  47.  
  48.     private BufferedImage image;
  49.  
  50.     @Override
  51.     public void paintComponent(Graphics g) {
  52.  
  53.         File imageFile = new File("tlo.jpg");
  54.         try {
  55.             image = ImageIO.read(imageFile);
  56.         } catch (IOException e) {
  57.             System.err.println("Blad odczytu obrazka");
  58.             e.printStackTrace();
  59.         }
  60.  
  61.         Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
  62.         setPreferredSize(dimension);
  63.  
  64.         Graphics2D g2d = (Graphics2D) g;
  65.            
  66.                 g2d.drawImage(image, 0, 0, this);
  67.                 g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
  68.                 int x = -263;
  69.                 int i, j;
  70.                 for (i = 0; i < 8; i++) {
  71.                     int y = -263;
  72.                     for (j = 0; j < 8; j++) {
  73.                         if (szach.pola[i][j].getPionek() != null)
  74.                             if ((szach.pola[i][j].getPionek().getKolor()) != ' ') {
  75.                                 if ((szach.pola[i][j].getPionek().getKolor()) == 'B') {
  76.                                     g2d.setColor(Color.WHITE);
  77.                                     g2d.fillOval(x, y, 50, 50);
  78.                                     if ((szach.pola[i][j].getPionek().getDamka())) {
  79.                                         g2d.setColor(Color.RED);
  80.                                         g2d.fillOval(x+20, y+20, 10, 10);
  81.                                     }
  82.                                 }
  83.                                 if ((szach.pola[i][j].getPionek().getKolor()) == 'C') {
  84.                                     g2d.setColor(Color.BLACK);
  85.                                     g2d.fillOval(x, y, 50, 50);
  86.                                     if ((szach.pola[i][j].getPionek().getDamka())) {
  87.                                         g2d.setColor(Color.RED);
  88.                                         g2d.fillOval(x+20, y+20, 10, 10);
  89.                                     }
  90.                                 }
  91.                                
  92.                             }
  93.  
  94.                         y = y + 69;
  95.  
  96.                     }
  97.                     x = x + 69;
  98.                 }
  99.                 g2d.setColor(Color.WHITE);
  100.                 if(this.kolor == 'B')
  101.                     g2d.drawString("Biale", 0, 0);
  102.                 else
  103.                     g2d.drawString("Czarne", 0, 0);    
  104.             }
  105.        
  106.         public void mouseClicked(MouseEvent e) {
  107.             if (aktywny == false)
  108.                 return;
  109.             if (cpionek == true){
  110.                 x = (e.getX()-27)/69;
  111.                 y = (e.getY()-27)/69;
  112.                 pole = g.szach.pola[x][y];
  113.                 if (pole.getPionek() == null)
  114.                     return;
  115.                 else{
  116.                     if(pole.getPionek().getKolor() != g.kolor)
  117.                         return;
  118.                     else{
  119.                         pionek = pole.getPionek();
  120.                         cpionek = false;
  121.                     }
  122.                 }
  123.             }
  124.             if (cpole == true){
  125.                 x = (e.getX()-27)/69;
  126.                 y = (e.getY()-27)/69;
  127.                 pole = g.szach.pola[x][y];
  128.                 if (pole.getPionek() != null)
  129.                     return;
  130.                 Ruch ruch = pionek.sprawdzRuch(pole);
  131.                 if (ruch.nowePole == null)
  132.                     return;
  133.                 else{
  134.                     String mes = komunikator.przygotujWiadomosc(ruch);
  135.                     System.out.println(mes);
  136.                     komunikator.analizuj(mes);
  137.                     g.repaint();
  138.                     cpionek = true;
  139.                     aktywny = false;
  140.                 }
  141.             }
  142.         }
  143.        
  144.         public void mouseEntered(MouseEvent e) {
  145.         }
  146.      
  147.    
  148.         public void mouseExited(MouseEvent e) {
  149.         }
  150.      
  151.         public void mousePressed(MouseEvent e) {
  152.             x = e.getX();
  153.             y = e.getY();
  154.            
  155.             repaint();
  156.         }
  157.      
  158.         public void mouseReleased(MouseEvent e) {
  159.         }
  160.        
  161.    
  162.     public static void rozgrywka() {
  163.         g.cpionek = true;
  164.         g.cpole = true;
  165.         frame.setSize(new Dimension(601, 624));
  166.         frame.setResizable(false);
  167.         frame.setLocationRelativeTo(null);
  168.         frame.setVisible(true);
  169.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  170.         frame.add(g);
  171.         //while(g.koniec == false){
  172.            
  173.         //}
  174.        
  175. }
  176.     public void start(char kol, PrintWriter wy, BufferedReader we){
  177.         g.kolor = kol;
  178.         g.we = we;
  179.         g.wy = wy;
  180.         g.komunikator.setSzachownica(szach);
  181.         if (kol == 'B')
  182.             g.aktywny = true;
  183.         else
  184.             g.aktywny = false;
  185.         SwingUtilities.invokeLater(new Runnable() {
  186.             public void run() {
  187.                 Gra.rozgrywka();
  188.             }
  189.         });
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment