Hi,
I would be extremely grateful if someone can help me with my problem. I have a
game that I made for a class project. The game can be seen at:
http://www.ecst.csuchico.edu/~ruggieri/csci111/lab7/Lab7.html
I want to have images instead of the gray boxes. I currently have two classes
for this applet.
[code]import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class GameCube extends Applet implements ActionListener, MouseListener
{
Box [][]gamebox = new Box [4][4];
Box boxChoice;
int x,y;
int col, row;
int rint = (int)(Math.random() * 8);
int xStart = 0;
int yStart = 0;
int boxHeight = 100;
int boxWidth = 100;
int count = 0;
Graphics g;
Color colors[]= {Color.blue, Color.red, Color.cyan,
Color.green, Color.magenta, Color.orange, Color.pink,
Color.yellow};
Box boxP[]= new Box[3];
public void init() {
Button newGame = new Button("New Game");
setLayout(new BorderLayout());
add(newGame, BorderLayout.SOUTH);
newGame.addActionListener(this);
addMouseListener(this);
setBackground(Color.white);
setNewGame();
}
public void setNewGame()
{
int chosenColors[] = {0,0,0,0,0,0,0,0};
for(int col = 0; col <=3; col++){
System.out.println("my col value is: "+col);
for(int row = 0; row < 4; row++){
System.out.println("my row value is: " + row);
int y1 = yStart + row * boxHeight;
rint = (int)(Math.random() * 8);
while (chosenColors[rint] >= 2)
{rint = (int)(Math.random() * 8);}
chosenColors[rint] = chosenColors[rint] +1;
int x1 = xStart + col * boxWidth;
gamebox[row][col] = new Box(x1, y1, boxHeight,
boxWidth, colors[rint]);
System.out.println("coordinates: row " + row +
" column " + col);
}
}
repaint();
}
public void paint(Graphics g){
displayGame(g);
}
public void displayGame(Graphics g){
for (int i = 0; i <= 3; i++) {
for (int j = 0; j<= 3; j++){
gamebox[i][j].display(g);
} }
for (int row = 0; row <= 4; row++) {
g.setColor(Color.black);
g.fillRect(row * boxWidth + 0,
yStart, 1, 400);
}
for (int col = 0; col <= 4; col++) {
g.setColor(Color.black);
g.fillRect(xStart, col *
boxHeight + 0, 400, 1);
}
}
public void playGame(Box newestChosen)
{
if (count==0){
boxP[0]=boxChoice;
boxP[0].showColor=true;
}
else if (count==1){
boxP[1]=boxChoice;
boxP[1].showColor=true;
}
else{
boxP[2]=boxChoice;
if (boxP[0].boxColor==boxP[1].boxColor){
boxP[0].showColor=true;
boxP[2].showColor=true;
}
else{
boxP[0].showColor=false;
boxP[1].showColor=false;
}
boxP[0]=boxP[2];
}
System.out.println("boxP["+ count +"]");
}
public void actionPerformed(ActionEvent event)
{
setNewGame();
}
public void mouseClicked (MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
x=e.getX();
y=e.getY();
System.out.println("the coordinates clicked are x " + x
+ "and y" + y);
selectBox(x,y);
boxChoice.chosen();
playGame(boxChoice);
if (count==2)
count =1;
else
count++;
repaint();
}
public void selectBox(int x, int y) {
int locI = (y - xStart)/boxHeight;
int locJ = (x - yStart)/boxWidth;
boxChoice = gamebox[locI][locJ];
}
}
[/code]
[b]and[/b]
[code]import java.awt.Color;
import java.awt.Graphics;
public class Box {
int x;
int y;
int Height;
int Width;
Color boxColor;
boolean showColor = false;
Graphics g;
public Box(int X1,int Y1,int h ,int w, Color myColor){
x = X1;
y = Y1;
Height = h;
Width = w;
boxColor = myColor;
}
public void display (Graphics graph){
g = graph;
if (showColor == false) {
g.setColor(Color.darkGray); //gray boxes
g.fillRect(x, y, Height, Width);}
else {
g.setColor(boxColor);
g.fillRect (x, y, Height, Width);}
}
public void chosen()
{
showColor =true;
}
}
[/code]
The gray boxes are in the above class. I need help asap because this project is
due on Monday and it is vital to my grade. I am done, but I just wanted to add
images instead.
Thanks,
CR
[Message sent by forum member 'teamzerosc' (teamzerosc)]
http://forums.java.net/jive/thread.jspa?messageID=321615
===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[email protected] and include in the body of the message "help".