Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package klient.warcaby.wykonanie;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- import klient.warcaby.deklaracje.Pionek;
- import klient.warcaby.deklaracje.Pole;
- import klient.warcaby.deklaracje.Ruch;
- import klient.warcaby.deklaracje.Szachownica;
- public class Gra extends JPanel implements MouseListener {
- Komunikator komunikator = new Komunikator();
- PrintWriter wy;
- BufferedReader we;
- Szachownica szach;
- boolean koniec = false;
- boolean zmiana = true;
- private int x, y;
- static JFrame frame = new JFrame("Warcaby");
- static Gra g = new Gra();
- boolean aktywny, cpionek, cpole;
- char kolor;
- Pole pole = null;
- Pionek pionek = null;
- public Gra() {
- szach = new Szachownica();
- addMouseListener(this);
- setPreferredSize(new Dimension(601, 624));
- }
- private BufferedImage image;
- @Override
- public void paintComponent(Graphics g) {
- File imageFile = new File("tlo.jpg");
- try {
- image = ImageIO.read(imageFile);
- } catch (IOException e) {
- System.err.println("Blad odczytu obrazka");
- e.printStackTrace();
- }
- Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
- setPreferredSize(dimension);
- Graphics2D g2d = (Graphics2D) g;
- g2d.drawImage(image, 0, 0, this);
- g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
- int x = -263;
- int i, j;
- for (i = 0; i < 8; i++) {
- int y = -263;
- for (j = 0; j < 8; j++) {
- if (szach.pola[i][j].getPionek() != null)
- if ((szach.pola[i][j].getPionek().getKolor()) != ' ') {
- if ((szach.pola[i][j].getPionek().getKolor()) == 'B') {
- g2d.setColor(Color.WHITE);
- g2d.fillOval(x, y, 50, 50);
- if ((szach.pola[i][j].getPionek().getDamka())) {
- g2d.setColor(Color.RED);
- g2d.fillOval(x+20, y+20, 10, 10);
- }
- }
- if ((szach.pola[i][j].getPionek().getKolor()) == 'C') {
- g2d.setColor(Color.BLACK);
- g2d.fillOval(x, y, 50, 50);
- if ((szach.pola[i][j].getPionek().getDamka())) {
- g2d.setColor(Color.RED);
- g2d.fillOval(x+20, y+20, 10, 10);
- }
- }
- }
- y = y + 69;
- }
- x = x + 69;
- }
- g2d.setColor(Color.WHITE);
- if(this.kolor == 'B')
- g2d.drawString("Biale", 0, 0);
- else
- g2d.drawString("Czarne", 0, 0);
- }
- public void mouseClicked(MouseEvent e) {
- if (aktywny == false)
- return;
- if (cpionek == true){
- x = (e.getX()-27)/69;
- y = (e.getY()-27)/69;
- pole = g.szach.pola[x][y];
- if (pole.getPionek() == null)
- return;
- else{
- if(pole.getPionek().getKolor() != g.kolor)
- return;
- else{
- pionek = pole.getPionek();
- cpionek = false;
- }
- }
- }
- if (cpole == true){
- x = (e.getX()-27)/69;
- y = (e.getY()-27)/69;
- pole = g.szach.pola[x][y];
- if (pole.getPionek() != null)
- return;
- Ruch ruch = pionek.sprawdzRuch(pole);
- if (ruch.nowePole == null)
- return;
- else{
- String mes = komunikator.przygotujWiadomosc(ruch);
- System.out.println(mes);
- komunikator.analizuj(mes);
- g.repaint();
- cpionek = true;
- aktywny = false;
- }
- }
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- x = e.getX();
- y = e.getY();
- repaint();
- }
- public void mouseReleased(MouseEvent e) {
- }
- public static void rozgrywka() {
- g.cpionek = true;
- g.cpole = true;
- frame.setSize(new Dimension(601, 624));
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(g);
- //while(g.koniec == false){
- //}
- }
- public void start(char kol, PrintWriter wy, BufferedReader we){
- g.kolor = kol;
- g.we = we;
- g.wy = wy;
- g.komunikator.setSzachownica(szach);
- if (kol == 'B')
- g.aktywny = true;
- else
- g.aktywny = false;
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- Gra.rozgrywka();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment