Multiple Bookings System - java

I am currently developing a booking system as a task for my degree. I need the seating plan to be "saved/stored" for each showing time (radio buttons), so that e.g. If I book 2 tickets at 13:00, I can also book 2 tickets on the same spot for 15:00. What is the best way of doing this?
PS: I'm not making use of a database and I would prefer not to; due to task's requirements.
Here's my code, please run it if you can.
// CM1203 Fundamentals of Computing with Java; Second Assignement.
// Walter Carvalho - C1001984; 2012.
// Cardiff University
// Load Libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class cinemaSystem extends JFrame implements ActionListener {
// Global Variables
boolean lselected, rselected, mselected;
double chargeDue;
int a, b, c;
static Ticket oapticket, childticket, adultticket;
JFrame frame = new JFrame(); // Creates JFrame
JLabel title, lchild, ladult, loap, ltotalprice, time;
JTextField child, adult, oap, totalprice;
JButton submit;
JRadioButton time1, time2, time3, time4, time5; // Radio Butons
JToggleButton l[][], m[][], r[][]; // Names grid of JButtons
JPanel panel1, panel2, panel3, panel4, panel5, panel6;
ArrayList<String> seatsAvailable = new ArrayList<String>();
ArrayList<String> coupon = new ArrayList<String>();
// Constructor
public cinemaSystem(){
title = new JLabel("Cinema Booking System");
title.setFont(new Font("Helvetica", Font.BOLD, 30));
title.setLocation(12,5);
title.setSize(600, 60);
frame.setLayout(null); // Setting Grid Layout
// panel1.setLayout(new GridLayout(seat,row));
l = new JToggleButton[4][4]; // Allocating Size of Grid
panel1 = new JPanel(new GridLayout(4,4));
panel1.setBounds(20, 95, 220, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
l[x][y] = new JToggleButton("L" + y + x); // Creates New JButton
l[x][y].addActionListener(this);
seatsAvailable.add("L" + y + x);
panel1.add(l[x][y]); //adds button to grid
}
}
m = new JToggleButton[4][2]; // Allocating Size of Grid
panel2 = new JPanel(new GridLayout(2,4));
panel2.setBounds(240, 165, 220, 70);
for(int y = 0; y <2 ; y++){
for(int x = 0; x < 4; x++){
m[x][y] = new JToggleButton("M" + y + x); // Creates New JButton
m[x][y].addActionListener(this);
seatsAvailable.add("M" + y + x);
panel2.add(m[x][y]); //adds button to grid
}
}
r = new JToggleButton[4][4]; // Allocating Size of Grid
panel3 = new JPanel(new GridLayout(4,4));
panel3.setBounds(460, 95, 220, 140);
for(int y = 0; y <4 ; y++){
for(int x = 0; x < 4; x++){
r[x][y] = new JToggleButton("R" + y + x); // Creates New JButton
r[x][y].addActionListener(this);
seatsAvailable.add("R" + y + x);
panel3.add(r[x][y]); //adds button to grid
}
}
panel4 = new JPanel(new FlowLayout());
panel4.setBounds(0, 250, 300, 70);
lchild = new JLabel("Child");
child = new JTextField("0", 2);
child.addActionListener(this);
ladult = new JLabel("Adult");
adult = new JTextField("0", 2);
adult.addActionListener(this);
loap = new JLabel("OAP");
oap = new JTextField("0", 2);
oap.addActionListener(this);
submit = new JButton("Submit");
submit.addActionListener(this);
oapticket = new Ticket(4.70);
childticket = new Ticket(3.50);
adultticket = new Ticket(6.10);
child.addKeyListener(new MyKeyAdapter());
oap.addKeyListener(new MyKeyAdapter());
adult.addKeyListener(new MyKeyAdapter());
panel4.add(lchild);
panel4.add(child);
panel4.add(ladult);
panel4.add(adult);
panel4.add(loap);
panel4.add(oap);
panel4.add(submit);
panel5 = new JPanel(new FlowLayout());
panel5.setBounds(240, 250, 300, 70);
ltotalprice = new JLabel("Charge Due (£): ");
totalprice = new JTextField("£0.00", 5);
totalprice.setEnabled(false);
panel5.add(ltotalprice);
panel5.add(totalprice);
panel6 = new JPanel(new FlowLayout());
panel6.setBounds(0, 55, 560, 30);
time = new JLabel("Please select a film time: ");
time1 = new JRadioButton("13:00", true);
time2 = new JRadioButton("15:00", false);
time3 = new JRadioButton("17:00", false);
time4 = new JRadioButton("19:00", false);
time5 = new JRadioButton("21:00", false);
ButtonGroup group = new ButtonGroup();
group.add(time1);
group.add(time2);
group.add(time3);
group.add(time4);
group.add(time5);
panel6.add(time);
panel6.add(time1);
panel6.add(time2);
panel6.add(time3);
panel6.add(time4);
panel6.add(time5);
time1.addActionListener(this);
time2.addActionListener(this);
time3.addActionListener(this);
time4.addActionListener(this);
time5.addActionListener(this);
frame.add(title);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
frame.add(panel4);
frame.add(panel5);
frame.add(panel6);
frame.setPreferredSize(new Dimension(700, 350));
frame.setTitle("Cinema Booking");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
// Calculates Charge Due for current transaction.
public double calcChargeDue(){
DecimalFormat df = new DecimalFormat("#.##");
double chargeDue = 0.0;
chargeDue = (a*childticket.price) + (b*oapticket.price) + (c*adultticket.price);
totalprice.setText("£"+df.format(chargeDue));
return chargeDue;
}
// Method to check if the number of people matches the number of seats selected.
public void check(){
int check = coupon.size();
int noTickets = a + b + c;
if (check != noTickets){
submit.setEnabled(false);
}
else {
submit.setEnabled(true);
}
}
// Ticket Object
public class Ticket {
double price;
// Constructor
public Ticket(double cost) {
price = cost;
}
public double getTicketPrice() {
return price;
}
}
public void actionPerformed(ActionEvent e)
{
a = Integer.parseInt(child.getText());
b = Integer.parseInt(oap.getText());
c = Integer.parseInt(adult.getText());
for(int y = 0; y < 4; y++){
for(int x = 0; x < 4; x++){
lselected = l[x][y].isSelected();
rselected = r[x][y].isSelected();
if (e.getSource() == l[x][y]) {
if(lselected == true){
coupon.add(e.getActionCommand());
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
if (e.getSource() == r[x][y]) {
if(rselected == true){
coupon.add(e.getActionCommand());
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
if (e.getSource() == oap){
calcChargeDue();
check();
}
if (e.getSource() == adult){
calcChargeDue();
check();
}
if (e.getSource() == child){
calcChargeDue();
check();
}
}
}
for(int y = 0; y < 2; y++){
for(int x = 0; x < 4; x++){
mselected = m[x][y].isSelected();
if (e.getSource() == m[x][y]) {
if(mselected == true){
coupon.add(e.getActionCommand());
calcChargeDue();
check();
}
else {
coupon.remove(e.getActionCommand());
check();
}
}
}
}
if(time1 == e.getSource()){
}
if(time2 == e.getSource()){
}
if(time3 == e.getSource()){
}
if(time4 == e.getSource()){
}
if(time5 == e.getSource()){
}
if(submit == e.getSource()) {
for(int y = 0; y < 4; y++){
for(int x = 0; x < 4; x++){
lselected = l[x][y].isSelected();
rselected = r[x][y].isSelected();
if (lselected == true) {
l[x][y].setEnabled(false);
}
if (rselected == true) {
r[x][y].setEnabled(false);
}
}
}
for(int y = 0; y < 2; y++){
for(int x = 0; x < 4; x++){
mselected = m[x][y].isSelected();
if (mselected == true) {
m[x][y].setEnabled(false);
}
}
}
Collections.sort(coupon);
System.out.println("Here's your ticket:");
System.out.println(coupon);
System.out.println("Child: " + child.getText());
System.out.println("Adult: " + adult.getText());
System.out.println("OAP: " + oap.getText());
System.out.println("Total Price: " + totalprice.getText());
System.out.println("Thank you. Enjoy your film.");
System.out.println(" ");
coupon.clear();
child.setText("0");
adult.setText("0");
oap.setText("0");
}
}
// Main Class
public static void main(String[] args) {
new cinemaSystem(); //makes new ButtonGrid with 2 parameters
}
}
Related: Java: Disable all JToggleButtons after Submission — setEnabled(false);

If you already have it working for one booking time, all you need to do is take all your data structures used for storing information about that booking time and double them up to support several independent booking times.
For instance, ArrayList<String> seatsAvailable = new ArrayList<String>(); will become:
Dictionary<Time, ArrayList<String> > seatsAvailable =
new Dictionary<Time, ArrayList<String> >();
Time firstBooking = new Time(13,0,0);
Time secondBooking = new Time(15,0,0);
seatsAvailable.put( firstBooking , new ArrayList<String>() );
seatsAvailable.put( secondBooking , new ArrayList<String>() );
Now you can keep track of two completely seperate ArrayLists of seatsAvailable.

Related

How can i put delay in displaying the final result of my maze

i am creating a maze generation application. what i want to happen is that, when i run my program, it will show the animation on how the maze was created (it will show how it knocks the wall to create a path).
i tried to put delay on some of its parts but it won't run.thank you for the HUGE HELP!
here's the code:
public class Maze extends JPanel {
private Room[][] rooms;// m x n matrix of rooms
private ArrayList<Wall> walls; // List of walls
private Random rand;// for random wall
private int height;// height of matrix
private int width;// width of matrix
private int num;// incrementor
private JoinRoom ds;// union paths
// paint methods //
private int x_cord; // x-axis rep
private int y_cord;// y-axis rep
private int roomSize;
private int randomWall;
private int create;
int mazectr;
public static int m;// these are variables for the size of maze (m x n)
public static int n;
public Maze(int m, int n) {
JPanel j = new JPanel();
final JFrame f = new JFrame();
this.height = m;
this.width = n;
rooms = new Room[m][n];
walls = new ArrayList<Wall>((m - 1) * (n - 1));
long startTime = System.currentTimeMillis();
generateRandomMaze();
long endTime = System.currentTimeMillis();
final JLabel jl = new JLabel("Time Taken: " + (endTime-startTime) + "ms");
final JButton y = new JButton("OK");
j.add(jl);
j.add(y);
f.add(j);
y.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e)
{
f.setVisible(false);
}
});
f.setVisible(true);
f.setSize(100, 100);
//jl.setLocation(1000, 1500);
//jl.setBounds(0, 0, 110, 130);
setPreferredSize(new Dimension(800, 700));
}
private void generateRandomMaze() {
generateInitialRooms();// see next method
ds = new JoinRoom(width * height);
rand = new Random(); // here is the random room generator
num = width * height;
while (num > 1) {
// when we pick a random wall we want to avoid the borders getting eliminated
randomWall = rand.nextInt(walls.size());
Wall temp = walls.get(randomWall);
// we will pick two rooms randomly
int roomA = temp.currentRoom.y + temp.currentRoom.x * width;
int roomB = temp.nextRoom.y + temp.nextRoom.x * width;
// check roomA and roomB to see if they are already members
if (ds.find(roomA) != ds.find(roomB)) {
walls.remove(randomWall);
ds.unionRooms(ds.find(roomA), ds.find(roomB));
temp.isGone = true;
temp.currentRoom.adj.add(temp.nextRoom);
temp.nextRoom.adj.add(temp.currentRoom);
num--;
}// end of if
}// end of while
}
// name the room to display
private int roomNumber = 0;
private static Label input;
private static Label input2;
/**
* Sets the grid of rooms to be initially boxes
* This is self explanitory, we are only creating an reverse L for all
* The rooms and there is an L for the border
*/
private void generateInitialRooms() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// create north walls
rooms[i][j] = new Room(i, j);
if (i == 0) {
rooms[i][j].north = new Wall(rooms[i][j]);
} else {
rooms[i][j].north = new Wall(rooms[i - 1][j], rooms[i][j]);
walls.add(rooms[i][j].north);
}
if (i == height - 1) {
rooms[i][j].south = new Wall(rooms[i][j]);
}
if (j == 0) {
rooms[i][j].west = new Wall(rooms[i][j]);
} else {
rooms[i][j].west = new Wall(rooms[i][j - 1], rooms[i][j]);
walls.add(rooms[i][j].west);
}
if (j == width - 1) {
rooms[i][j].east = new Wall(rooms[i][j]);
}
rooms[i][j].roomName = roomNumber++;// we will name the rooms
}
}
// initalize entrance and exit
rooms[0][0].west.isGone = true;// you can replace .west.isGone with .north.isGone
// this is just saying the roomName for top left is 0
rooms[0][0].roomName = 0;
// we will remove the south wall of the last room
rooms[height - 1][width - 1].south.isGone = true;
// this is just saying the roomName for bottom right is the last element in the mxn room matrix
rooms[height - 1][width - 1].roomName = (height * width);
}
public void paintComponent(Graphics g) {
x_cord = 40;
y_cord = 40;
Thread t = new Thread();
// could have taken height as well as width
// just need something to base the roomsize
roomSize = (width - x_cord) / width + 20;
// temp variables used for painting
int x = x_cord;
int y = y_cord;
for (int i = 0; i <= height - 1; i++) {
for (int j = 0; j <= width - 1; j++) {
if (!(rooms[i][j].north.isGone)) {
g.drawLine(x, y, x + roomSize, y);
}//end of north if
// west wall not there draw the line
if (rooms[i][j].west.isGone == false) {
g.drawLine(x, y, x, y + roomSize);
}// end of west if
if ((i == height - 1) && rooms[i][j].south.isGone == false) {
g.drawLine(x, y + roomSize, x + roomSize,
y + roomSize);
}// end of south if
if ((j == width - 1) && rooms[i][j].east.isGone == false) {
g.drawLine(x + roomSize, y, x + roomSize,
y + roomSize);
}// end of east if
x += roomSize;// change the horizontal
try
{
Thread.sleep(50);
} catch (Exception e) {};
t.start();
}// end of inner for loop
x = x_cord;
y += roomSize;
}// end of outer for loop
}
public static void main(String[] args) throws IOException {
// use JFrame to put the created panel on
String path = "E:\\Iskul\\trys\\tryy\\bin\\GUI.jpg";
File file = new File("E:\\Iskul\\trys\\tryy\\bin\\GUI.jpg");
BufferedImage image = ImageIO.read(file);
File fileRec = new File("E:\\Iskul\\trys\\tryy\\bin\\re.jpg");
BufferedImage imageRec = ImageIO.read(fileRec);
File fileHex = new File("E:\\Iskul\\trys\\tryy\\bin\\hexx.jpg");
BufferedImage imageHex = ImageIO.read(fileHex);
final JFrame frame = new JFrame("Prim's Algorithm");
final JPanel jp = new JPanel();
final JTextField input = new JTextField(10);
final JTextField input2 = new JTextField(10);
final JButton jb = new JButton(new ImageIcon(imageRec));
jb.setBorder(BorderFactory.createEmptyBorder());
final JButton jb1 = new JButton(new ImageIcon(imageHex));
jb1.setBorder(BorderFactory.createEmptyBorder());
final JLabel label = new JLabel(new ImageIcon(image));
jb.setLocation(100, 10);
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(795, 501));
jp.add(input);
jp.add(input2);
frame.add(jp);
jp.add(jb);
jp.add(jb1);
//jb.setImage(image);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//int mazectr = 1;
int m = Integer.valueOf(input.getText());
int n = Integer.valueOf(input2.getText());
frame.remove(label);
frame.remove(jp);
//frame.add(new Maze(m,n));
frame.getContentPane().add(new Maze(m, n));
frame.pack();
}});
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//int m = Integer.valueOf(input.getText());
//int n = Integer.valueOf(input2.getText());
Hexa1 hexa = new Hexa1();
hexa.main();
//hexa.run();
}
});
}}// end of main
Your code wont work, you cant do an sleep in the Paint method, because if you do so, he wont draw, he just waits and at the end he'll draw your whole image.
If you want to do a nice effect, you should draw one step after the other but you should be aware that this is a lot of work to realize...

Get buttons already added in a JFrame

I'm developing a simple 15-puzzle game. I added 16 generated numbered buttons to a JFrame and that was the exercise (University).
I'm now trying to go further and make interactions so I need to get all buttons and put them into a 2D vector in order to calculate where user clicks and if and where the cell could "slide", but I don't know how to get them from the Frame.
Here is the generator code:
public void generation(){
int num;
Random rand = new Random();
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < this.getTot(); i++)
list.add(""+ i);
while(!list.isEmpty()){
do{
num = rand.nextInt(this.getTot());
} while (!list.contains("" + num));
list.remove("" + num);
if(num == 0){
this.add(new Button(" ");
}
else{
this.add(new Button("" + num);
}
}
}
And here is the constructor:
public Base15(int x, int y){
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(x, y));
this.x = x;
this.y = y;
this.generation();
this.cells = new Button[x][y];
}
Thank you.
UPDATE: Followed ufis suggestions and had the 2d array done!
for (int row = 0; row < x; row++){
for (int col = 0; col < y; col++){
do{
num = rand.nextInt(this.getTot());
} while (!list.contains("" + num));
list.remove("" + num);
if(num == 0){
cells[row][col] = new Button(" ", sw, label);
this.add(cells[row][col]);
}else{
cells[row][col] = new Button("" + num, sw, label);
this.add(cells[row][col]);
}
}
Thank you all!
Unless I completely misunderstand your question you can do
JFrame theFrame = new JFrame();
// lots of code here to add buttons
Component[] components = theFrame.getComponents();
for (Component component : components) {
if (component instanceof Button) {
// do something
}
}
But it would be better if you store some reference to all your Buttons as you create / add them to the Frame.
EDIT:
I think you should check the way you create your Buttons when you add them to the frame.
When you do
JFrame theFrame = new JFrame();
for (int i = 0; i < 15; i++) {
theFrame.add(new Button());
}
You have no reference to your Buttons. This is why you need to "get the Buttons from the Frame.
If you do something like
JFrame theFrame = new JFrame();
Button[] buttons = new Button[15];
for (int i = 0; i < 15; i++) {
buttons[i] = new Button();
theFrame.add(buttons[i]);
}
You will not have to loop through all the components at a later stage, because you have reference to the buttons in your buttons array. You can of course make that a Button[][] too. But the win here is that you have the reference to the list of buttons at creation time.
public void resetPanel(JFrame form)
{
Component[] components = form.getContentPane().getComponents();
for(Component component : components)
{
if(component instanceof JButton){
JButton button = (JButton) component;
button.setText("?");
}
}
}

Snake game, start [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have fixed the errors but I am still unable to get the game to start
This is a snake game and I can not get it to start. When I try to run it on Eclipse it keeps giving me
Exception in thread "main" java.lang.NullPointerException
at Snake.createSnake(Snake.java:110)
at Snake.<init>(Snake.java:95)
at Driver.main(Driver.java:6)
I just need someone to please explain and help me fix this. I have asked numerous friends and no one that I know can help me fix it.
public class Driver {
public static void main(String[] args) {
Snake game = new Snake();
}
}
Below is this Snake class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
#SuppressWarnings("serial")
class Snake extends JFrame implements KeyListener, Runnable{
JPanel gamePanel, scorePanel;
JButton extraFood;
JTextArea textAreaScore;
int x = 500;
int y = 250;
int minSnake = 3;
int directionx = 1;
int directiony = 0;
int speed = 50;
int difference = 0;
int oldx = 0;
int oldy = 0;
int score = 0;
boolean food = false;
boolean runLeft = false;
boolean runRight = true;
boolean runUp = true;
boolean runDown = true;
boolean bonusFlag = true;
Random ran = new Random();
JButton[] bc = new JButton[200];
int[] bx = new int[300];
int[] by = new int[300];
Thread myThread;
Point[] bp = new Point[300];
Point bonusp = new Point();
//initializing values
public void Values() {
minSnake = 3;
directionx = 10;
directiony = 0;
difference = 0;
score = 0;
food = false;
runLeft = false;
runRight = true;
runUp = true;
runDown = true;
bonusFlag = true;
}
//sets layout of game
public Snake() {
getContentPane().setBackground(Color.GRAY);
getContentPane().setLayout(null);
gamePanel = new JPanel();
gamePanel.setBackground(Color.LIGHT_GRAY);
gamePanel.setBounds(6, 6, 438, 223);
getContentPane().add(gamePanel);
gamePanel.setLayout(new GridLayout(1, 0, 0, 0));
scorePanel = new JPanel();
scorePanel.setBackground(Color.GRAY);
scorePanel.setBounds(6, 241, 438, 31);
getContentPane().add(scorePanel);
scorePanel.setLayout(null);
textAreaScore = new JTextArea("Your score is:" + score);
textAreaScore.setBackground(Color.LIGHT_GRAY);
textAreaScore.setBounds(0, 0, 303, 31);
scorePanel.add(textAreaScore);
//Exit Game button
JButton btnExitGame = new JButton("Exit Game");
btnExitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnExitGame.setBounds(315, 0, 117, 29);
scorePanel.add(btnExitGame);
btnExitGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setVisible(true);
createSnake();
extraFood = new JButton();
extraFood.setEnabled(false);
addKeyListener(this);
//This starts the game
myThread = new Thread(this);
myThread.start();
}
//Creates snake
public void createSnake() {
for (int i = 0; i < 3; i++){
bc[i] = new JButton("b" + i);
bc[i].setEnabled(false);
gamePanel.add(bc[i]);
bc[i].setBounds(bx[0], by[0], 10, 10);
bx[i + 1] = bx[i] - 10;
by[i + 1] = by[i];
}
}
#SuppressWarnings("deprecation")
void reset(){
Values();
gamePanel.removeAll();
myThread.stop();
createSnake();
textAreaScore.setText("Your score is: " + score);
myThread = new Thread(this);
myThread.start();
}
//As snake eats food, it grows
void snakeGrow(){
bc[minSnake] = new JButton();
bc[minSnake].setEnabled(false);
gamePanel.add(bc[minSnake]);
int a = 10 + (10 * ran.nextInt(48));
int b = 10 + (10 * ran.nextInt(23));
bx[minSnake] = a;
by[minSnake] = b;
bc[minSnake].setBounds(a, b, 10, 10);
minSnake++;
}
//Snake moving logic
void moveForward() {
for (int i = 0; i < minSnake; i++) {
bp[i] = bc[i].getLocation();
}
bx[0] += directionx;
by[0] += directiony;
bc[0].setBounds(bx[0], by[0], 10, 10);
for (int i = 1; i < minSnake; i++) {
bc[i].setLocation(bp[i - 1]);
}
if (bx[0] == x) {
bx[0] = 10;
} else if (bx[0] == 0) {
bx[0] = x - 10;
} else if (by[0] == y) {
by[0] = 10;
} else if (by[0] == 0) {
by[0] = y - 10;
}
if (bx[0] == bx[minSnake - 1] && by[0] == by[minSnake - 1]) {
food = false;
score += 1;
textAreaScore.setText("Your score is: " + score);
if (score % 50 == 0 && bonusFlag == true) {
gamePanel.add(extraFood);
extraFood.setBounds((10 * ran.nextInt(50)), (10 * ran.nextInt(25)), 15, 15);
bonusp = extraFood.getLocation();
bonusFlag = false;
}
}
if (bonusFlag == false) {
if (bonusp.x <= bx[0] && bonusp.y <= by[0] && bonusp.x + 10 >= bx[0] && bonusp.y +
10 >= by[0]) {
gamePanel.remove(extraFood);
score += 100;
textAreaScore.setText("Your score is: " + score);
bonusFlag = true;
}
}
if (food == false) {
snakeGrow();
food = true;
} else {
bc[minSnake - 1].setBounds(bx[minSnake - 1], by[minSnake - 1], 10, 10);
}
gamePanel.repaint();
extracted();
}
#SuppressWarnings("deprecation")
private void extracted() {
show();
}
public void keyPressed(KeyEvent e) {
//Move to the left when player presses the left arrow key
if (runLeft == true && e.getKeyCode() == 37) {
directionx = -10; //Moves to the left by 10 pixels
directiony = 0;
runRight = false;
runUp = true;
runDown = true;
}
//Move up when player presses the up arrow key
if (runUp == true && e.getKeyCode() == 38) {
directionx = 0;
directiony = -10; //Moves up by 10 pixels
runDown = false;
runRight = true;
runLeft = true;
}
//Move to the right when the player presses the right arrow key
if (runRight == true && e.getKeyCode() == 39) {
directionx = +10; //Moves right by 10 pixels
directiony = 0;
runLeft = false;
runUp = true;
runDown = true;
}
//Move down when the player presses the down arrow key
if (runDown == true && e.getKeyCode() == 40) {
directionx = 0;
directiony = +10; //Moves down by 10 pixels
runUp = false;
runRight = true;
runLeft = true;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void run() {
for (;;) {
//Moves the snake forward
moveForward();
try {
Thread.sleep(speed);
} catch (InterruptedException ie) {
}
}
}
}
The problem is that you're not setting the global gamePanel variable in your constructor just a local version of it. Remove the JPanel in front of the variable initialization and it should work.
Same applies to your scorePanel and textAreaScore variables.
You declared view objects (i.e. gamePanel, scorePanel, extraFood, textAreaScore) as member variables of your class Snake, but then declared local variables of the same name in your constructor.
Member variables:
JPanel gamePanel, scorePanel;
JButton extraFood;
JTextArea textAreaScore;
Local variables:
public Snake() {
...
JPanel gamePanel = new JPanel();
...
JPanel scorePanel = new JPanel();
...
JTextArea textAreaScore = new JTextArea("Your score is:" + score);
...
Change the constructor to instantiate your member variables like so:
public Snake() {
...
this.gamePanel = new JPanel();
...
this.scorePanel = new JPanel();
...
this.textAreaScore = new JTextArea("Your score is:" + score);
...
The problem is here:
You defined the global variable:
JPanel gamePanel, scorePanel;
JTextArea textAreaScore;
But you defined dupplicate in:
public Snake() {
JPanel gamePanel = new JPanel();
...
JPanel scorePanel = new JPanel();
...
JTextArea textAreaScore = new JTextArea("Your score is:" + score);
...
}
So, change them to:
public Snake() {
gamePanel = new JPanel();
...
scorePanel = new JPanel();
...
textAreaScore = new JTextArea("Your score is:" + score);
...
}
That's it.
You are shadowing gamePanel in the constructor of Snake. Replace
JPanel gamePanel = new JPanel();
with
gamePanel = new JPanel();
The variables scorePanel and textAreaScore have the same issue.
Side issues:
Don't use KeyListeners in Swing applications. They were not designed to be used with Swing applications. Use Key Bindings instead.
Similarly don't use raw Threads for Swing applications, use a Swing Timer instead

plotting a graph [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Hi there I am quite new to java, however I need to plot a graph in JApplet. I have managed to declare all variables and classes, the equation is working (when compiled on it's own). but all I get is a strait line! can anyone tell me what am I doing wrong please?!
in this applet the user will be asked to insert the values for abcd and the min and max values for the x axes
here is my code.......any help will be gratefully appreciated :)
package CubicEquationSolver;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
public class graphApplet extends JApplet implements ActionListener {
private static class g {
public g() {
}
}
int a;
int b;
int c;
int d;
int minimumX;
int maximumX;
int minimumY;
int maximumY;
int xCoOrdinates[];
int yCoOrdinates[];
int y;
// Setting labels
JLabel labelA = new JLabel("Enter value of A");
JLabel labelB = new JLabel("Enter value of B");
JLabel labelC = new JLabel("Enter value of C");
JLabel labelD = new JLabel("Enter value of D");
JLabel labelMinX = new JLabel("Minimum X value");
JLabel labelMaxX = new JLabel("Maximum X value");
JLabel message = new JLabel("Please insert your values");
// Values will be entered here using JTextField
JTextField textA = new JTextField();
JTextField textB = new JTextField();
JTextField textC = new JTextField();
JTextField textD = new JTextField();
JTextField minX = new JTextField();
JTextField maxX = new JTextField();
JTextField ref = new JTextField("Enter value 0-1");
// declaring the layout for layout manager
JPanel north = new JPanel();
JPanel south = new JPanel();
JPanel west = new JPanel();
JPanel east = new JPanel();
JPanel center = new JPanel();
// declaring buttons using JButtons
JButton calculate = new JButton("Calculate");
JButton delete = new JButton("Delete");
JButton refine = new JButton("Refine");
// Calling from equation class
// equation eq = new equation();
private JPanel panel;
private int width = center.getWidth();
private int height = center.getHeight();
#Override
public void init() {
// setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = this.getContentPane();
this.setSize(900, 480);
this.setVisible(true);
// listener to buttons
calculate.addActionListener(this);
delete.addActionListener(this);
refine.addActionListener(this);
// listeer to user's input
textA.addActionListener(this);
textB.addActionListener(this);
textC.addActionListener(this);
textD.addActionListener(this);
minX.addActionListener(this);
maxX.addActionListener(this);
ref.addActionListener(this);
// assigning colours to panels to be distinguished
north.setBackground(Color.LIGHT_GRAY);
south.setBackground(Color.LIGHT_GRAY);
west.setBackground(Color.YELLOW);
east.setBackground(Color.GREEN);
center.setBackground(Color.GRAY);
// Declaring border
BorderLayout layoutBorder = new BorderLayout();
// setting up the grid (x rows, y clumns, space, space)
GridLayout layoutGrid = new GridLayout(2, 8, 4, 4);
// layout grid
north.setLayout(layoutGrid);
// set labels
north.add(labelA);
north.add(labelB);
north.add(labelC);
north.add(labelD);
north.add(labelMinX);
north.add(labelMaxX);
north.add(ref);
// calculate button
north.add(calculate);
// text boxes
north.add(textA);
north.add(textB);
north.add(textC);
north.add(textD);
north.add(minX);
north.add(maxX);
north.add(refine);
// delete button
north.add(delete);
south.add(message);
// border layout
c.add(north, BorderLayout.NORTH);
c.add(south, BorderLayout.SOUTH);
c.add(center, BorderLayout.CENTER);
// c .add(west, BorderLayout.WEST);
// c .add(east, BorderLayout.EAST);
// panel = new JPanel();
// panel.setPreferredSize(new Dimension(width, height));
// panel.setBackground(Color.GRAY);
// center.add(panel);
}
#Override
public void actionPerformed(ActionEvent e) // throws NumberFormatException
{
// dafault message will be <message> -- "Please insert values"
message.setText(e.getActionCommand());
// when button "Delete" is pressed all values in text firlds will turn
// null
if (e.getActionCommand().equals("Delete")) {
message.setForeground(Color.DARK_GRAY);
textA.setText(null);
textB.setText(null);
textC.setText(null);
textD.setText(null);
minX.setText(null);
maxX.setText(null);
repaint();
} else if (e.getActionCommand().equals("Calculate"))
// when "Calculate" button is pressed, values will be attached to
// equation
try {
message.setForeground(Color.DARK_GRAY);
// -------------------------------------------------
a = Integer.parseInt(textA.getText());
b = Integer.parseInt(textB.getText());
c = Integer.parseInt(textC.getText());
d = Integer.parseInt(textD.getText());
minimumX = Integer.parseInt(minX.getText());
maximumX = Integer.parseInt(maxX.getText());
System.out.println("center.getWidth() " + center.getWidth());
System.out.println("center.getHeight() " + center.getHeight());
System.out.println("minimum " + minX.getText());
System.out.println("maximum " + maxX.getText());
System.out.println("a " + textA.getText());
System.out.println("b " + textB.getText());
System.out.println("c " + textC.getText());
System.out.println("d " + textD.getText());
// ------------------------------------------------------
message.setText("This is the result for " + "A "
+ textA.getText() + ", B " + textB.getText() + ", C "
+ textC.getText() + ", D " + textD.getText());
draw();
}
catch (NumberFormatException ex)
// if user inputs other than numbers, a warning message in the south
// panel will show
{
message.setText("Please insert numerical value in "
+ ex.getMessage());
message.setForeground(Color.red);
message.setFont(new Font("Tahoma", Font.BOLD, 12));
}
else if (e.getActionCommand().equals("Refine")) {
// for refine
}
}
// ===================================================================================
private void calculation() {
xCoOrdinates = new int[(maximumX - minimumX) + 1];
yCoOrdinates = new int[(maximumX - minimumX) + 1];
for (int i = 0; i < xCoOrdinates.length; i++)
// for(int j = 0; j < yCoOrdinates.length; j++)
{
// generating the x co-ordinates and storing them in arrays
xCoOrdinates[i] = minimumX + i;
// generating the y co-ordinates using the formula given
y = ((a * (int) Math.pow(i, 3)) + (b * (int) Math.pow(i, 2))
+ (c * i) + (d));
// storing y co-ordinates
yCoOrdinates[i] = y;
// displaying results
// System.out.println("X = " + i + " Y = " + getY());
System.out.println("These are the values of X = " + i);
}
// printing the y axes values
for (int i = 0; i < yCoOrdinates.length; i++) {
System.out.println("this is the extracted Y " + yCoOrdinates[i]);
}
maximumX = xCoOrdinates[0];
maximumX = xCoOrdinates[0];
for (int i = 1; i < yCoOrdinates.length; i++) {
if (yCoOrdinates[i] > maximumX)
maximumX = xCoOrdinates[i];
else if (yCoOrdinates[i] < minimumX)
minimumX = xCoOrdinates[i];
}
System.out.println("MAXX is " + maximumX);
System.out.println("MINX is " + minimumX);
maximumY = yCoOrdinates[0];
minimumY = yCoOrdinates[0];
for (int i = 1; i < yCoOrdinates.length; i++) {
if (yCoOrdinates[i] > maximumY)
maximumY = yCoOrdinates[i];
else if (yCoOrdinates[i] < minimumY)
minimumY = yCoOrdinates[i];
}
System.out.println("MAXY is " + maximumY);
System.out.println("MINY is " + minimumY);
}
// =================================================================================================
public void draw() {
Graphics g = center.getGraphics();
g.setColor(Color.GRAY);
g.fillRect(0, 0, center.getWidth(), center.getHeight());
// g.fillRect(25,25, center.getWidth()-50, center.getHeight()-50);
double x, y, nextX, nextY;
int xPoint; // = 0;
int yPoint; // = 0;
int nextXpoint; // = 0;
int nextYpoint; // = 0;
g.setColor(Color.BLUE);
for (xPoint = 0; xPoint <= (double) center.getWidth(); xPoint++) {
x = scaleX(xPoint);
y = equation(x);
yPoint = scaleY(y);
nextXpoint = xPoint + 1;
nextX = scaleX(nextXpoint);
nextY = equation(nextX);
nextYpoint = scaleY(nextY);
g.drawLine(xPoint, yPoint, nextXpoint, nextYpoint);
// System.out.println("equation --->" + eq.getY());
}
}
private double equation(double x) {
return y;
// return a*x*x*x + b*x*x + c*x + d;
}
private double scaleX(int xPoint) {
int minXstart = minimumX;
int maxXend = maximumX;
double xScale = (double) center.getWidth() / (maxXend - minXstart);
return (xPoint - (center.getWidth() / 2)) / xScale;
}
private int scaleY(double y) {
int minYstart = minimumY;
int maxYend = maximumY;
int yCoord;
double yScale = (double) center.getHeight() / (maxYend - minYstart);
yCoord = (int) (-y * yScale) + (int) (center.getHeight() / 2);
return yCoord;
}
}
There are 3rd party libraries out there to do this type of work for you. Check out:
JFreeChart
Charts4J
Short term help: Check your output, do the values of x and y vary as expected? are they actually used during plotting? Use a debugger to step through your code. Then come back with a more focused questions.
Mid term (within the next two days): Please buy and read the following two books:
http://www.amazon.com/Pragmatic-Programmer-Andrew-Hunt/dp/020161622X
http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?s=books&ie=UTF8&qid=1342369060&sr=1-1&keywords=clean+code
Classes as long as the one you posted are not acceptable, and make it next to impossible to understand the revlevant part. This is the reason why you received the down votes I'd guess.
I'm a JFreeChart fan, but you can also plot graphs using Cartesian coordinates in Java 2D, as suggested here.

How to block a spot when playing tic tac toe, if i clicked on it, or when the game is over

I have a problem, I have a game, tic tac toe, and i can't figure out how to make it so when someone wins or someone moves it blocks the certain spot, and when someone wins it will not let you do anything, it is inside a JFrame, I can always keep changing the O and X
Here is the code
package project;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Project extends JFrame
{
static JButton startButton = new JButton();
static ImageIcon image = new ImageIcon("src/project/imagesX1.jpg");
static ImageIcon imageX = new ImageIcon("src/project/images1.jpg");
static JButton restart = new JButton();
static JLabel label0 = new JLabel();
static JLabel label1 = new JLabel();
static JLabel label2 = new JLabel();
static JLabel label3 = new JLabel();
static JLabel label4 = new JLabel();
static JLabel label5 = new JLabel();
static JLabel label6 = new JLabel();
static JLabel label7 = new JLabel();
static JLabel label8 = new JLabel();
static JLabel vert1 = new JLabel();
static JLabel vert2 = new JLabel();
static JLabel horiz1 = new JLabel();
static JLabel horiz2 = new JLabel();
static JLabel showWinner = new JLabel();
static int[] gridMark = new int[9];
static boolean xTurn = true;
static int numberClicks = 0;
static Timer drawTimer;
static JLabel[] choiceLabel = new JLabel[9];
static int[] labelValue = new int[9];
static int screen = 0;
static int whosTurn = 0;
static int counter = 0;
public static void main(String[] args)
{
new Project().show();
}
public Project()
{
setTitle("Tic Tac Toe");
setVisible(true);
setResizable(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints myGrid = new GridBagConstraints();
getContentPane().setBackground(Color.white);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight());
for (int i = 0; i < 9; i++)
{
gridMark[i] = 0;
}
for (int i = 0; i < 9; i++)
{
labelValue[i] = 0;
}
choiceLabel[0] = label0;
choiceLabel[1] = label1;
choiceLabel[2] = label2;
choiceLabel[3] = label3;
choiceLabel[4] = label4;
choiceLabel[5] = label5;
choiceLabel[6] = label6;
choiceLabel[7] = label7;
choiceLabel[8] = label8;
//ROW 1
int x = 0;
for (int i = 0; i < 3; i++)
{
myGrid = new GridBagConstraints();
choiceLabel[i].setBackground(Color.white);
choiceLabel[i].setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
choiceLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
choiceLabel[i].setOpaque(true);
choiceLabel[i].setForeground(Color.black);
myGrid.gridx = x;
x+=2;
myGrid.gridy = 0;
//gridConstraints.insets = new Insets(10, 10, 10, 10);
myGrid.ipadx = 40;
myGrid.ipady = 40;
getContentPane().add(choiceLabel[i], myGrid);
choiceLabel[i].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
labelMouseClicked(e);
}
});
}
//ROW 2
x = 0;
for (int i = 3; i < 6; i++)
{
myGrid = new GridBagConstraints();
choiceLabel[i].setBackground(Color.white);
choiceLabel[i].setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
choiceLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
choiceLabel[i].setOpaque(true);
choiceLabel[i].setForeground(Color.black);
myGrid.gridx = x;
x+=2;
myGrid.gridy = 3;
myGrid.ipadx = 40;
myGrid.ipady = 40;
getContentPane().add(choiceLabel[i], myGrid);
choiceLabel[i].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
labelMouseClicked(e);
}
});
}
//ROW 3
x = 0;
for (int i = 6; i < 9; i++)
{
myGrid = new GridBagConstraints();
choiceLabel[i].setBackground(Color.white);
choiceLabel[i].setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
choiceLabel[i].setHorizontalAlignment(SwingConstants.CENTER);
choiceLabel[i].setOpaque(true);
choiceLabel[i].setForeground(Color.black);
myGrid.gridx = x;
x+=2;
myGrid.gridy = 5;
myGrid.ipadx = 40;
myGrid.ipady = 40;
getContentPane().add(choiceLabel[i], myGrid);
choiceLabel[i].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
labelMouseClicked(e);
}
});
}
myGrid = new GridBagConstraints();
horiz1.setOpaque(true);
horiz1.setBackground(Color.blue);
myGrid.gridx = 0;
myGrid.gridy = 1;
myGrid.gridwidth = 5;
myGrid.ipadx = 500;
myGrid.ipady = 2;
getContentPane().add(horiz1, myGrid);
myGrid = new GridBagConstraints();
horiz2.setOpaque(true);
horiz2.setBackground(Color.blue);
myGrid.gridx = 0;
myGrid.gridy = 4;
myGrid.gridwidth = 5;
myGrid.ipadx = 500;
myGrid.ipady = 2;
getContentPane().add(horiz2, myGrid);
myGrid = new GridBagConstraints();
vert1.setOpaque(true);
vert1.setBackground(Color.blue);
vert1.setVerticalAlignment(SwingConstants.CENTER);
myGrid.gridx = 1;
myGrid.gridy = 0;
myGrid.gridheight = 6;
myGrid.ipadx = 2;
myGrid.ipady = 500;
//gridConstraints.insets = new Insets(10, 0, 10, 0);
getContentPane().add(vert1, myGrid);
myGrid = new GridBagConstraints();
vert2.setOpaque(true);
vert2.setBackground(Color.blue);
myGrid.gridx = 3;
myGrid.gridy = 0;
myGrid.gridheight = 6;
myGrid.ipadx = 2;
myGrid.ipady = 500;
//gridConstraints.insets = new Insets(10, 0, 10, 0);
getContentPane().add(vert2, myGrid);
myGrid = new GridBagConstraints();
showWinner.setBackground(Color.white);
showWinner.setOpaque(true);
myGrid.gridx = 0;
myGrid.gridy = 6;
myGrid.gridwidth = 5;
myGrid.gridheight = 6;
getContentPane().add(showWinner, myGrid);
restart.setText("Restart");
myGrid.gridx = 1;
myGrid.gridy = 7;
getContentPane().add(restart, myGrid);
restart.setVisible(false);
restart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
restartActionPerformed(e);
}
});
pack();
}
private void labelMouseClicked(MouseEvent e)
{
Component clickedComponent = e.getComponent();
int choice;
for (choice = 0; choice < 9; choice++)
{
if (clickedComponent == choiceLabel[choice])
{
break;
}
}
choiceLabel[choice].setBackground(Color.WHITE);
if (whosTurn == 0)
{
choiceLabel[choice].setIcon(image);
labelValue[choice] = 1;
whosTurn = 1;
}
else if (whosTurn == 1)
{
choiceLabel[choice].setIcon(imageX);
labelValue[choice] = 5;
whosTurn = 0;
}
counter += 1;
checkWinner();
}
public static void checkWinner()
{
if (labelValue[0] + labelValue[1] + labelValue[2] == 3 || labelValue[3] + labelValue[4] + labelValue[5] == 3 || labelValue[6] + labelValue[7] + labelValue[8] == 3 ||
labelValue[0] + labelValue[3] + labelValue[6] == 3 || labelValue[1] + labelValue[4] + labelValue[7] == 3 || labelValue[2] + labelValue[5] + labelValue[8] == 3 ||
labelValue[0] + labelValue[4] + labelValue[8] == 3 || labelValue[2] + labelValue[4] + labelValue[6] == 3)
{
showWinner.setFont(new Font("Arial" , Font.BOLD, 30));
showWinner.setText("X wins!");
restart.setVisible(true);
}
else if (labelValue[0] + labelValue[1] + labelValue[2] == 15 || labelValue[3] + labelValue[4] + labelValue[5] == 15 || labelValue[6] + labelValue[7] + labelValue[8] == 15 ||
labelValue[0] + labelValue[3] + labelValue[6] == 15 || labelValue[1] + labelValue[4] + labelValue[7] == 15 || labelValue[2] + labelValue[5] + labelValue[8] == 15 ||
labelValue[0] + labelValue[4] + labelValue[8] == 15 || labelValue[2] + labelValue[4] + labelValue[6] == 15)
{
showWinner.setFont(new Font("Arial" , Font.BOLD, 30));
showWinner.setText("O wins!");
restart.setVisible(true);
}
else if (counter == 9)
{
showWinner.setFont(new Font("Arial" , Font.BOLD, 30));
showWinner.setText("It's a draw");
restart.setVisible(true);
}
}
public static int getGridSelected (int a, int b, int c, int d)
{
if(gridMark[a] + gridMark[b] + gridMark[c] == d)
{
if(gridMark[a] == 0) return a;
else if(gridMark[b] == 0) return b;
else if(gridMark[c] == 0) return c;
}
return -1;
}
public void restartActionPerformed(ActionEvent e)
{
}
}
AI FOR FRAME:
public static void makeMove()
{
myFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
int gridSelected;
int y;
Random r = new Random();
Graphics myGraphics = myFrame.getGraphics();
//find gridSelected for "can win"
if (getGridSelected(0,1,2,10) != -1) gridSelected = getGridSelected(0,1,2,10);
else if (getGridSelected(3,4,5,10) != -1) gridSelected = getGridSelected(3,4,5,10);
else if (getGridSelected(6,7,8,10) != -1) gridSelected = getGridSelected(6,7,8,10);
else if (getGridSelected(0,3,6,10) != -1) gridSelected = getGridSelected(0,3,6,10);
else if (getGridSelected(1,4,7,10) != -1) gridSelected = getGridSelected(1,4,7,10);
else if (getGridSelected(2,5,8,10) != -1) gridSelected = getGridSelected(2,5,8,10);
else if (getGridSelected(0,4,8,10) != -1) gridSelected = getGridSelected(0,4,8,10);
else if (getGridSelected(2,4,6,10) != -1) gridSelected = getGridSelected(2,4,6,10);
//find gridSelected for "block win"
else if (getGridSelected(0,1,2,2) != -1) gridSelected = getGridSelected(0,1,2,2);
else if (getGridSelected(3,4,5,2) != -1) gridSelected = getGridSelected(3,4,5,2);
else if (getGridSelected(6,7,8,2) != -1) gridSelected = getGridSelected(6,7,8,2);
else if (getGridSelected(0,3,6,2) != -1) gridSelected = getGridSelected(0,3,6,2);
else if (getGridSelected(1,4,7,2) != -1) gridSelected = getGridSelected(1,4,7,2);
else if (getGridSelected(2,5,8,2) != -1) gridSelected = getGridSelected(2,5,8,2);
else if (getGridSelected(0,4,8,2) != -1) gridSelected = getGridSelected(0,4,8,2);
else if (getGridSelected(2,4,6,2) != -1) gridSelected = getGridSelected(2,4,6,2);
//is the center available?
else if (gridMark[4] == 0)gridSelected = 4;
//if all else fail pick a random blank square
else
{
while(true)
{
gridSelected = r.nextInt(9);
if (gridMark[gridSelected] == 0)break;
}
}
int x = 40 + (gridSelected % 3) * 80;
if (gridSelected <= 2) y = 50;
else if (gridSelected <= 5) y = 130;
else y = 210;
// draw O
gridMark[gridSelected] = 5;
myGraphics.setColor(Color.red);
myGraphics.drawOval(x, y, 60, 60);
checkWinner();
xTurn = true;
if (numberClicks != 10)
{
numberClicks++;
}
}
In labelMouseClicked() you have to check if the field is already clicked. For example check choiceLabel[choice].getBackground():
if (choiceLabel[choice].getBackground().equal(Color.WHITE))
{
// already clicked
return;
}
To check if the game is over you may return a boolean value in checkWinner() or better make a new function for it: isGameEnd(). You could use it in labelMouseClicked() and checkWinner().
public class Project extends JFrame
{
static boolean gameHasEnded;
...
public Project()
{
...
gameHasEnded = false;
}
...
private void labelMouseClicked(MouseEvent e)
{
// If any player has already won, abort. Label will not be changed.
// Players turn will not be switched.
if (gameHasEnded) return;
...
// If the label has already been clicked, abort.
if (labelValue[choice] != 0) return;
choiceLabel[choice].setBackground(Color.WHITE);
...
}
public static void checkWinner()
{
...
restart.setVisible(true);
gameHasEnded = true;
...
restart.setVisible(true);
gameHasEnded = true;
...
restart.setVisible(true);
gameHasEnded = true;
...
}
public void restartActionPerformed(ActionEvent e)
{
for (int i = 0; i < 9; i++)
{
labelValue[i] = 0;
choiceLabel[i].setIcon(null);
choiceLabel[i].setBackground(Color.white);
}
showWinner.setText("");
restart.setVisible(false);
counter = 0;
whosTurn = 0;
gameHasEnded = false;
}
}
Edit: You seem to have two "grids". The JFrame version uses labelValue and the Fame version gridMark.
Remove the declarations of the fields gridMark, numberClicks and xTurn, since they should not be used.
Replace all references to gridMark with labelValue.
Replace all references to numberClicks with counter.
Change xTurn = true; to whosTurn = 0;.
Replace the drawing commands. (E.g. myGraphics.drawOval(x, y, 60, 60);) with choiceLabel[gridSelected].setIcon(imageX);
After the call to checkWinner() in labelMouseClicked, insert this:
if (!gameHasEnded)
{
makeMove();
}
One tip: Remove all fields you are not using in the JFrame-version. This will make all references to Frame-version variables to be highlighted as errors.

Categories