please help with passing variables between class's
Hello everyone. Im new to java and this forum and I need some help passing variables between class's. I am writing a soduku game. I have the main class create an object from the class SudokuBoardDisplay. The class board creates a new object, from the class MyItems to create each cell of the soduku board. I'm having problems passing parameters between classes. heres what I have:
Board (Main Class):
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
public class Board extends JFrame implements ActionListener{
private SudokuBoardDisplay _sudokuBoard = new SudokuBoardDisplay(null);
public Board() {
// 1... Create/initialize components
JButton moveBtn = new JButton("Check For Solution");
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(moveBtn);
moveBtn.addActionListener(new MoveListener());
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(controlPanel, BorderLayout.NORTH);
content.add(_sudokuBoard, BorderLayout.CENTER);
setContentPane(content);
setTitle("Sudoku");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public void actionPerformed(ActionEvent e) {
}
class MoveListener implements ActionListener {
private String _board;
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(null, "Checking for solution" + _board);
}
}
public static void main(String[] args) {
new Board().setVisible(true);
}
}
SudokuBoardDisplay Class:
Code:
class SudokuBoardDisplay extends JComponent {
private static final int CELL_PIXELS = 49; // Size of each cell.
private static final int PUZZLE_SIZE = 9; // Number of rows/cols
private static final int BOARD_PIXELS = CELL_PIXELS * PUZZLE_SIZE;
SudokuBoardDisplay(Object object) {
setPreferredSize(new Dimension(BOARD_PIXELS + 2, BOARD_PIXELS + 2));
setBackground(Color.WHITE);
drawGridLines();
}
private void drawGridLines()
{
for (int i = 1; i <= PUZZLE_SIZE; i++)
{
for (int j = 1; j <= PUZZLE_SIZE-1;j++)
{
int acrossOrDown =( i * CELL_PIXELS)-50;
int Down = (j * CELL_PIXELS)-50;
MyItems SodukuCell = new MyItems();
if ((j==3) || (j==6))
{
SodukuCell.setMyPos(acrossOrDown,Down);
SodukuCell.setNewName(i + "," + j );
SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 5, 1, Color.BLACK));
this.add(SodukuCell);
repaint();
}
else if ((i==3) || (i==6))
{
SodukuCell.setMyPos(acrossOrDown,Down);
SodukuCell.setNewName(i + "," + j );
SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 1, 5, Color.BLACK));
this.add(SodukuCell);
repaint();
}
else
{
SodukuCell.setMyPos(acrossOrDown,Down);
SodukuCell.setNewName(i + "," + j);
SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black));
this.add(SodukuCell);
repaint();
}
}
}
}
MyItems Class:
Code:
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
//=======================================================================
public class MyItems extends JPanel implements MouseListener
{
public int MyX;
public int MyY;
public String MyName;
public Border myBorder=BorderFactory.createMatteBorder(0, 0, 1, 1, Color.BLACK);
public Color myColor = Color.WHITE;
public Color myFontColor = Color.BLACK;
public String lblNum = "test";
public JPanel InputPanel;
public JLabel MyNum = new JLabel("name=" + MyName);
public MyItems()
{
this.setBorder(myBorder);
this.setSize(50, 50);
this.setLocation(MyX, MyY);
this.setBorder(myBorder);
this.addMouseListener(this);
this.add(MyNum);
System.out.print("my new name is:" + MyName + "\n");
repaint();
}
public void setNewName(String NewName)
{
this.MyName = NewName;
System.out.print("coming in:" + this.MyName + "\n");
this.repaint();
}
public void ChangeBorder(Border NewBorder)
{
myBorder = NewBorder;
this.setBorder(NewBorder);
if (this.MyName=="3,3"){
this.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.BLACK));
}
this.repaint();
}
public void setMyPos(int i, int j)
{
MyX=i;
MyY=j;
this.setLocation(MyX, MyY);
this.repaint();
}
public void mouseClicked(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
String s = (String) JOptionPane
.showInputDialog(this, "Please enter a number:\n" + this.getParent().getName(), "Please enter a number!",
JOptionPane.PLAIN_MESSAGE, null, null, " ");
}
public void mousePressed(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
}
what i want to do is, when MyItems class is initialized, set the label to a specific value. You can see that, once i create the object from the class, I call a method in that class to change a particular value (in the MyItems.SetNewName method. the value gets changed there but the label doesnt change. anyone have any ideas?
thanks
jason