Go Back   Java Programming Forums > Java Standard Edition Programming Help > Object Oriented Programming


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 27-10-2009, 05:36 AM
Junior Member
 

Join Date: Sep 2009
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
derky is on a distinguished road
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
Java Code
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
Java Code
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.

Java Code
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
Java Code
sudoku = new SudokuWindow(Sudoku, sudoku);
saying [ or ( expected but I think that isn't the problem.

Thanks




Last edited by derky; 27-10-2009 at 08:33 AM.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 27-10-2009, 07:33 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,188
Thanks: 57
Thanked 137 Times in 133 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: Passing objects as a constructor parameter

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

Java Code
        sudoku = new Sudoku();
        sudokuWindow = new SudokuWindow(sudoku);
Something like that should work better looking at your code above.

// Json
Reply With Quote
The Following User Says Thank You to Json For This Useful Post:
derky (27-10-2009)
  #3 (permalink)  
Old 27-10-2009, 08:31 AM
Junior Member
 

Join Date: Sep 2009
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
derky is on a distinguished road
Default Re: Passing objects as a constructor parameter

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

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays Of Objects? MysticDeath Object Oriented Programming 2 21-10-2009 02:39 AM
Throwing arrays as objects Audemars Collections and Generics 1 23-09-2009 11:29 PM
[SOLVED] Problem with array objects sadi_shihab Collections and Generics 4 09-07-2009 06:38 PM
transferring and recieving encryted objects over a network vikas Java Networking 1 07-07-2009 04:00 PM
Recycling objects from multiple classes that all descend from the same class PhilTroy Object Oriented Programming 6 15-05-2009 10:06 PM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 11:48 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.