Вы находитесь на странице: 1из 9

package donjeweled;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class DonJeweledPanel extends JPanel {


private static final Dimension PREFERRED_DIM = new Dimension(800,1200);
private final static int COLS = 8,ROWS = 12;
private static final int SQ= (int) (PREFERRED_DIM.getWidth()/COLS);
private JProgressBar timeLeftBar;

private static final int TOP_PADDING = (int) (PREFERRED_DIM.getHeight()-


ROWS*SQ);
private List<Image> backGroundList = new ArrayList();
private int TEXT_YC = 20;
private int timeElapsed;
private Timer timer;
private Don[][] donGrid;
private int firstCol,firstRow, secondCol, secondRow;
boolean firstClick = true;
int score = 0;
private static Don[] donList = {
new red(), new blue(), new green(), new orange(), new white(), n
ew purple(), new yellow()
};
public DonJeweledPanel() {
// See below for the proper way to open images and other types o
f
// resources
System.out.println("Opening the panel!!");
openImages();
this.setPreferredSize(PREFERRED_DIM);
// See the method below for the BEST way to set up ways to inter
act with
// mouse events. Similar to interact with dragging and moving m
ouse
setUpClickListener();
setUpGame();
setUpTimer();
setVisible(true);
timer.start();
}

private void setUpGame() {


// set up the grid.
// add Dons to the Grid
donGrid = new Don[ROWS][COLS];
for (int r=0; r<donGrid.length; r++) {
for (int c=0; c<donGrid[0].length; c++) {
int donNum = (int) (6*Math.random());
System.out.println(donNum);
donGrid[r][c] = donList[donNum];
while (threeInRow(r,c)==true) {
donNum = (int) (6*Math.random());
donGrid[r][c] = donList[donNum];
}
}
}
addDons();
}
private boolean threeInRow(int row, int col) {

try {
if (donGrid[row][col].equals(donGrid[row][col-1]) && don
Grid[row][col].equals(donGrid[row][col-2])) {
return true;
}
} catch (Exception e) {}

try {
if (donGrid[row][col].equals(donGrid[row-1][col]) && don
Grid[row][col].equals(donGrid[row-2][col])) {
return true;
}
} catch (Exception e) {}

return false;
}

private boolean removeImage(){


for (int row = 0; row<donGrid.length; row++) {
for (int col = 0; col<donGrid[0].length; col++){
if (fullThreeInRow(row, col)){
if (isHorizontal(row,col) == false) {
int k = row+1;
score +=100;
Don oldDon = donGrid[row][col];
donGrid[row][col] = null;
try {
while (donGrid[k][col].e
quals(oldDon)){
donGrid[k][col]=
null;
k++;
score +=100;
}
}
catch (Exception e) {
}
k = row-1;
try {
while (donGrid[k][col].e
quals(oldDon)){
donGrid[k][col]=
null;
k--;
score +=100;
}
}
catch (Exception e){
}
}
else {
Don oldDon = donGrid[row][col];
int l = col+1;
donGrid[row][col] = null;
try {
while (donGrid[row][l].e
quals(oldDon)){
donGrid[row][l]
= null;
l++;
score +=100;
}
}
catch (Exception e) {
}
l = col-1;
try {
while (donGrid[row][l].e
quals(oldDon)){
donGrid[row][l]
= null;
l--;
score +=100;
}
}
catch (Exception e) {
}
}
}

}
}

return false;
}
private boolean fullThreeInRow(int row, int col) {

try {
if (donGrid[row][col].equals(donGrid[row][col-1]) && don
Grid[row][col].equals(donGrid[row][col+1])) {
return true;
}
} catch (Exception e) {}

try {
if (donGrid[row][col].equals(donGrid[row-1][col]) && don
Grid[row][col].equals(donGrid[row+1][col])) {
return true;
}
} catch (Exception e) {}

return false;
}

private boolean isHorizontal(int row, int col) {

try {
if (donGrid[row][col].equals(donGrid[row][col-1]) && don
Grid[row][col].equals(donGrid[row][col+1])) {
return true;
}
} catch (Exception e) {}

try {
if (donGrid[row][col].equals(donGrid[row-1][col]) && don
Grid[row][col].equals(donGrid[row+1][col])) {
return false;
}
} catch (Exception e) {}

return false;
}

private void setUpTimer() {


timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeElapsed++;
updateProgressBar();
repaint();
}
});
}
// We would like to see a progress bar get smaller and smaller
// as time elapses.
protected void updateProgressBar() {
}

private void setUpClickListener() {


// Whoever has focus is who can interact with mouse and keyboard
, etc
this.requestFocusInWindow();
// similar to having an entity ready to interact with the Mouse
this.addMouseListener(new MouseListener() {
/*
* If you want to detect mouse dragging, then use a mous
eMotionListener
*/
@Override
public void mouseClicked(MouseEvent arg0) {
// I like to avoid using this one because if you
are moving
// the mouse while you are trying to click, it s
ometimes doesn't
// register the click. A click is a press and r
elease at the
// same location
}
@Override
public void mouseEntered(MouseEvent arg0) {
// when the mouse enters the panel, this is call
ed
}
@Override
public void mouseExited(MouseEvent arg0) {
// duh...
}
@Override
public void mousePressed(MouseEvent click) {
// use this method when you wish to detect a cli
ck
System.out.println(click);
// here are some things you can do with a MouseE
vent
System.out.println(click.getX());
System.out.println(click.getPoint());
System.out.println(click.getButton());// try cli
cking left and right buttons
//// So... What should we do when we detect a cl
ick?
// This is a good way to isolate the clicking an
d the behavior
// to be done when the JPanel is clicked
clickedAt(click);
}
@Override
public void mouseReleased(MouseEvent arg0) {
// use this to find out when the mouse was relea
sed
}
});
}

private boolean shouldSwitch() {


for (int row = 0; row<donGrid.length; row++) {
for (int col = 0; col<donGrid[0].length; col++){
if (threeInRow(row,col)){
return true;
}
}
}
return false;

}
protected void clickedAt(MouseEvent click) {
// This is called when the panel is clicked. What should we do?
if (firstClick==true) {
firstRow = click.getX()/SQ;
firstCol = click.getY()/SQ;
firstClick = false;
System.out.println("FIRST CLICK");
}
else {
secondRow = click.getX()/SQ;
secondCol = click.getY()/SQ;
System.out.println("SecondClick!!!!!!!!!!!!");

if ((firstRow == secondRow && Math.abs(firstCol-secondCo


l) <=1) ||
(firstCol == secondCol && Math.abs(first
Row-secondRow) <=1)){
Don temp = donGrid[firstCol][firstRow];
donGrid[firstCol][firstRow] = donGrid[secondCol]
[secondRow];
donGrid[secondCol][secondRow] = temp;
if (!shouldSwitch()){
donGrid[secondCol][secondRow]=donGrid[fi
rstCol][firstRow];
donGrid[firstCol][firstRow] = temp;
}
}
while (shouldSwitch()){
removeImage();
dropDons();
}
firstClick = true;
}
repaint();
dropDons();
System.out.println(score);
}
// this is called any time new Dons need to be added
private void addDons() {
// in case there are some empty spots, tell all the Dons in the
// grid to check to see if any empty spots below them
dropDons();
// add Dons to each column and have them drop. Repeat until col
is full.
// repeat for each column

}
// Starting at bottom, if there are any empty spots, have those spots ge
t filled by
// the Dons above, if there are any non-null Dons above
private void dropDons() {
for (int r=0; r<donGrid.length; r++) {
for (int c=0; c<donGrid[0].length; c++) {
if (donGrid[r][c] == null) {
int row = r;
while (row>0) {
donGrid[row][c] = donGrid[row-1]
[c];
row--;
}
int num = (int) (6 * Math.random());
donGrid[0][c] = donList[num];
}
}
}
}

private void printDons() {


for(Don[] row: donGrid) {
for(Don d:row) {
System.out.print(d);
}
System.out.println();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// System.out.println("painting the panel");
drawBackground(g);
for(int r = 0; r < donGrid.length; r++) {
for(int c = 0; c< donGrid[0].length;c++) {
Don d = donGrid[r][c];
// System.out.print
ln(r+" , "+c+" is a "+d);
if(d != null) {
//
System.out.println("drawing "+d);
d.draw(g,c*SQ,TOP_PADDING + SQ*r,SQ, SQ)
;
}
}
}
drawExtraStuff(g);
}
private void drawExtraStuff(Graphics g) {
// TODO Auto-generated method stub
}

private void drawBackground(Graphics g) {


// TODO Auto-generated method stub
}

private void openImages() {


/*
*Only Open Images needed in this class that are needed
*by this class. Images that each Don uses will be opened
*in that Don class.
**/
try {
URL url = getClass().getResource("res/images/green.png")
;
backGroundList.add( ImageIO.read(url));
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
}
try {
URL url = getClass().getResource("res/images/blue.png");
backGroundList.add( ImageIO.read(url));
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
}
try {
URL url = getClass().getResource("res/images/green.png")
;
backGroundList.add( ImageIO.read(url));
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
} try {
URL url = getClass().getResource("res/images/orange.png"
);
backGroundList.add( ImageIO.read(url));
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
} try {
URL url = getClass().getResource("res/images/purple.png"
);
backGroundList.add( ImageIO.read(url));
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
} try {
URL url = getClass().getResource("res/images/white.png")
;
backGroundList.add( ImageIO.read(url));
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
} try {
URL url = getClass().getResource("res/images/yellow.png"
);
backGroundList.add( ImageIO.read(url));
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
}
}
}

Вам также может понравиться