new JLabel? Position? *FIXED*

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2925 Views - Last Post: 20 June 2011 - 06:44 PM Rate Topic: -----

#1 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

new JLabel? Position? *FIXED*

Posted 18 June 2011 - 08:01 PM

Within this class, I have it have an instance variable of the score JLabel:
	transient JLabel score;


Next, it will call the createGUI() method once the object of the class is created:
	public void createGUI() {
		//Unnecessary Code Here
		JPanel scorePanel = new JPanel();
		scorePanel.setPreferredSize(new Dimension(290, 25));
		
		score = new JLabel("Wins: " + wins + " Losses: " + losses + " Ties: " + ties + " Percentage: " + ratio);
		scorePanel.add(BorderLayout.CENTER, score);
		frame.add(BorderLayout.SOUTH, scorePanel);
		//Unnecessary Code Here
	}


Once a round of tic-tac-toe is over, it calls the updateLabel() method to change the win/loss/tie/ratio label:
	public void updateLabel() {
		score.setText("Wins: " + wins + " Losses: " + losses + " Ties: " + ties + " Percentage: " + ratio);
	}



However, when the updateLabel() method gets called, ANOTHER label is created in the top left corner of the contentPane. It does increment fine (I didn't show that code here, it's not necessary), and does update the one at the bottom, but it also creates another one. How can I stop it from creating the new one at the top left? Gosh, thanks D.I.C!

This post has been edited by maniacalsounds: 20 June 2011 - 07:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: new JLabel? Position? *FIXED*

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: new JLabel? Position? *FIXED*

Posted 18 June 2011 - 08:28 PM

One thing for sure, you didn't show us the code that generate that ghost JLabel.

Your code, the part you showed us, should work. You update the text of the JLabel score that is located in the CENTER region of the scorePanel.
Was This Post Helpful? 0
  • +
  • -

#3 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 18 June 2011 - 08:33 PM

Oh, I'm sorry. I'm just not sure where that portion of the code is, because those are the only instances of "score." in my program...

I also have no other JLabels in my Java application...
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: new JLabel? Position? *FIXED*

Posted 18 June 2011 - 08:42 PM

Look, Jlabel do not appear and disappear
May be your code interfere with the layouts and do something not really standard

Unless you show your code, it will be a waste of time trying to speculate on where you screw up your display.
Was This Post Helpful? 0
  • +
  • -

#5 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 18 June 2011 - 08:45 PM

Okay, I'll just put it all up.

tictactoe.java:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.*;

public class tictactoe implements MouseListener, Serializable {
	private static final long serialVersionUID = -1316872218357386859L;
	transient JFrame frame;
	static tictactoe program;
	transient board board;
	transient Boolean isyourturn = true;
	int wins;
	int losses;
	int ratio;
	int ties;
	transient JLabel score;
	transient Boolean finished = false;
	transient int turns = 0;
	
	public static void main(String[] args) {
		program = new tictactoe();
		try {
			FileInputStream fin = new FileInputStream("history.ser");
			ObjectInputStream oin = new ObjectInputStream(fin);
			//Create objects for de-serialization.
			
			program.wins = oin.readInt();
			program.losses = oin.readInt();
			program.createRatio();
			program.ties = oin.readInt();
			
			oin.close();
			fin.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		program.createGUI();
	}
	
	public void createRatio() {
		program.ratio = wins / (wins + losses);
	}
	
	public void incrementLabels(String condition) {
		if (condition == "win") {
			wins++;
			if (losses == 0) {
				ratio = 100;
				//Can't divide by zero, so just set it to 100 percent.
			} else {
				ratio = (wins / (wins + losses)) * 100;
			}
			program.updateLabel();
		} else if (condition == "loss") {
			losses++;
			ratio = (wins / (wins + losses)) * 100;
			program.updateLabel();
		}
	}
	
	public void updateLabel() {
		score.setText("Wins: " + wins + " Losses: " + losses + " Ties: " + ties + " Percentage: " + ratio);
	}
	
	public void createGUI() {
		frame = new JFrame("Nathan's Tic-Tac-Toe");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setPreferredSize(new Dimension(290, 325));
		frame.pack();
		frame.setBackground(Color.white);
		//Create GUI JFrame and set its size, etc.
		
		board = new board();
		frame.add(BorderLayout.CENTER, board);
		//Add the board to the JFrame
		
		frame.addMouseListener(this);
		
		JPanel scorePanel = new JPanel();
		scorePanel.setPreferredSize(new Dimension(290, 25));
		
		score = new JLabel("Wins: " + wins + " Losses: " + losses + " Ties: " + ties + " Percentage: " + ratio);
		scorePanel.add(BorderLayout.CENTER, score);
		frame.add(BorderLayout.SOUTH, scorePanel);
		
		frame.setResizable(false);
		frame.setVisible(true);
		//Make the GUI JFrame visible.
	}
	
	public void mouseClicked(MouseEvent e) {
		if (finished == true) {
			return;
		}
		
		if (checkWin()[0] == "Player" || checkWin()[0] == "Computer" || checkWin()[0] == "Tie") {
			isyourturn = false;
			return;
		}
		
		if (isyourturn == false) {
			return;
			//If it's not your turn, don't allow it to do anything with a mouse click.
		}
		
		if (program.turns == 9) {
			isyourturn = false;
			return;
		}
			
		program.turns++;
		//Increment number of turns.
		
		int xpos = e.getX() - 8;
		int ypos = e.getY() - 30;
		int left = 0;
		int top = 0;
		//Get mouse location
		
		if (xpos < 100) {
			left = 1;
		} else if (xpos > 100 && xpos < 200) {
			left = 2;
		} else if (xpos > 200) {
			left = 3;
		} else if (xpos == 100 || xpos == 200) {
			return;
		}
		
		if (ypos < 100) {
			top = 3;
		} else if (ypos > 100 && ypos < 200) {
			top = 2;
		} else if (ypos > 200) {
			top = 1;
		} else if (ypos == 100 || ypos == 200) {
			return;
		}
		
		if (board.gamegrid[left - 1][top - 1] == "X" || board.gamegrid[left - 1][top - 1] == "O") {
			isyourturn = false;
			return;
			//Spot is already taken
		} else {
			//If the spot is already taken, then don't do anything!
			board.gamegrid[left - 1][top - 1] = "X";
			//Put an X in the array that contains all info about places
			board.repaint();
			//Repaint the board
			isyourturn = false;
			//Set the isyourturn variable to false
			if (checkWin()[0] == "Player" || checkWin()[0] == "Computer") {
				isyourturn = false;
				return;
				//If there's a winner now, return.
			}
			//Check to see if you win!
			opponentTurn();
			//Have the opponent take a turn
			isyourturn = true;
			//Back to your turn!
			if (checkWin()[0] == "Player" || checkWin()[0] == "Computer") {
				isyourturn = false;
				return;
				//If there's a winner now, return.
			}
		}
	}
	
	public void opponentTurn() {
		if (program.turns == 9) {
			isyourturn = false;
			checkWin();
		} else {
			int x = (int) Math.floor((Math.random() * 3));
			int y = (int) Math.floor((Math.random() * 3));
			//Create the random indexes to check.
			
			if (board.gamegrid[x][y] == "X" || board.gamegrid[x][y] == "O") {
				opponentTurn();
				//If the space was taken, redo this method to find new random numbers.
			} else {
				board.gamegrid[x][y] = "O";
				board.repaint();
			}
		}
	}
	
	public String[] checkWin() {

		/*
		 * Have the checkWin() evaluate if someone won, and return an array of Strings
		 * OUTLINE OF win array:
		 * result[0]: The name of the winner.
		 * result[1]: The x location of where a winning box is.
		 * result[2]: The y location of where a winning box is.
		 * result[3]: The x location of where a winning box is.
		 * result[4]: The y location of where a winning box is.
		 * result[5]: The x location of where a winning box is.
		 * result[6]: The y location of where a winning box is.
		 * 
		 * The win array is found in the board class.
		 */
		
		for (int x = 1; x <= 3; x++) {
			//Check for vertical wins
			String first = "";
			String second = "";
			String third = "";
			String firstindex = "";
			String secondindex = "";
			String thirdindex = "";
			
			for (int y = 1; y <= 3; y++) {
				//Remember to decrease x and y by one, due to Java having an array's index start at 0
				
				if (y == 1) {
					first = board.gamegrid[x - 1][0];
					firstindex = String.valueOf(x - 1);
				} else if (y == 2) {
					second = board.gamegrid[x - 1][1];
					secondindex = String.valueOf(x - 1);
				} else {
					third = board.gamegrid[x - 1][2];
					thirdindex = String.valueOf(x - 1);
				}
			}
			
			if (first == second && second == third) {
				//Winner!!
				if (first == "X") {
					board.win[0] = "Player";
					board.win[1] = firstindex;
					board.win[2] = "0";
					board.win[3] = secondindex;
					board.win[4] = "1";
					board.win[5] = thirdindex;
					board.win[6] = "2";
					board.repaint();
					program.incrementLabels("win");
					program.serialize();
					finished = true;
					return board.win;
				} else if (first == "O") {
					board.win[0] = "Computer";
					board.win[0] = "Player";
					board.win[1] = firstindex;
					board.win[2] = "0";
					board.win[3] = secondindex;
					board.win[4] = "1";
					board.win[5] = thirdindex;
					board.win[6] = "2";
					board.repaint();
					program.incrementLabels("loss");
					program.serialize();
					finished = true;
					return board.win;
				}
				//Return the name of the winner
			}
		}
		
		for (int y = 1; y <= 3; y++) {
			//Check for horizontal wins
			String first = "";
			String second = "";
			String third = "";
			String firstindex = "";
			String secondindex = "";
			String thirdindex = "";
			
			for (int x = 1; x <= 3; x++) {
				//Remember to decrease x and y by one, due to Java having an array's index start at 0
				
				if (x == 1) {
					first = board.gamegrid[0][y - 1];
					firstindex = String.valueOf(y - 1);
				} else if (x == 2) {
					second = board.gamegrid[1][y - 1];
					secondindex = String.valueOf(y - 1);
				} else {
					third = board.gamegrid[2][y - 1];
					thirdindex = String.valueOf(y - 1);
				}
			}
			
			if (first == second && second == third) {
				//Winner!!
				if (first == "X") {
					board.win[0] = "Player";
					board.win[1] = "0";
					board.win[2] = firstindex;
					board.win[3] = "1";
					board.win[4] = secondindex;
					board.win[5] = "2";
					board.win[6] = thirdindex;
					board.repaint();
					program.incrementLabels("win");
					program.serialize();
					finished = true;
					return board.win;
				} else if (first == "O") {
					board.win[0] = "Computer";
					board.win[1] = "0";
					board.win[2] = firstindex;
					board.win[3] = "1";
					board.win[4] = secondindex;
					board.win[5] = "2";
					board.win[6] = thirdindex;
					board.repaint();
					program.incrementLabels("loss");
					program.serialize();
					finished = true;
					return board.win;
				}
				//Return the name of the winner
			}
		}
		
		if (board.gamegrid[0][0] == board.gamegrid[1][1] && board.gamegrid[1][1] == board.gamegrid[2][2]) {
			//Diagonal win!
			if (board.gamegrid[1][1] == "X") {
				board.win[0] = "Player";
				board.win[1] = "0";
				board.win[2] = "0";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "2";
				board.repaint();
				program.incrementLabels("win");
				program.serialize();
				finished = true;
				return board.win;
			} else if (board.gamegrid[1][1] == "O") {
				board.win[0] = "Computer";
				board.win[1] = "0";
				board.win[2] = "0";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "2";
				board.repaint();
				program.incrementLabels("loss");
				program.serialize();
				finished = true;
				return board.win;
			}
		}
		
		if (board.gamegrid[0][2] == board.gamegrid[1][1] && board.gamegrid[1][1] == board.gamegrid[2][0]) {
			//Diagonal win!
			if (board.gamegrid[0][2] == "X") {
				board.win[0] = "Player";
				board.win[1] = "0";
				board.win[2] = "2";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "0";
				board.repaint();
				program.incrementLabels("win");
				program.serialize();
				finished = true;
				return board.win;
			} else if (board.gamegrid[0][2] == "O") {
				board.win[0] = "Computer";
				board.win[1] = "0";
				board.win[2] = "2";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "0";
				board.repaint();
				program.incrementLabels("loss");
				program.serialize();
				finished = true;
				return board.win;
			}
		}
		
		if (program.turns == 9) {
			//Max number of turns and nobody wins still. It's a tie.
			//Placeholder
			board.win[0] = "Tie";
			return board.win;
		}
		
		board.win[0] = "Nobody";
		return board.win;
	}
	
	public void serialize() {
		try {
			FileOutputStream fout = new FileOutputStream("history.ser");
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			//Create fout and oout for serialization
			
			oout.writeInt(wins);
			oout.writeInt(losses);
			oout.writeInt(ties);
			//Write the variables I need later
			
			oout.close();
			fout.close();
			//I'm done - close the streams
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void mousePressed(MouseEvent e) {
		//Don't care
	}

	public void mouseReleased(MouseEvent e) {
		//Don't care
	}

	public void mouseEntered(MouseEvent e) {
		//Don't care
	}

	public void mouseExited(MouseEvent e) {
		//Don't care
	}
}



board.java:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;

public class board extends JPanel {
	String[][] gamegrid = new String[3][3];
	String[] win = new String[7];
	
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		
		g2d.setColor(Color.white);
		g2d.drawRect(0, 0, 300, 325);
		
		g2d.setColor(Color.black);
		
		g2d.setStroke(new BasicStroke(1));
		
		g2d.drawLine(100, 0, 100, 300);
		g2d.drawLine(200, 0, 200, 300);
		g2d.drawLine(0, 100, 300, 100);
		g2d.drawLine(0, 200, 300, 200);
		
		for (int x = 1; x <= 3; x++) {
			for (int y = 1; y <= 3; y++) {
				//Remember, arrays start at an index of 0, so we'll have to decrese all indexes by one.
				if(gamegrid[x - 1][y - 1] == "X") {
					//We have a match! So let's calculate the points.
					
					int x1 = 0;
					int y1 = 0;
					int x2 = 0;
					int y2 = 0;
					
					if (x == 1) {
						x1 = 0;
					} else if (x == 2) {
						x1 = 100;
					} else {
						x1 = 200;
					}
					
					x2 = x1 + 100;
					
					if (y == 1) {
						y1 = 200;
						y2 = 300;
					} else if (y == 2) {
						y1 = 100;
						y2 = 200;
					} else {
						y1 = 0;
						y2 = 100;
					}
					
					g2d.drawLine(x1, y1, x2, y2);
					
					g2d.drawLine(x1, y2, x2, y1);
				}
				if(gamegrid[x - 1][y - 1] == "O") {
					//Opponent has a match! Let's calculate the points.
					
					int x1 = 0;
					int y1 = 0;
					
					if (x == 1) {
						x1 = 0;
					} else if (x == 2) {
						x1 = 100;
					} else {
						x1 = 200;
					}
					
					if (y == 1) {
						y1 = 200;
					} else if (y == 2) {
						y1 = 100;
					} else {
						y1 = 0;
					}
					
					g2d.drawOval(x1, y1, 100, 100);
				}
			}
		}
		
		if (win[6] != null) {
			//Someone won. Let's draw the line through all the boxes that are part of the win.
			
			int x1 = Integer.parseInt(win[1]);
			int y1 = Integer.parseInt(win[2]);
			int x2 = Integer.parseInt(win[5]);
			int y2 = Integer.parseInt(win[6]);
			
			if (x1 == 0) {
				x1 = 50;
			} else if (x1 == 1) {
				x1 = 150;
			} else {
				x1 = 250;
			}
			
			if (x2 == 0) {
				x2 = 50;
			} else if (x2 == 1) {
				x2 = 150;
			} else {
				x2 = 250;
			}
			
			if (y1 == 0) {
				y1 = 250;
			} else if (y1 == 1) {
				y1 = 150;
			} else {
				y1 = 50;
			}
			
			if (y2 == 0) {
				y2 = 250;
			} else if (y2 == 1) {
				y2 = 150;
			} else {
				y2 = 50;
			}
			
			System.out.println("(" + x1 + ", " + y1 + ") - (" + x2 + ", " + y2 +")");
			
			g2d.setStroke(new BasicStroke(3));
			g2d.setColor(Color.red);
			
			g2d.drawLine(x1, y1, x2, y2);
		}
	}
}



Oh hey, maybe a picture would help:
Correct way:
Posted Image
Wrong way:
Posted Image

I can't quite seem to pinpoint why it SOMETIMES displays the JLabel...

This post has been edited by maniacalsounds: 18 June 2011 - 08:56 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: new JLabel? Position? *FIXED*

Posted 18 June 2011 - 09:14 PM

The frame.pack() method can be called on frames containing JComponent containing JComponent that are known by the GUI.
In your case, the GUI cannot determine what is the size of your drawing inside the JPanel

You cannot use frame.pack() on a JFrame containing JPanel on which you do drawing yourself.

At a certain moment, you scorePanel is packed and the only thing appearing is the SOUTH region region with its JLabel (that the GUI knows its dimension)

replace frame.pack() by frame.setSize(400,400); or something like that

Also you scorePanel() has a FlowLayout not a BorderLayout so putting the JLabel BorderLayout.SOUTH is kind of hazardous and the syntax is

scorePanel.add(score, BorderLayout.CENTER);
not
scorePanel.add(BorderLayout.CENTER, score);
Was This Post Helpful? 1
  • +
  • -

#7 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 18 June 2011 - 09:24 PM

I've made those changes as suggested, but I still am having the troubles.

I also added this snippet of code:
scorePanel.setLayout(new BorderLayout());


I'm not quite sure if that does exactly what I think it does. Wouldn't that make it safe and fine to position it via a BorderLayout? (That's what I did.)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: new JLabel? Position? *FIXED*

Posted 18 June 2011 - 09:35 PM

This one too is wrong

frame.add(BorderLayout.CENTER, board);

and as your scorePanel contains only a JLabel no need to create a JPanel for nothing

Forget your scorePanel and just do

frame.add(score, BorderLayout.SOUTH);

Works for me :)
Was This Post Helpful? 1
  • +
  • -

#9 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 19 June 2011 - 06:06 AM

I wish I could say the same. Sadly, I'm still having trouble with my label popping up at the top left location. I updated my createGUI() method to get rid of the scorePanel JPanel (Which that part is working perfectly, thanks. That was a lot of unnecessary code) However, my label still pops up from time to time.

Updated createGUI() method:
public void createGUI() {
		frame = new JFrame("Nathan's Tic-Tac-Toe");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(307, 355));
		frame.setBackground(Color.white);
		//Create GUI JFrame and set its size, etc.
		
		board = new board();
		frame.add(board, BorderLayout.CENTER);
		//Add the board to the JFrame
		
		frame.addMouseListener(this);
		
		score = new JLabel("Wins: " + wins + " Losses: " + losses + " Ties: " + ties + " Percentage: " + ratio + "%");
		frame.add(score, BorderLayout.SOUTH);
		
		frame.setResizable(false);
		frame.setVisible(true);
	}



This is driving me nuts. Sorry to be persistent.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: new JLabel? Position? *FIXED*

Posted 19 June 2011 - 09:47 AM

Your board does not contain any JComponent
make it null layout rather than its default FlowLayout

actually using a Canvas might be a better idea
Was This Post Helpful? 1
  • +
  • -

#11 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 19 June 2011 - 12:45 PM

Kill me, please.

Okay, so I switched to a Canvas.

I switched "extends JPanel" to "extends Canvas"
public class board extends Canvas {


I changed paintComponent() to paint()
public void paint(Graphics g) {


However, I can't test it because when I click on the screen, it won't repaint()?

Thanks for sticking with me through this. I'm such a n00b.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: new JLabel? Position? *FIXED*

Posted 19 June 2011 - 01:55 PM

It was a suggestion. You can also keep it as JPanel
add a constructor to it

public board extends JPanel {

   board() {
     super(null);    // put it a null layout
   }



For a Canavas to receive user input you migth have (not 100% sure) to
canvas.setFocusable(true);
canvas.requestFocus();
Was This Post Helpful? 1
  • +
  • -

#13 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 19 June 2011 - 02:32 PM

I put it back as a JPanel, with your suggestion. I'm still having the same glitch.

I'll paste my updated board class:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class board extends JPanel {
	private static final long serialVersionUID = -7675219776569552809L;
	String[][] gamegrid = new String[3][3];
	String[] win = new String[7];
	
	public board() {
		super(null);
	}
	
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		
		g2d.setColor(Color.black);
		
		g2d.setStroke(new BasicStroke(1));
		
		g2d.drawLine(100, 0, 100, 300);
		g2d.drawLine(200, 0, 200, 300);
		g2d.drawLine(0, 100, 300, 100);
		g2d.drawLine(0, 200, 300, 200);
		
		for (int x = 1; x <= 3; x++) {
			for (int y = 1; y <= 3; y++) {
				//Remember, arrays start at an index of 0, so we'll have to decrese all indexes by one.
				if(gamegrid[x - 1][y - 1] == "X") {
					//We have a match! So let's calculate the points.
					
					int x1 = 0;
					int y1 = 0;
					int x2 = 0;
					int y2 = 0;
					
					if (x == 1) {
						x1 = 0;
					} else if (x == 2) {
						x1 = 100;
					} else {
						x1 = 200;
					}
					
					x2 = x1 + 100;
					
					if (y == 1) {
						y1 = 200;
						y2 = 300;
					} else if (y == 2) {
						y1 = 100;
						y2 = 200;
					} else {
						y1 = 0;
						y2 = 100;
					}
					
					g2d.drawLine(x1, y1, x2, y2);
					
					g2d.drawLine(x1, y2, x2, y1);
				}
				if(gamegrid[x - 1][y - 1] == "O") {
					//Opponent has a match! Let's calculate the points.
					
					int x1 = 0;
					int y1 = 0;
					
					if (x == 1) {
						x1 = 0;
					} else if (x == 2) {
						x1 = 100;
					} else {
						x1 = 200;
					}
					
					if (y == 1) {
						y1 = 200;
					} else if (y == 2) {
						y1 = 100;
					} else {
						y1 = 0;
					}
					
					g2d.drawOval(x1, y1, 100, 100);
				}
			}
		}
		
		if (win[6] != null) {
			//Someone won. Let's draw the line through all the boxes that are part of the win.
			
			int x1 = Integer.parseInt(win[1]);
			int y1 = Integer.parseInt(win[2]);
			int x2 = Integer.parseInt(win[5]);
			int y2 = Integer.parseInt(win[6]);
			
			if (x1 == 0) {
				x1 = 50;
			} else if (x1 == 1) {
				x1 = 150;
			} else {
				x1 = 250;
			}
			
			if (x2 == 0) {
				x2 = 50;
			} else if (x2 == 1) {
				x2 = 150;
			} else {
				x2 = 250;
			}
			
			if (y1 == 0) {
				y1 = 250;
			} else if (y1 == 1) {
				y1 = 150;
			} else {
				y1 = 50;
			}
			
			if (y2 == 0) {
				y2 = 250;
			} else if (y2 == 1) {
				y2 = 150;
			} else {
				y2 = 50;
			}
			
			System.out.println("(" + x1 + ", " + y1 + ") - (" + x2 + ", " + y2 +")");
			
			g2d.setStroke(new BasicStroke(3));
			g2d.setColor(Color.red);
			
			g2d.drawLine(x1, y1, x2, y2);
		}
	}
}




Any other suggestions? :P

This post has been edited by maniacalsounds: 19 June 2011 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#14 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 19 June 2011 - 07:11 PM

Anybody else see anyproblems with these classes that would cause the JLabel score to be duplicated and positioned at the top left?

tictactoe.java:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.*;

public class tictactoe implements MouseListener, Serializable {
	private static final long serialVersionUID = -1316872218357386859L;
	transient JFrame frame;
	static tictactoe program;
	transient board board;
	transient Boolean isyourturn = true;
	int wins;
	int losses;
	double ratio;
	int ties;
	transient JLabel score;
	transient Boolean finished = false;
	transient int turns = 0;
	
	public static void main(String[] args) {
		program = new tictactoe();
		try {
			FileInputStream fin = new FileInputStream("history.ser");
			ObjectInputStream oin = new ObjectInputStream(fin);
			//Create objects for de-serialization.
			
			program.wins = oin.readInt();
			program.losses = oin.readInt();
			program.createRatio();
			program.ties = oin.readInt();
			
			oin.close();
			fin.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		program.createGUI();
	}
	
	public void createRatio() {
		program.ratio = Math.round((double) wins / (double) (wins + losses) * 100);
	}
	
	public void incrementLabels(String condition) {
		if (condition == "win") {
			wins++;
			if (losses == 0) {
				ratio = 100d;
				//Can't divide by zero, so just set it to 100 percent.
			} else {
				ratio = Math.round((double) wins / (double) (wins + losses) * 100);
			}
			program.updateLabel();
		} else if (condition == "loss") {
			losses++;
			ratio = Math.round((double) wins / (double) (wins + losses) * 100);
			program.updateLabel();
		}
	}
	
	public void updateLabel() {
		score.setText("Wins: " + wins + " Losses: " + losses + " Ties: " + ties + " Percentage: " + ratio + "%");
	}
	
	public void createGUI() {
		frame = new JFrame("Nathan's Tic-Tac-Toe");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(307, 355));
		frame.setBackground(Color.white);
		//Create GUI JFrame and set its size, etc.
		
		board = new board();
		frame.add(board, BorderLayout.CENTER);
		//Add the board to the JFrame
		
		frame.addMouseListener(this);
		
		score = new JLabel("Wins: " + wins + " Losses: " + losses + " Ties: " + ties + " Percentage: " + ratio + "%");
		frame.add(score, BorderLayout.SOUTH);
		
		frame.setResizable(false);
		frame.setVisible(true);
	}
	
	public void mouseClicked(MouseEvent e) {
		if (finished == true) {
			return;
		}
		
		if (checkWin()[0] == "Player" || checkWin()[0] == "Computer" || checkWin()[0] == "Tie") {
			isyourturn = false;
			return;
		}
		
		if (isyourturn == false) {
			return;
			//If it's not your turn, don't allow it to do anything with a mouse click.
		}
		
		if (program.turns == 9) {
			isyourturn = false;
			return;
		}
			
		program.turns++;
		//Increment number of turns.
		
		int xpos = e.getX() - 8;
		int ypos = e.getY() - 30;
		int left = 0;
		int top = 0;
		//Get mouse location
		
		if (xpos < 100) {
			left = 1;
		} else if (xpos > 100 && xpos < 200) {
			left = 2;
		} else if (xpos > 200) {
			left = 3;
		} else if (xpos == 100 || xpos == 200) {
			return;
		}
		
		if (ypos < 100) {
			top = 3;
		} else if (ypos > 100 && ypos < 200) {
			top = 2;
		} else if (ypos > 200) {
			top = 1;
		} else if (ypos == 100 || ypos == 200) {
			return;
		}
		
		if (board.gamegrid[left - 1][top - 1] == "X" || board.gamegrid[left - 1][top - 1] == "O") {
			return;
			//Spot is already taken
		} else {
			//If the spot is already taken, then don't do anything!
			board.gamegrid[left - 1][top - 1] = "X";
			//Put an X in the array that contains all info about places
			board.repaint();
			//Repaint the board
			isyourturn = false;
			//Set the isyourturn variable to false
			if (checkWin()[0] == "Player" || checkWin()[0] == "Computer") {
				isyourturn = false;
				return;
				//If there's a winner now, return.
			}
			//Check to see if you win!
			opponentTurn();
			//Have the opponent take a turn
			isyourturn = true;
			//Back to your turn!
			if (checkWin()[0] == "Player" || checkWin()[0] == "Computer") {
				isyourturn = false;
				return;
				//If there's a winner now, return.
			}
		}
	}
	
	public void opponentTurn() {
		if (program.turns == 9) {
			isyourturn = false;
			checkWin();
		} else {
			int x = (int) Math.floor((Math.random() * 3));
			int y = (int) Math.floor((Math.random() * 3));
			//Create the random indexes to check.
			
			if (board.gamegrid[x][y] == "X" || board.gamegrid[x][y] == "O") {
				opponentTurn();
				//If the space was taken, redo this method to find new random numbers.
			} else {
				board.gamegrid[x][y] = "O";
				board.repaint();
			}
		}
	}
	
	public String[] checkWin() {

		/*
		 * Have the checkWin() evaluate if someone won, and return an array of Strings
		 * OUTLINE OF win array:
		 * result[0]: The name of the winner.
		 * result[1]: The x location of where a winning box is.
		 * result[2]: The y location of where a winning box is.
		 * result[3]: The x location of where a winning box is.
		 * result[4]: The y location of where a winning box is.
		 * result[5]: The x location of where a winning box is.
		 * result[6]: The y location of where a winning box is.
		 * 
		 * The win array is found in the board class.
		 */
		
		for (int x = 1; x <= 3; x++) {
			//Check for vertical wins
			String first = "";
			String second = "";
			String third = "";
			String firstindex = "";
			String secondindex = "";
			String thirdindex = "";
			
			for (int y = 1; y <= 3; y++) {
				//Remember to decrease x and y by one, due to Java having an array's index start at 0
				
				if (y == 1) {
					first = board.gamegrid[x - 1][0];
					firstindex = String.valueOf(x - 1);
				} else if (y == 2) {
					second = board.gamegrid[x - 1][1];
					secondindex = String.valueOf(x - 1);
				} else {
					third = board.gamegrid[x - 1][2];
					thirdindex = String.valueOf(x - 1);
				}
			}
			
			if (first == second && second == third) {
				//Winner!!
				if (first == "X") {
					board.win[0] = "Player";
					board.win[1] = firstindex;
					board.win[2] = "0";
					board.win[3] = secondindex;
					board.win[4] = "1";
					board.win[5] = thirdindex;
					board.win[6] = "2";
					board.repaint();
					program.incrementLabels("win");
					program.serialize();
					finished = true;
					return board.win;
				} else if (first == "O") {
					board.win[0] = "Computer";
					board.win[0] = "Player";
					board.win[1] = firstindex;
					board.win[2] = "0";
					board.win[3] = secondindex;
					board.win[4] = "1";
					board.win[5] = thirdindex;
					board.win[6] = "2";
					board.repaint();
					program.incrementLabels("loss");
					program.serialize();
					finished = true;
					return board.win;
				}
				//Return the name of the winner
			}
		}
		
		for (int y = 1; y <= 3; y++) {
			//Check for horizontal wins
			String first = "";
			String second = "";
			String third = "";
			String firstindex = "";
			String secondindex = "";
			String thirdindex = "";
			
			for (int x = 1; x <= 3; x++) {
				//Remember to decrease x and y by one, due to Java having an array's index start at 0
				
				if (x == 1) {
					first = board.gamegrid[0][y - 1];
					firstindex = String.valueOf(y - 1);
				} else if (x == 2) {
					second = board.gamegrid[1][y - 1];
					secondindex = String.valueOf(y - 1);
				} else {
					third = board.gamegrid[2][y - 1];
					thirdindex = String.valueOf(y - 1);
				}
			}
			
			if (first == second && second == third) {
				//Winner!!
				if (first == "X") {
					board.win[0] = "Player";
					board.win[1] = "0";
					board.win[2] = firstindex;
					board.win[3] = "1";
					board.win[4] = secondindex;
					board.win[5] = "2";
					board.win[6] = thirdindex;
					board.repaint();
					program.incrementLabels("win");
					program.serialize();
					finished = true;
					return board.win;
				} else if (first == "O") {
					board.win[0] = "Computer";
					board.win[1] = "0";
					board.win[2] = firstindex;
					board.win[3] = "1";
					board.win[4] = secondindex;
					board.win[5] = "2";
					board.win[6] = thirdindex;
					board.repaint();
					program.incrementLabels("loss");
					program.serialize();
					finished = true;
					return board.win;
				}
				//Return the name of the winner
			}
		}
		
		if (board.gamegrid[0][0] == board.gamegrid[1][1] && board.gamegrid[1][1] == board.gamegrid[2][2]) {
			//Diagonal win!
			if (board.gamegrid[1][1] == "X") {
				board.win[0] = "Player";
				board.win[1] = "0";
				board.win[2] = "0";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "2";
				board.repaint();
				program.incrementLabels("win");
				program.serialize();
				finished = true;
				return board.win;
			} else if (board.gamegrid[1][1] == "O") {
				board.win[0] = "Computer";
				board.win[1] = "0";
				board.win[2] = "0";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "2";
				board.repaint();
				program.incrementLabels("loss");
				program.serialize();
				finished = true;
				return board.win;
			}
		}
		
		if (board.gamegrid[0][2] == board.gamegrid[1][1] && board.gamegrid[1][1] == board.gamegrid[2][0]) {
			//Diagonal win!
			if (board.gamegrid[0][2] == "X") {
				board.win[0] = "Player";
				board.win[1] = "0";
				board.win[2] = "2";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "0";
				board.repaint();
				program.incrementLabels("win");
				program.serialize();
				finished = true;
				return board.win;
			} else if (board.gamegrid[0][2] == "O") {
				board.win[0] = "Computer";
				board.win[1] = "0";
				board.win[2] = "2";
				board.win[3] = "1";
				board.win[4] = "1";
				board.win[5] = "2";
				board.win[6] = "0";
				board.repaint();
				program.incrementLabels("loss");
				program.serialize();
				finished = true;
				return board.win;
			}
		}
		
		if (program.turns == 9) {
			//Max number of turns and nobody wins still. It's a tie.
			//Placeholder
			board.win[0] = "Tie";
			return board.win;
		}
		
		board.win[0] = "Nobody";
		return board.win;
	}
	
