Yes! in methods something like this:
String input = fenbar.getText();
fen.fenInput(input);
fen.scanFen();
chessboardview.addSquaresAndPiecesToPanel();
Screen Shot 2013-08-15 at 10.40.19 PM.jpg
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.
Yes! in methods something like this:
String input = fenbar.getText();
fen.fenInput(input);
fen.scanFen();
chessboardview.addSquaresAndPiecesToPanel();
Screen Shot 2013-08-15 at 10.40.19 PM.jpg
System.out.println(" dream in code ");
What part is the problem?
The listener gets the FEN string from the textfield (Note: The listener is in the same class as the textfield and button)
The listener calls the parse method, passing it the string. If the parse method is in another class, the listener method will need a reference to that class's object.
As the FEN string is parsed, the pieces are put into their locations on the board.
If you don't understand my answer, don't ignore it, ask a question.
in a other thread i posted the classes but its not working that way
--- Update ---
if i build this in the class with the textfield infopanel its not working
actionPerformed(ActionEvent e){ String input = fenbar.getText(); fen.fenInput(input); fen.scanFen(); chessboardview.addSquaresAndPiecesToPanel(); }
System.out.println(" dream in code ");
Please explain how you want it to work.its not working that way
It may be possible to do it that way
or it may not be possible to do it that way.
If there are error messages, you need to copy the full text and paste it here.with the textfield infopanel its not working
If you don't understand my answer, don't ignore it, ask a question.
fenbar = new JTextField(new Fen().getFen()); gbc.gridy = 1; fenbar .setPreferredSize(new Dimension(500, 25)); fenbar .setBorder(BorderFactory.createLineBorder(Color.BLACK)); fenbar .addActionListener(new fenActionListener()); add(fenbar, gbc);
class fenActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { String input = fenbar.getText(); fen.fenInput(input); fen.scanParser(); chessboardview.addSquaresAndPiecesToContentPane(); } }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at view.InfoPanel$fenActionListener.actionPerformed(I nfoPanel.java:65)
at javax.swing.JTextField.fireActionPerformed(JTextFi eld.java:508)
at javax.swing.JTextField.postActionEvent(JTextField. java:721)
at javax.swing.JTextField$NotifyAction.actionPerforme d(JTextField.java:836)
at javax.swing.SwingUtilities.notifyAction(SwingUtili ties.java:1664)
at javax.swing.JComponent.processKeyBinding(JComponen t.java:2879)
at javax.swing.JComponent.processKeyBindings(JCompone nt.java:2926)
at javax.swing.JComponent.processKeyEvent(JComponent. java:2842)
at java.awt.Component.processEvent(Component.java:628 2)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.KeyboardFocusManager.redispatchEvent(Keyb oardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.jav a:4731)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719 )
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
at java.awt.EventQueue.access$200(EventQueue.java:103 )
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
--- Update ---
but as i said in my controller class which implements actionlistener the chessboardview is initiated..a new chessboardview class is made!
but if i give the controller as a parameter in the infopanel class it is not working
System.out.println(" dream in code ");
At line 65 there is a variable with a null value. Find it and make sure it gets a valid value.Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at view.InfoPanel$fenActionListener.actionPerformed(I nfoPanel.java:65)
How does the method in the controller class get the data it needs?controller class which implements actionlistener
A new one every time? Why not just have one that is reused?new chessboardview class is made!
If you don't understand my answer, don't ignore it, ask a question.
if i make a new chessboardview class in the infopanel the line 65 problem is solved but nothing happens when i press enter in the program to set the pieces
Controller class:
package controller; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*; import javax.swing.*; import model.Fen; import view.ControlPanel; import view.InfoPanel; import view.ChessBoardView; public class Controller extends JFrame implements ActionListener { private InfoPanel infopanel; private ControlPanel controlpanel; private Fen fen; private ChessBoardView chessboardview; public Controller() { super("Chess Manager"); makeFrame(); makeMenuBar(); this.pack(); } private void makeFrame(){ setSize(1050, 1050); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable( false ); Container contentpane = super.getContentPane(); contentpane.setLayout(new BorderLayout()); contentpane.setBackground(new Color(204,229,255)); controlpanel = new ControlPanel(this); contentpane.add(controlpanel, BorderLayout.EAST); infopanel = new InfoPanel(this); contentpane.add(infopanel, BorderLayout.SOUTH); chessboardview = new ChessBoardView(); contentpane.add(chessboardview, BorderLayout.WEST); } private void makeMenuBar(){ JMenuBar menubar = new JMenuBar(); super.setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenu editMenu = new JMenu("Edit"); menubar.add(editMenu); JMenu optionMenu = new JMenu("Options"); menubar.add(optionMenu); JMenu extraMenu = new JMenu("Extra"); menubar.add(extraMenu); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
System.out.println(" dream in code ");
Try adding some println statements to print out messages as the code executes when Enter is pressed. Print out the values of the variables in all the methods that are called. The printout will show you what is happening when the code executes. If nothing is printed, then you are correct when you say:nothing happens when i press enter in the program to set the pieces
"nothing happens". Add the println statements, execute the program and press Enter to see if anything happens or not.
If you don't understand my answer, don't ignore it, ask a question.
the print statement help me to find the problem, the fen notation works. the new fen place the pieces the only thing that doest work is the chessboardview is doesn't update the images.
System.out.println(" dream in code ");
Is there more than one instance of the chessboardview class being created? If there are, which one is being shown in the GUI and which one are you updating? You need to update the one that is shown in the GUI.the chessboardview is doesn't update the images.
If you don't understand my answer, don't ignore it, ask a question.
more than one chessboards are made! not more chessboardviews.
--- Update ---
chessboard in the model package
--- Update ---
i have no idea what are object or class methods
System.out.println(" dream in code ");
If there is only one instance of the chessboardview class, where is the GUI updated to make the changes you want to see but that are not being shown?
If you don't understand my answer, don't ignore it, ask a question.
in the chessboardview class! but in that class i make a chessboard object let me post chessboardview
package view; import java.awt.*; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.JLabel; import controller.Controller; import model.ChessBoard; import model.Square; import model.Fen; import model.ChessPiece; public class ChessBoardView extends JPanel { //intialize variables private final static ImageIcon DARK_BROWN = new ImageIcon (ChessBoardView.class.getResource("assets/sqb.gif")); private final static ImageIcon LIGHT_BROWN = new ImageIcon (ChessBoardView.class.getResource("assets/sqw.gif")); //intialize components //intialize arrays to hold panels and images of the board; private static ChessBoard chessboard; private static ChessPiecesView chesspiecesview; private ChessPiece chesspiece; private static Fen fen; private static JPanel panel; public ChessBoardView (Controller controller){ chessboard = new ChessBoard(); chesspiecesview = new ChessPiecesView(); fen = new Fen(); fen.scanParser(); addImageToSquare(); createGUI(); } public void createGUI() { Dimension boardSize = new Dimension(250, 250); setLayout( new GridLayout(8, 8) ); setPreferredSize( boardSize ); setMinimumSize( boardSize ); setMaximumSize( boardSize ); setBounds(0, 0, boardSize.width, boardSize.height); setBorder(BorderFactory.createLineBorder(Color.BLACK)); addSquaresAndPiecesToContentPane(); } private void addImageToSquare() { for(int i = 0; i < 64; i++){ if (chessboard.getSquare(i).getColor().equals("WHITE")){ chessboard.getSquare(i).setImage(LIGHT_BROWN); } else if (chessboard.getSquare(i).getColor().equals("BROWN")){ chessboard.getSquare(i).setImage(DARK_BROWN); } } } public void addSquaresAndPiecesToContentPane(){ for (int i = 0; i < 64; i++){ if(chessboard.getAllSquares().get(i).isEmpty()){ JLabel square = new JLabel(chessboard.getAllSquares().get(i).getImage()); square.setBounds(0,0,0,0); super.add(square); } else{ JLabel piece = new JLabel(chessboard.getAllSquares().get(i).getPieceImage()); super.add(piece); } } } }
System.out.println(" dream in code ");
Did the println() statements show that the statements to update the GUI were executed?
If you don't understand my answer, don't ignore it, ask a question.
it all has to do with the call to methods and static type. Only getting null pointers
--- Update ---
i fucked up! removed all the statics but the problem is most of my classes use this chessboard class
package model; import java.util.ArrayList; import javax.swing.ImageIcon; public class ChessBoard { private ArrayList<ChessPiece> pieces; private ArrayList<Square> squares; private final int NUM_OF_COLS = 8; private final String[] LOCATIONS = { "A8","B8","C8","D8","E8","F8","G8","H8", "A7","B7","C7","D7","E7","F7","G7","H7", "A6","B6","C6","D6","E6","F6","G6","H6", "A5","B5","C5","D5","E5","F5","G5","H5", "A4","B4","C4","D4","E4","F4","G4","H4", "A3","B3","C3","D3","E3","F3","G3","H3", "A2","B2","C2","D2","E2","F2","G2","H2", "A1","B1","C1","D1","E1","F1","G1","H1", }; public ChessBoard(){ squares = new ArrayList<Square>(); pieces = new ArrayList<ChessPiece>(); addLocations(); //printCoorToSqi(); setSquareColors(); makePieces(); } public void coorToSqi(ChessPiece piece, int col, int row) { int index = row * NUM_OF_COLS + col; ChessBoard.getAllSquares().get(index).setChessPiece(piece); System.out.println(ChessBoard.getAllSquares().get(index).printChessPiece()); System.out.println(ChessBoard.getAllSquares().get(index).toString()); //return getSquare(index); } public void printCoorToSqi(){ for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { int index = row * NUM_OF_COLS + col; System.out.println(squares.get(index)+"@"+"r="+row+",c="+col); } } } private void addLocations(){ for(int i = 0; i < LOCATIONS.length; i++){ squares.add(new Square(LOCATIONS[i])); } } private void setSquareColors(){ for(Square square: squares){ if(square.toString().matches("A1|C1|E1|G1|B2|D2|F2|" + "H2|A3|C3|E3|G3|B4|D4|F4|H4|A5|C5|E5|G5|" + "B6|D6|F6|H6|A7|C7|E7|G7|B8|D8|F8|H8")){ square.setColor("BROWN"); } else if(square.toString().matches("A2|C2|E2|G2|B1|D1|F1|" + "H1|A4|C4|E4|G4|B3|D3|F3|H3|A6|C6|E6|G6|" + "B5|D5|F5|H5|A8|C8|E8|G8|B7|D7|F7|H7")){ square.setColor("WHITE"); } } } public Square getSquare(int i){ return squares.get(i); } public ChessPiece getPiece(int i){ return pieces.get(i); } public String getSquareColor(int i){ return squares.get(i).getColor(); } public ArrayList<Square> getAllSquares(){ return squares; } public ArrayList<ChessPiece> getAllPieces(){ return pieces; } public void setSquareImage(int i, ImageIcon image){ getSquare(i).setImage(image); } public void setSquaresEmpty(){ for(int i = 0; i < squares.size(); i++) squares.get(i).setEmpty(); } public String toString(){ return squares.toString(); } public void makePieces(){ pieces.add(new ChessPiece("r", "rook", "BLACK")); pieces.add(new ChessPiece("n", "knight", "BLACK")); pieces.add(new ChessPiece("b", "bishop", "BLACK")); pieces.add(new ChessPiece("q", "queen", "BLACK")); pieces.add(new ChessPiece("k", "king", "BLACK")); pieces.add(new ChessPiece("p", "pawn", "BLACK")); pieces.add(new ChessPiece("R", "rook", "WHITE")); pieces.add(new ChessPiece("N", "knight", "WHITE")); pieces.add(new ChessPiece("B", "bishop", "WHITE")); pieces.add(new ChessPiece("Q", "queen", "WHITE")); pieces.add(new ChessPiece("K", "king", "WHITE")); pieces.add(new ChessPiece("P", "pawn", "WHITE")); } }
System.out.println(" dream in code ");
I thought that the Square class extended the JLabel class (post#6) and that 64 Square objects were added to the ChessBoardView's grid layout. That should only happen one time. Once all the Square objects are added to the ChessBoardView class, that is all that is added to it. The posted code shows that JLabels are added to the ChessBoardView class, not the Square class objects.
After the Square objects are added, then changes to the board display would be made by calling methods in the Square class telling it to change what it shows in the GUI.
If you don't understand my answer, don't ignore it, ask a question.
the problem for now is is that the chessboardview class methods uses from the chessboard class. it cant access it static somehow. the chessboard only needs to be made in the controller class. here take a look at the chessboardview:
package view; import java.awt.*; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.JLabel; import controller.Controller; import model.ChessBoard; import model.Square; import model.Fen; import model.ChessPiece; public class ChessBoardView extends JPanel { //intialize variables private final static ImageIcon DARK_BROWN = new ImageIcon (ChessBoardView.class.getResource("assets/sqb.gif")); private final static ImageIcon LIGHT_BROWN = new ImageIcon (ChessBoardView.class.getResource("assets/sqw.gif")); //intialize components //intialize arrays to hold panels and images of the board; private static ChessBoard chessboard; private ChessPiecesView chesspiecesview; private ChessPiece chesspiece; private Fen fen; private JPanel panel; public ChessBoardView (){ //chesspiecesview = new ChessPiecesView(); //fen = new Fen(); //fen.scanParser(); addImageToSquare(); createGUI(); } public void createGUI() { Dimension boardSize = new Dimension(250, 250); setLayout( new GridLayout(8, 8) ); setPreferredSize( boardSize ); setMinimumSize( boardSize ); setMaximumSize( boardSize ); setBounds(0, 0, boardSize.width, boardSize.height); setBorder(BorderFactory.createLineBorder(Color.BLACK)); addSquaresAndPiecesToContentPane(); } private void addImageToSquare() { for(int i = 0; i < 64; i++){ if (chessboard.getSquare(i).getColor().equals("WHITE")){ chessboard.getSquare(i).setImage(LIGHT_BROWN); } else if (chessboard.getSquare(i).getColor().equals("BROWN")){ chessboard.getSquare(i).setImage(DARK_BROWN); } } } public void addSquaresAndPiecesToContentPane(){ for (int i = 0; i < 64; i++){ if(chessboard.getAllSquares().get(i).isEmpty()){ JLabel square = new JLabel(chessboard.getAllSquares().get(i).getImage()); square.setBounds(0,0,0,0); super.add(square); } else{ JLabel piece = new JLabel(chessboard.getAllSquares().get(i).getPieceImage()); super.add(piece); } } } }
System.out.println(" dream in code ");
Where are the Square objects added to the grid? I only see JLabel objects being added to the grid layout.
The 64 Square objects should be added only one time and then used to display different items as the contents of the square changes.
If you don't understand my answer, don't ignore it, ask a question.
only getting nullpointers when i run the program
System.out.println(" dream in code ");
Make sure the variables have non-null values.
If you don't understand my answer, don't ignore it, ask a question.
spend the night debugging and now i am back to the point i was. Done a little bit more en with "System.out.println" as you suggested i see that the Fen notation works. It empty all squares en places new ones on the right place.
The only problem now is (also first instance) the GUI doesnt update when i use the method to search the squares for pieces or emptiness. and places images. I checked but i only make one instance(object) of the chessboardview, in the controller class. also one chessboard and one fen.
--- Update ---
ive tried to add the update method "statements" directly in the actionperformed(ActionEvent e) from the controller class even that doesnt work
--- Update ---
i think i know the problem as you said earlier you cannot put a JLabel over an JLabel. first i need to clean the JPanel.. Is there a way to clear a Jpanel of JLabels?
System.out.println(" dream in code ");
Why change the Square objects after they have been put into the gridlayout?
Reuse them. The JLabel class (That's what Square extends) has methods for changing its contents.
Also the Square class should have methods for changing its contents.
If you don't understand my answer, don't ignore it, ask a question.
i have butt the square class also contains a field en method which sets a piece on that square and empty it to
System.out.println(" dream in code ");
The methods should use the Square objects that have already been added to the ChessBoardView grid. There should not be methods that create new JLabels and add them.
If you don't understand my answer, don't ignore it, ask a question.
Ive got it working. But still my big problem is, i have 2 buttons with actionlisteners each with a reference to the controller class. In the controller class which implements ActionListener there is one actionperformed(ActionEvent e) with some statements for instance if you press enter in the Fen bar the fen notation is checked and the pieces are placed on the board. How can i add statements for the 2 buttons while i have one actionperformed(ActionEvent e) with all ready a task.
Think you explained this earlier but i still don't understand how i can perform different tasks for the different buttons in the controller class
System.out.println(" dream in code ");