Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.image.BufferedImage;
- import java.awt.image.ImageObserver;
- import java.util.Random;
- public class GameArea extends Canvas implements MouseListener {
- public BufferedImage image = new BufferedImage(405, 405, 1);
- public Graphics2D graphic;
- public int[][] tabSquere;
- public int[][] tabBall;
- public byte selectedX;
- public byte selectedY;
- public int tempX;
- public int tempY;
- public int x1;
- public int y1;
- public int x2;
- public int y2;
- public int x3;
- public int y3;
- public boolean selected;
- public Random los;
- public GameArea() {
- this.graphic = (Graphics2D)this.image.getGraphics();
- this.los = new Random();
- this.addMouseListener(this);
- this.tabSquere = new int[9][9];
- this.tabBall = new int[9][9];
- this.newGame();
- }
- public void newGame() {
- for(int x = 0; x < 9; ++x) {
- for(int y = 0; y < 9; ++y) {
- this.tabBall[x][y] = 0;
- this.tabSquere[x][y] = 0;
- }
- }
- this.newBall();
- this.newBall();
- this.newBall();
- this.newBall();
- Ball.points = 0;
- this.selected = false;
- }
- public void paint(Graphics g) {
- super.paint(g);
- this.printSquere();
- }
- public void printSquere() {
- this.squere();
- Graphics g2 = this.getGraphics();
- g2.drawImage(this.image, 0, 0, (ImageObserver)null);
- g2.dispose();
- }
- public void squere() {
- int index = 0;
- for(int x = 0; x < 9; ++x) {
- for(int y = 0; y < 9; ++y) {
- if (this.tabBall[x][y] == 0) {
- ++index;
- }
- this.graphic.setColor(Ball.SQUERE_COLOUR[this.tabSquere[x][y]]);
- this.graphic.fillRect(x * 45, y * 45, 45, 45);
- this.graphic.setColor(Ball.BACKGROUNG_COLOUR);
- this.graphic.drawRect(x * 45, y * 45, 45, 45);
- if (this.tabBall[x][y] > 0) {
- this.oneBall(x, y, this.tabBall[x][y]);
- }
- }
- }
- if (index < 3) {
- this.gameOver();
- }
- }
- public void gameOver() {
- JOptionPane.showMessageDialog((Component)null, "Your Score: " + String.valueOf(Ball.points), "GAME OVER", 1);
- this.newGame();
- Ball.lPoints.setText("0");
- }
- public void oneBall(int x, int y, int k) {
- this.graphic.setColor(Color.BLACK);
- this.graphic.fillOval(x * 45 + 2, y * 45 + 2, 41, 41);
- this.graphic.setColor(Ball.BALL_COLOUR[k - 1]);
- this.graphic.fillOval(x * 45 + 4, y * 45 + 3, 38, 39);
- this.graphic.setColor(Color.WHITE);
- this.graphic.fillOval(x * 45 + 8, y * 45 + 8, 7, 7);
- }
- public void mouseClicked(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- int x = e.getX() / 45;
- int y = e.getY() / 45;
- if (e.getButton() == 1) {
- if (this.tabBall[x][y] > 0 && !this.selected) {
- this.selected = true;
- this.selectedX = (byte)x;
- this.selectedY = (byte)y;
- this.tabSquere[x][y] = 2;
- this.printSquere();
- } else if (this.tabSquere[x][y] == 1) {
- this.tabBall[x][y] = this.tabBall[this.selectedX][this.selectedY];
- this.tabBall[this.selectedX][this.selectedY] = 0;
- this.clearSelected();
- if (!this.check()) {
- this.newBall();
- this.x1 = this.tempX;
- this.y1 = this.tempY;
- this.newBall();
- this.x2 = this.tempX;
- this.y2 = this.tempY;
- this.newBall();
- this.x3 = this.tempX;
- this.y3 = this.tempY;
- this.newBall();
- } else {
- Ball.lPoints.setText(String.valueOf(Ball.points));
- }
- this.printSquere();
- this.clearSelected();
- } else {
- this.clearSelected();
- this.printSquere();
- }
- }
- }
- public void selectNew() {
- this.tabSquere[this.x1][this.y1] = 3;
- this.tabSquere[this.x2][this.y2] = 3;
- this.tabSquere[this.x3][this.y3] = 3;
- }
- public void mouseReleased(MouseEvent e) {
- if (this.selected) {
- if (!this.freeSpaces()) {
- this.clearSelected();
- }
- this.printSquere();
- }
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void clearSelected() {
- this.selected = false;
- for(int x = 0; x < 9; ++x) {
- for(int y = 0; y < 9; ++y) {
- this.tabSquere[x][y] = 0;
- }
- }
- }
- public boolean select(int x, int y) {
- return this.tabBall[x][y] == 0 && this.tabSquere[x][y] == 0;
- }
- public boolean freeSpaces() {
- boolean pola = false;
- int index;
- do {
- index = 0;
- for(int x = 0; x < 9; ++x) {
- for(int y = 0; y < 9; ++y) {
- if (this.tabSquere[x][y] > 0) {
- if (x - 1 >= 0 && this.select(x - 1, y)) {
- ++index;
- this.tabSquere[x - 1][y] = 1;
- pola = true;
- }
- if (x + 1 < 9 && this.select(x + 1, y)) {
- ++index;
- this.tabSquere[x + 1][y] = 1;
- pola = true;
- }
- if (y - 1 >= 0 && this.select(x, y - 1)) {
- ++index;
- this.tabSquere[x][y - 1] = 1;
- pola = true;
- }
- if (y + 1 < 9 && this.select(x, y + 1)) {
- ++index;
- this.tabSquere[x][y + 1] = 1;
- pola = true;
- }
- }
- }
- }
- } while(index > 0);
- return pola;
- }
- public void newBall() {
- int x;
- int y;
- do {
- x = this.los.nextInt(9);
- y = this.los.nextInt(9);
- } while(this.tabBall[x][y] > 0);
- this.tabBall[x][y] = this.los.nextInt(5) + 1;
- this.tempX = x;
- this.tempY = y;
- }
- public boolean check() {
- boolean con = false;
- for(int x = 0; x < 9; ++x) {
- for(int y = 0; y < 9; ++y) {
- if (this.tabBall[x][y] > 0 && this.line1(x, y)) {
- con = true;
- }
- if (this.tabBall[x][y] > 0 && this.line2(x, y)) {
- con = true;
- }
- }
- }
- return con;
- }
- public boolean line1(int x, int y) {
- if (this.tabBall[x][y] == 0) {
- return false;
- } else {
- int index = 0;
- int color = this.tabBall[x][y];
- for(int i = x; i < 9; ++i) {
- int j;
- if (this.tabBall[i][y] != color) {
- if (index < 5) {
- return false;
- }
- for(j = i - 1; j > x - 1; --j) {
- this.tabBall[j][y] = 0;
- }
- this.addPoints(index);
- return true;
- }
- ++index;
- if (i == 8) {
- if (index < 5) {
- return false;
- }
- for(j = i; j > x - 1; --j) {
- this.tabBall[j][y] = 0;
- }
- this.addPoints(index);
- return true;
- }
- }
- return false;
- }
- }
- public boolean line2(int x, int y) {
- if (this.tabBall[x][y] == 0) {
- return false;
- } else {
- int index = 0;
- int color = this.tabBall[x][y];
- for(int i = y; i < 9; ++i) {
- int j;
- if (this.tabBall[x][i] != color) {
- if (index < 5) {
- return false;
- }
- for(j = i - 1; j > y - 1; --j) {
- this.tabBall[x][j] = 0;
- }
- this.addPoints(index);
- return true;
- }
- ++index;
- if (i == 8) {
- if (index < 5) {
- return false;
- }
- for(j = i; j > y - 1; --j) {
- this.tabBall[x][j] = 0;
- }
- this.addPoints(index);
- return true;
- }
- }
- return false;
- }
- }
- public void addPoints(int p) {
- switch(p) {
- case 5:
- Ball.points += 5;
- break;
- case 6:
- Ball.points += 8;
- break;
- case 7:
- Ball.points+= 15;
- break;
- case 8:
- Ball.points += 30;
- break;
- case 9:
- Ball.points += 50;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement