Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple applet problem

Status
Not open for further replies.

flyingsheep

Technical User
Apr 24, 2004
15
US
i'm sorry i have to post this, but i havent been able to figure it out and i have a big project due tomorrow for computer science, what this is is a really bloated tic tac toe applet, for now, all i am looking for is 1. why it only fills the first square i select (from the JOptionPane), any other feedback about the game is welcome, sorry its not such an intelligent snippet, but its the best i can do at the moment.
thanks in advance,
alex

CODE:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.Rectangle;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.awt.font.FontRenderContext;
import java.awt.Color;
public class Chart extends Applet
{
public void paint(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
Graphics2D g3=(Graphics2D)g;
Graphics2D g4=(Graphics2D)g;
Graphics2D g5=(Graphics2D)g;
Graphics2D g6=(Graphics2D)g;
Graphics2D g7=(Graphics2D)g;
Graphics2D g8=(Graphics2D)g;
Graphics2D g9=(Graphics2D)g;
Graphics2D g10=(Graphics2D)g;
Graphics2D g11=(Graphics2D)g;
Graphics2D g12=(Graphics2D)g;
Rectangle box1=new Rectangle(70,100,50,50);
g2.draw(box1);
Rectangle box2=new Rectangle(120,100,50,50);
g3.draw(box2);
Rectangle box3=new Rectangle(170,100,50,50);
g4.draw(box3);
Rectangle box4=new Rectangle(70,150,50,50);
g5.draw(box4);
Rectangle box5=new Rectangle(70,150,100,50);
g6.draw(box5);
Rectangle box6=new Rectangle(70,150,150,50);
g7.draw(box6);
Rectangle box7=new Rectangle(70,200,50,50);
g8.draw(box7);
Rectangle box8=new Rectangle(70,200,100,50);
g9.draw(box8);
Rectangle box9=new Rectangle(70,200,150,50);
g10.draw(box9);
g2.drawString("1", 90,130);
g2.drawString("2", 140,130);
g2.drawString("3", 190,130);
g2.drawString("4", 90,180);
g2.drawString("5", 140,180);
g2.drawString("6", 190,180);
g2.drawString("7", 90,230);
g2.drawString("8", 140,230);
g2.drawString("9", 190,230);
int moveNum=0;
String boxNum="0";
String color="red";
boxNum=JOptionPane.showInputDialog("input box number player");
int box1Istaken=0,box2Istaken=0,box3Istaken=0,box4Istaken=0,box5Istaken=0,box6Istaken=0,box7Istaken=0,box8Istaken=0,box9Istaken=0;
if(boxNum.equals("1"))
{
if(box1Istaken==0)
{
System.out.println("cool");
box1Istaken=1;
moveNum+=1;
g2.fill(box1);
g2.draw(box1);
}
}
if(boxNum.equals("2"))
{
if(box2Istaken==0)
{
System.out.println("cool");
box2Istaken=1;
moveNum+=1;
g3.fill(box2);

}
}
if(boxNum.equals("3"))
{
if(box3Istaken==0)
{
System.out.println("cool");
box3Istaken=1;
moveNum+=1;
g4.fill(box3);

}
}
if(boxNum.equals("4"))
{
if(box4Istaken==0)
{
System.out.println("cool");
box4Istaken=1;
moveNum+=1;
g5.fill(box4);

}
}
if(boxNum.equals("5"))
{
if(box5Istaken==0)
{
System.out.println("cool");
box5Istaken=1;
moveNum+=1;
g6.fill(box5);

}
}
if(boxNum.equals("6"))
{
if(box6Istaken==0)
{
System.out.println("cool");
box6Istaken=1;
moveNum+=1;
g7.fill(box6);

}
}
if(boxNum.equals("7"))
{
if(box7Istaken==0)
{
System.out.println("cool");
box7Istaken=1;
moveNum+=1;
g8.fill(box7);

}
}
if(boxNum.equals("8"))
{
if(box8Istaken==0)
{
System.out.println("cool");
box8Istaken=1;
moveNum+=1;
g9.fill(box8);

}
}
if(boxNum.equals("9"))
{
if(box9Istaken==0)
{
System.out.println("cool");
box9Istaken=1;
moveNum+=1;
g10.fill(box9);

}
}
if(moveNum==9)
{
g2.setColor(Color.red);
g2.drawString("SomeOne Wins", 140,180);
}
}
}
 
This is really messy code..
I could give a hundred remarks and hints about this ..

Use only 1 g var to paint on, the one given in the paint(Graphics g);
In your paint method, all you may do is paint! don't put showInputDialogs in there.
So what you do, is everytime the field wants to paint, you let it draw the rectangles, and then you ask the user for a number. and that number then is run over,and that square is filled. You set your vars but no use,they never get the chance to paint themselves!
You paint method should ONLY paint, so paint your rectangles, then go over the boxIstaken vars and paint them if the vars are set.

You input box should be somewhere else.

e.g:
if(boxNum.equals("1")) //if the user entered number 1
{
if(box1Istaken==0) // if not yet taken
{
//do stuff
System.out.println("cool");
box1Istaken=1;
moveNum+=1;
g2.fill(box1);
g2.draw(box1);
}
}

but here, since the user entered 1, you'll never paint anything anymore.

--------------

The quick and very dirty solution to your problem is:

remove all the if(boxNum.equals("7"))
lines and only check on istaken to paint, and just set the istaken vars accoring to the user input in the beginning, before painting anything. So then you'll set the var and paint after that if the boxes are taken.

Capiche ?


Greetz,
img


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
Anyway, the answer is way to late and what the hell is this doing in a J2ee forum ?
...
:(

Greetz,
img


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top