Results 1 to 5 of 5
  1. #1
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default 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):
    Java 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:
    Java 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:
    Java 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    TLDR

    Did you repaint?

  3. #3
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    yeah, i got repaint everywhere. still not workin

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If you are talking about changing the JLabel MyNum when you call the setNewName method then please indicate where in the below code do you actually change the label?
    Java Code:
    public void setNewName(String NewName) {
        this.MyName = NewName;
        System.out.print("coming in:" + this.MyName + "\n");
        this.repaint();
    }

  5. #5
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

Similar Threads

  1. Passing dynamic variables between classes
    By Brekk in forum New To Java
    Replies: 1
    Last Post: 03-22-2010, 06:07 AM
  2. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  3. Replies: 7
    Last Post: 03-16-2009, 07:19 AM
  4. passing variables to another frame
    By Grom in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 05:55 PM
  5. Replies: 0
    Last Post: 11-10-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •