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;
- @SuppressWarnings("serial")
- 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 printSquere(){
- this.squere();
- Graphics g2 = this.getGraphics();
- g2.drawImage(this.image, 0, 0, (ImageObserver)null);
- g2.dispose();
- }
- public void paint (Graphics g){
- super.paint(g);
- printSquere();
- }
- public void gameOver() {
- JOptionPane.showMessageDialog((Component) null, "Your Score: " + String.valueOf(Ball.points), "GAME OVER", 1);
- this.newGame();
- Ball.lPoints.setText("0");
- }
- public void ball1(int x, int y, int b){
- graphic.setColor(Color.BLACK);
- graphic.fillOval((x*Ball.BALL_SIZE)+2, (y*Ball.BALL_SIZE)+2, Ball.BALL_SIZE-4, Ball.BALL_SIZE-4);
- graphic.setColor(Ball.BALL_COLOUR[b-1]);
- graphic.fillOval((x*Ball.BALL_SIZE)+4, (y*Ball.BALL_SIZE)+3, Ball.BALL_SIZE-7, Ball.BALL_SIZE-6);
- graphic.setColor(Color.WHITE);
- graphic.fillOval((x*Ball.BALL_SIZE)+8, (y*Ball.BALL_SIZE)+8, 7, 7);
- }
- 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.ball1(x, y, this.tabBall[x][y]);
- }
- }
- }
- if (index < 3) {
- this.gameOver();
- }
- }
- 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 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.selectNew();
- } 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 selectSpace(int x, int y) {
- return this.tabBall[x][y] == 0 && this.tabSquere[x][y] == 0;
- }
- public boolean freeSpaces() {
- boolean spaces = false;
- 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.selectSpace(x - 1, y)) {
- ++index;
- this.tabSquere[x - 1][y] = 1;
- spaces= true;
- }
- if (x + 1 < 9 && this.selectSpace(x + 1, y)) {
- ++index;
- this.tabSquere[x + 1][y] = 1;
- spaces = true;
- }
- if (y - 1 >= 0 && this.selectSpace(x, y - 1)) {
- ++index;
- this.tabSquere[x][y - 1] = 1;
- spaces = true;
- }
- if (y + 1 < 9 && this.selectSpace(x, y + 1)) {
- ++index;
- this.tabSquere[x][y + 1] = 1;
- spaces = true;
- }
- }
- }
- }
- } while(index > 0);
- return spaces;
- }
- 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 condition = 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)) {
- condition = true;
- }
- if (this.tabBall[x][y] > 0 && this.line2(x, y)) {
- condition = true;
- }
- }
- }
- return condition;
- }
- 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