	public void serialize() {
		try {
			FileOutputStream fout = new FileOutputStream("history.ser");
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			//Create fout and oout for serialization
			
			oout.writeInt(wins);
			oout.writeInt(losses);
			oout.writeInt(ties);
			//Write the variables I need later
			
			oout.close();
			fout.close();
			//I'm done - close the streams
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void mousePressed(MouseEvent e) {
		//Don't care
	}

	public void mouseReleased(MouseEvent e) {
		//Don't care
	}

	public void mouseEntered(MouseEvent e) {
		//Don't care
	}

	public void mouseExited(MouseEvent e) {
		//Don't care
	}
}



board.java:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class board extends JPanel {
	private static final long serialVersionUID = -7675219776569552809L;
	String[][] gamegrid = new String[3][3];
	String[] win = new String[7];
	
	public board() {
		super(null);
	}
	
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		
		g2d.setColor(Color.black);
		
		g2d.setStroke(new BasicStroke(1));
		
		g2d.drawLine(100, 0, 100, 300);
		g2d.drawLine(200, 0, 200, 300);
		g2d.drawLine(0, 100, 300, 100);
		g2d.drawLine(0, 200, 300, 200);
		
		for (int x = 1; x <= 3; x++) {
			for (int y = 1; y <= 3; y++) {
				//Remember, arrays start at an index of 0, so we'll have to decrese all indexes by one.
				if(gamegrid[x - 1][y - 1] == "X") {
					//We have a match! So let's calculate the points.
					
					int x1 = 0;
					int y1 = 0;
					int x2 = 0;
					int y2 = 0;
					
					if (x == 1) {
						x1 = 0;
					} else if (x == 2) {
						x1 = 100;
					} else {
						x1 = 200;
					}
					
					x2 = x1 + 100;
					
					if (y == 1) {
						y1 = 200;
						y2 = 300;
					} else if (y == 2) {
						y1 = 100;
						y2 = 200;
					} else {
						y1 = 0;
						y2 = 100;
					}
					
					g2d.drawLine(x1, y1, x2, y2);
					
					g2d.drawLine(x1, y2, x2, y1);
				}
				if(gamegrid[x - 1][y - 1] == "O") {
					//Opponent has a match! Let's calculate the points.
					
					int x1 = 0;
					int y1 = 0;
					
					if (x == 1) {
						x1 = 0;
					} else if (x == 2) {
						x1 = 100;
					} else {
						x1 = 200;
					}
					
					if (y == 1) {
						y1 = 200;
					} else if (y == 2) {
						y1 = 100;
					} else {
						y1 = 0;
					}
					
					g2d.drawOval(x1, y1, 100, 100);
				}
			}
		}
		
		if (win[6] != null) {
			//Someone won. Let's draw the line through all the boxes that are part of the win.
			
			int x1 = Integer.parseInt(win[1]);
			int y1 = Integer.parseInt(win[2]);
			int x2 = Integer.parseInt(win[5]);
			int y2 = Integer.parseInt(win[6]);
			
			if (x1 == 0) {
				x1 = 50;
			} else if (x1 == 1) {
				x1 = 150;
			} else {
				x1 = 250;
			}
			
			if (x2 == 0) {
				x2 = 50;
			} else if (x2 == 1) {
				x2 = 150;
			} else {
				x2 = 250;
			}
			
			if (y1 == 0) {
				y1 = 250;
			} else if (y1 == 1) {
				y1 = 150;
			} else {
				y1 = 50;
			}
			
			if (y2 == 0) {
				y2 = 250;
			} else if (y2 == 1) {
				y2 = 150;
			} else {
				y2 = 50;
			}
			
			System.out.println("(" + x1 + ", " + y1 + ") - (" + x2 + ", " + y2 +")");
			
			g2d.setStroke(new BasicStroke(3));
			g2d.setColor(Color.red);
			
			g2d.drawLine(x1, y1, x2, y2);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#15 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: new JLabel? Position? *FIXED*

Posted 20 June 2011 - 02:24 PM

Anybody have any ideas? Sorry, I just still can't figure it out.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3