Advertisement
Mary_99

GameArea

May 6th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.03 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseListener;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.ImageObserver;
  7. import java.util.Random;
  8.  
  9. @SuppressWarnings("serial")
  10. public class GameArea extends Canvas implements MouseListener {
  11.     public BufferedImage image = new BufferedImage(405, 405, 1);
  12.     public Graphics2D graphic;
  13.     public int[][] tabSquere;
  14.     public int[][] tabBall;
  15.     public byte selectedX;
  16.     public byte selectedY;
  17.     public int tempX;
  18.     public int tempY;
  19.     public int x1;
  20.     public int y1;
  21.     public int x2;
  22.     public int y2;
  23.     public int x3;
  24.     public int y3;
  25.     public boolean selected;
  26.     public Random los;
  27.  
  28.  
  29.     public GameArea()
  30.  
  31.         {   this.graphic = (Graphics2D)this.image.getGraphics();
  32.             this.los = new Random();
  33.             this.addMouseListener(this);
  34.             this.tabSquere = new int[9][9];
  35.             this.tabBall= new int[9][9];
  36.             this.newGame();
  37.         }
  38.  
  39.  
  40.  
  41.  
  42.         public void newGame() {
  43.         for(int x = 0; x < 9; ++x) {
  44.             for(int y = 0; y < 9; ++y) {
  45.                 this.tabBall[x][y] = 0;
  46.                 this.tabSquere[x][y] = 0;
  47.             }
  48.         }
  49.  
  50.         this.newBall();
  51.         this.newBall();
  52.         this.newBall();
  53.         this.newBall();
  54.         Ball.points = 0;
  55.         this.selected = false;
  56.     }
  57.  
  58.  
  59.  
  60.     public void printSquere(){
  61.         this.squere();
  62.         Graphics g2 = this.getGraphics();
  63.         g2.drawImage(this.image, 0, 0, (ImageObserver)null);
  64.         g2.dispose();
  65.     }
  66.  
  67.  
  68.     public void paint (Graphics g){
  69.             super.paint(g);
  70.             printSquere();
  71.     }
  72.  
  73.  
  74.  
  75.     public void gameOver() {
  76.         JOptionPane.showMessageDialog((Component) null, "Your Score: " + String.valueOf(Ball.points), "GAME OVER", 1);
  77.         this.newGame();
  78.         Ball.lPoints.setText("0");
  79.     }
  80.  
  81.  
  82.     public void ball1(int x, int y, int b){
  83.         graphic.setColor(Color.BLACK);
  84.         graphic.fillOval((x*Ball.BALL_SIZE)+2, (y*Ball.BALL_SIZE)+2, Ball.BALL_SIZE-4, Ball.BALL_SIZE-4);
  85.  
  86.         graphic.setColor(Ball.BALL_COLOUR[b-1]);
  87.         graphic.fillOval((x*Ball.BALL_SIZE)+4, (y*Ball.BALL_SIZE)+3, Ball.BALL_SIZE-7, Ball.BALL_SIZE-6);
  88.  
  89.         graphic.setColor(Color.WHITE);
  90.         graphic.fillOval((x*Ball.BALL_SIZE)+8, (y*Ball.BALL_SIZE)+8, 7, 7);
  91.     }
  92.  
  93.  
  94.  
  95.     public void squere() {
  96.  
  97.         int index = 0;
  98.         for(int x = 0; x < 9; ++x) {
  99.             for(int y = 0; y < 9; ++y) {
  100.                 if (this.tabBall[x][y] == 0) {
  101.                     ++index;
  102.                     {
  103.                         this.graphic.setColor(Ball.SQUERE_COLOUR[this.tabSquere[x][y]]);
  104.                         this.graphic.fillRect(x * 45, y * 45, 45, 45);
  105.                         this.graphic.setColor(Ball.BACKGROUNG_COLOUR);
  106.                         this.graphic.drawRect(x * 45, y * 45, 45, 45);
  107.                         if (this.tabBall[x][y] > 0) {
  108.                             this.ball1(x, y, this.tabBall[x][y]);
  109.                         }
  110.                     }
  111.                 }
  112.  
  113.                 if (index < 3) {
  114.                     this.gameOver();
  115.                 }
  116.             }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.             public void clearSelected() {
  123.                 this.selected = false;
  124.  
  125.                 for(int x = 0; x < 9; ++x) {
  126.                     for(int y = 0; y < 9; ++y) {
  127.                         this.tabSquere[x][y] = 0;
  128.                     }
  129.                 }
  130.  
  131.             }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. public void mouseClicked(MouseEvent e) {}
  139.  
  140.  
  141. public void mousePressed(MouseEvent e) {
  142.                 int x = e.getX() / 45;
  143.                 int y = e.getY() / 45;
  144.                 if (e.getButton() == 1) {
  145.                     if (this.tabBall[x][y] > 0 && !this.selected) {
  146.                         this.selected = true;
  147.                         this.selectedX = (byte)x;
  148.                         this.selectedY = (byte)y;
  149.                         this.tabSquere[x][y] = 2;
  150.                         this.printSquere();
  151.                     } else if (this.tabSquere[x][y] == 1) {
  152.                         this.tabBall[x][y] = this.tabBall[this.selectedX][this.selectedY];
  153.                         this.tabBall[this.selectedX][this.selectedY] = 0;
  154.                         this.clearSelected();
  155.                         if (!this.check()) {
  156.                             this.newBall();
  157.                             this.x1 = this.tempX;
  158.                             this.y1 = this.tempY;
  159.                             this.newBall();
  160.                             this.x2 = this.tempX;
  161.                             this.y2 = this.tempY;
  162.                             this.newBall();
  163.                             this.x3 = this.tempX;
  164.                             this.y3 = this.tempY;
  165.                             this.selectNew();
  166.                         } else {
  167.                             Ball.lPoints.setText(String.valueOf(Ball.points));
  168.                         }
  169.  
  170.                         this.printSquere();
  171.                         this.clearSelected();
  172.                     } else {
  173.                         this.clearSelected();
  174.                         this.printSquere();
  175.                     }
  176.                 }
  177.  
  178.             }
  179.  
  180.             public void selectNew() {
  181.                 this.tabSquere[this.x1][this.y1] = 3;
  182.                 this.tabSquere[this.x2][this.y2] = 3;
  183.                 this.tabSquere[this.x3][this.y3] = 3;
  184.             }
  185.  
  186.  
  187.  
  188.             public void mouseReleased(MouseEvent e) {
  189.                 if (this.selected) {
  190.                     if (!this.freeSpaces()) {
  191.                         this.clearSelected();
  192.                     }
  193.  
  194.                     this.printSquere();
  195.                 }
  196.  
  197.             }
  198.  
  199.  
  200. }
  201.  
  202.     public void mouseEntered(MouseEvent e){}
  203.  
  204.     public void mouseExited(MouseEvent e){}
  205.  
  206.     public void clearSelected() {
  207.         this.selected = false;
  208.  
  209.         for(int x = 0; x < 9; ++x) {
  210.             for(int y = 0; y < 9; ++y) {
  211.                 this.tabSquere[x][y] = 0;
  212.             }
  213.         }
  214.  
  215.     }
  216.  
  217.     public boolean selectSpace(int x, int y) {
  218.         return this.tabBall[x][y] == 0 && this.tabSquere[x][y] == 0;
  219.     }
  220.  
  221.     public boolean freeSpaces() {
  222.         boolean spaces = false;
  223.  
  224.  
  225.         do {
  226.             index = 0;
  227.  
  228.             for(int x = 0; x < 9; ++x) {
  229.                 for(int y = 0; y < 9; ++y) {
  230.                     if (this.tabSquere[x][y] > 0) {
  231.                         if (x - 1 >= 0 && this.selectSpace(x - 1, y)) {
  232.                             ++index;
  233.                             this.tabSquere[x - 1][y] = 1;
  234.                             spaces= true;
  235.                         }
  236.  
  237.                         if (x + 1 < 9 && this.selectSpace(x + 1, y)) {
  238.                             ++index;
  239.                             this.tabSquere[x + 1][y] = 1;
  240.                             spaces = true;
  241.                         }
  242.  
  243.                         if (y - 1 >= 0 && this.selectSpace(x, y - 1)) {
  244.                             ++index;
  245.                             this.tabSquere[x][y - 1] = 1;
  246.                             spaces = true;
  247.                         }
  248.  
  249.                         if (y + 1 < 9 && this.selectSpace(x, y + 1)) {
  250.                             ++index;
  251.                             this.tabSquere[x][y + 1] = 1;
  252.                             spaces = true;
  253.                         }
  254.                     }
  255.                 }
  256.             }
  257.         } while(index > 0);
  258.  
  259.         return spaces;
  260.     }
  261.  
  262.     public void newBall() {
  263.         int x;
  264.         int y;
  265.         do {
  266.             x = this.los.nextInt(9);
  267.             y = this.los.nextInt(9);
  268.         } while(this.tabBall[x][y] > 0);
  269.  
  270.         this.tabBall[x][y] = this.los.nextInt(5) + 1;
  271.         this.tempX = x;
  272.         this.tempY = y;
  273.     }
  274.  
  275.     public boolean check() {
  276.         boolean condition = false;
  277.  
  278.         for(int x = 0; x < 9; ++x) {
  279.             for(int y = 0; y < 9; ++y) {
  280.                 if (this.tabBall[x][y] > 0 && this.line1(x, y)) {
  281.                     condition = true;
  282.                 }
  283.  
  284.                 if (this.tabBall[x][y] > 0 && this.line2(x, y)) {
  285.                     condition = true;
  286.                 }
  287.             }
  288.         }
  289.  
  290.         return condition;
  291.     }
  292.  
  293.     public boolean line1(int x, int y) {
  294.         if (this.tabBall[x][y] == 0)
  295.         {
  296.             return false;
  297.         }
  298.         else {
  299.             int index = 0;
  300.             int color = this.tabBall[x][y];
  301.  
  302.             for(int i = x; i < 9; ++i)
  303.             {
  304.                 int j;
  305.                 if (this.tabBall[i][y] != color)
  306.                 {
  307.                     if (index < 5)
  308.                     {
  309.                         return false;
  310.                     }
  311.  
  312.                     for(j = i - 1; j > x - 1; --j)
  313.                     {
  314.                         this.tabBall[j][y] = 0;
  315.                     }
  316.  
  317.                     this.addPoints(index);
  318.                     return true;
  319.                 }
  320.  
  321.                 ++index;
  322.                 if (i == 8)
  323.                 {
  324.                     if (index < 5)
  325.                     {
  326.                         return false;
  327.                     }
  328.  
  329.                     for(j = i; j > x - 1; --j)
  330.                     {
  331.                         this.tabBall[j][y] = 0;
  332.                     }
  333.  
  334.                     this.addPoints(index);
  335.                     return true;
  336.                 }
  337.             }
  338.  
  339.             return false;
  340.         }
  341.     }
  342.  
  343.     public boolean line2(int x, int y)
  344.     {
  345.         if (this.tabBall[x][y] == 0)
  346.         {
  347.             return false;
  348.         }
  349.         else
  350.             {
  351.             int index = 0;
  352.             int color = this.tabBall[x][y];
  353.  
  354.             for(int i = y; i < 9; ++i) {
  355.                 int j;
  356.                 if (this.tabBall[x][i] != color) {
  357.                     if (index < 5) {
  358.                         return false;
  359.                     }
  360.  
  361.                     for(j = i - 1; j > y - 1; --j) {
  362.                         this.tabBall[x][j] = 0;
  363.                     }
  364.  
  365.                     this.addPoints(index);
  366.                     return true;
  367.                 }
  368.  
  369.                 ++index;
  370.                 if (i == 8) {
  371.                     if (index < 5) {
  372.                         return false;
  373.                     }
  374.  
  375.                     for(j = i; j > y - 1; --j) {
  376.                         this.tabBall[x][j] = 0;
  377.                     }
  378.  
  379.                     this.addPoints(index);
  380.                     return true;
  381.                 }
  382.             }
  383.  
  384.             return false;
  385.         }
  386.     }
  387.  
  388.     public void addPoints(int p) {
  389.         switch(p) {
  390.             case 5:
  391.                 Ball.points += 5;
  392.                 break;
  393.             case 6:
  394.                 Ball.Points  += 8;
  395.                 break;
  396.             case 7:
  397.                 Ball.Points  += 15;
  398.                 break;
  399.             case 8:
  400.                 Ball.Points  += 30;
  401.                 break;
  402.             case 9:
  403.                 Ball.Points  += 50;
  404.         }
  405.  
  406.     }
  407.  
  408.  
  409.  
  410.  
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement