Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Passing objects as a constructor parameter

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Passing objects as a constructor parameter

    Hello, my task is to create a new Sudoku object and then create a new SudokuWindow object, passing the Sudoku object as a constructor parameter.

    I am given

    SudokuWindow
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class SudokuWindow extends JFrame
    {
        private static final Color lockedColor = new Color(233, 233, 233);
        private static final Color bgColor = Color.white;
        private static final Color hoverColor = new Color(211, 255, 211);
        private static final Border regionBorder = BorderFactory.createLineBorder(new Color(102, 102, 153));
        private static final Border cellBorder = BorderFactory.createLineBorder(new Color(221, 221, 238));
        private static final Border hoverBorder = BorderFactory.createLineBorder(Color.black);
        private static final Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
        private Sudoku sudoku;
        private State state;
        private int lockCount;
        private BoardPanel boardPanel;
        private StatusPanel statusPanel;
     
        public SudokuWindow(Sudoku sudoku)
        {
            this.sudoku = sudoku;
            state = State.configuring;
            lockCount = 0;
            boardPanel = new BoardPanel();
            statusPanel = new StatusPanel();
            JPanel boardContainer = new JPanel();
            boardContainer.setBackground(bgColor);
            boardContainer.add(boardPanel);
            add(boardContainer, BorderLayout.CENTER);
            add(statusPanel, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
     
        class BoardPanel extends JPanel
        {
            public BoardPanel()
            {
                int size = sudoku.getSize();
                setLayout(new GridLayout(size, size));
                for (int row = 0; row < size; row++)
                    for (int col = 0; col < size; col++)
                        add(new RegionPanel(row, col));
            }
        }
     
        class RegionPanel extends JPanel
        {
            private JTextField[][] fields;
            private boolean[][] locked;
     
            public RegionPanel(int regRow, int regCol)
            {
                final int size = sudoku.getSize();
                final int bigSize = size*size;
                setLayout(new GridLayout(size, size));
                setBorder(regionBorder);
                fields = new JTextField[size][size];
                locked = new boolean[size][size];
                for (int row = 0; row < size; row++)
                    for (int col = 0; col < size; col++)
                    {
                        final int frow = row, fcol = col;
                        final JTextField field = new JTextField();
                        field.addMouseListener(new MouseAdapter()
                                {
                                    public void mouseEntered(MouseEvent event)
                                    {
                                        if (!locked[frow][fcol])
                                        {
                                            field.requestFocus();
                                            field.setBackground(hoverColor);
                                            field.setBorder(hoverBorder);
                                        }
                                    }
     
                                    public void mouseExited(MouseEvent event)
                                    {
                                        if (!locked[frow][fcol])
                                        {
                                            field.setBackground(bgColor);
                                            field.setBorder(cellBorder);
                                        }
                                    }
                                }
                                );
                        field.setCursor(defaultCursor);
                        field.setBorder(cellBorder);
                        field.setPreferredSize(new Dimension(35, 35));
                        field.setHorizontalAlignment(JTextField.CENTER);
                        field.setFont(new Font("Serif", Font.BOLD, 16));
                        final int region = regRow*size + regCol + 1;
                        final int cell = row*size + col + 1;
                        field.addKeyListener(new KeyAdapter()
                                {
                                    public void keyReleased(KeyEvent event)
                                    {
                                        if (locked[frow][fcol])
                                            return;
                                        int number;
                                        try
                                        {
                                            number = Integer.parseInt(field.getText());
                                            if (number > bigSize) number /= 10;
                                            if (number < 1) number = 1;
                                        }
                                        catch (Exception e)
                                        {
                                            number = Cell.BLANK;
                                        }
                                        boolean lock = state == State.configuring && number != Cell.BLANK;
                                        sudoku.enterNumber(region, cell, number, lock);
                                        number = sudoku.getNumber(region, cell);
                                        field.setText(number == Cell.BLANK ? "" : (""+number));
                                        if (lock)
                                        {
                                            field.setEditable(false);
                                            field.setBackground(lockedColor);
                                            field.setBorder(cellBorder);
                                            lockCount++;
                                            if (lockCount >= size*size*size*size/3)
                                            {
                                                state = State.playing;
                                                statusPanel.update();
                                            }
                                        }
                                        if (sudoku.isSolved())
                                        {
                                            state = State.solved;
                                            statusPanel.update();
                                        }
                                        locked[frow][fcol] |= lock;
                                    }
                                }
                                );
                        add(field);
                        fields[row][col] = field;
                    }
            }
        }
     
        class StatusPanel extends JPanel
        {
            private JLabel label;
     
            public StatusPanel()
            {
                setBackground(bgColor);
                add(label = new JLabel());
                label.setFont(new Font("Serif", Font.BOLD, 18));
                update();
            }
     
            public void update()
            {
                label.setText(message());
            }
     
            private String message()
            {
                switch (state)
                {
                    case configuring: return "Please initialise the puzzle...";
                    case playing: return "Puzzle ready to solve";
                    case solved: return "It's SOLVED!!!";
                    default: return "Invalid state?";
                }
            }
        }
     
        enum State { configuring, playing, solved }
    }

    Sudoku
    public class Sudoku
    {
        private Group reg1, reg2, reg3, reg4;
        private Group row1, row2, row3, row4;
        private Group col1, col2, col3, col4;
     
        public Sudoku()
        {
            Cell c1  = new Cell(), c2  = new Cell(), c3  = new Cell(), c4  = new Cell();
            Cell c5  = new Cell(), c6  = new Cell(), c7  = new Cell(), c8  = new Cell();
            Cell c9  = new Cell(), c10 = new Cell(), c11 = new Cell(), c12 = new Cell();
            Cell c13 = new Cell(), c14 = new Cell(), c15 = new Cell(), c16 = new Cell();
     
            reg1 = new Group(c1, c2, c5, c6);
            reg2 = new Group(c3, c4, c7, c8);
            reg3 = new Group(c9, c10, c13, c14);
            reg4 = new Group(c11, c12, c15, c16);
     
            row1 = new Group(c1, c2, c3, c4);
            row2 = new Group(c5, c6, c7, c8);
            row3 = new Group(c9, c10, c11, c12);
            row4 = new Group(c13, c14, c15, c16);
     
            col1 = new Group(c1, c5, c9, c13);
            col2 = new Group(c2, c6, c10, c14);
            col3 = new Group(c3, c7, c11, c15);
            col4 = new Group(c4, c8, c12, c16);
        }
     
        public void enterNumber(int region, int cell, int number, boolean lock)
        {
            getRegion(region).enterNumber(cell, number, lock);
        }
     
        public int getNumber(int region, int cell)
        {
            return getRegion(region).getCell(cell).getNumber();
        }
     
        private Group getRegion(int regionNumber)
        {
            switch (regionNumber)
            {
                case 1: return reg1;
                case 2: return reg2;
                case 3: return reg3;
                case 4: return reg4;
            }
            return null;
        }
     
        public boolean isSolved()
        {
            return reg1.isSolved() && reg2.isSolved() && reg3.isSolved() && reg4.isSolved()
                && row1.isSolved() && row2.isSolved() && row3.isSolved() && row4.isSolved()
                && col1.isSolved() && col2.isSolved() && col3.isSolved() && col4.isSolved();
        }
     
        public void print()
        {
            System.out.println();
            row1.print();
            row2.print();
            row3.print();
            row4.print();
            System.out.println();
        }
     
        public int getSize()
        {
            return 2;
        }
    }

    My attempt to link the Window to the main.

    import java.util.*;
     
    public class GraphicalMain
    {
        private static Scanner keyboard = new Scanner(System.in);
        private static SudokuWindow sudokuWindow;
        private static Sudoku sudoku;
     
        public static void main(String[] args)
        {
            sudoku = new Sudoku();
            sudoku = new SudokuWindow(Sudoku, sudoku);
        }
    }

    Please any advice or help would be greatly appreicated.
    I'm getting the error at
    sudoku = new SudokuWindow(Sudoku, sudoku);
    saying [ or ( expected but I think that isn't the problem.

    Thanks
    Last edited by derky; October 27th, 2009 at 05:33 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Passing objects as a constructor parameter

    This is just like passing a parameter to any type of method.

            sudoku = new Sudoku();
            sudokuWindow = new SudokuWindow(sudoku);

    Something like that should work better looking at your code above.

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    derky (October 27th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Passing objects as a constructor parameter

    thanks alot Json!
    I spent ages playing around with inside the brackets +_+
    Cheers

  5. The Following User Says Thank You to derky For This Useful Post:

    odnohnikizin (September 13th, 2010)

Similar Threads

  1. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 10:39 PM
  2. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 07:29 PM
  3. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 02:38 PM
  4. Java program to encrypt an image using crypto package
    By vikas in forum Java Networking
    Replies: 1
    Last Post: July 7th, 2009, 12:00 PM
  5. Replies: 6
    Last Post: May 15th, 2009, 06:06 PM