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.

Page 9 of 9 FirstFirst ... 789
Results 201 to 219 of 219

Thread: Chess Program

  1. #201
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    whats this?
    <FEN String1>;<Description1>
    <FEN String2>;<Description2>
    The text with <>s is meta data. The text inside the <>s describes a piece of data.
    The two line I posted were an example of how to use a special character ( ; ) to separate two fields in the record. Here is an example with two lines with real data:
    rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR;Starting position for the King's gambit
    rnbqkbnr/pp1ppppp/2p5/8/8/8/PPPPPPPP/RNBQKBNR;First move of the King's gambit
    If you don't understand my answer, don't ignore it, ask a question.

  2. #202
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    i use a scanner to read next lines in the file and add it to a string but how can i split the subscription from the fen

    --- Update ---

     private void openFile(File file){
           if(file.canRead()){
        	   String filePath = file.getPath();
        	   String fileContents = "";
     
        	   if (filePath.endsWith(".cmd")){
     
        		   try{
        			   Scanner scan = new Scanner(new FileInputStream(file));
        			   while (scan.hasNextLine()){
        				   fileContents+=scan.nextLine();
        			   }
        			   scan.close();
        		   } catch (FileNotFoundException e){}
        		   currentFile = file;
        	   }
        	   else{
        		   JOptionPane.showMessageDialog(null, "Only cmd files");
        	   }
           }
           else{
        	   JOptionPane.showMessageDialog(null, "Error loading file");
           }
        }
     
        private void saveFile(File file, String subscription, String fen){
        	BufferedWriter writer = null;
        	String filePath = file.getPath();
        	if (!filePath.endsWith(".cmd")){
        		filePath+=".cmd";
        		try{
        			writer = new BufferedWriter(new FileWriter(filePath));{
        			writer.write(content);
        			writer.close();
     
        			}
        		}catch(Exception e){}
        	}
        }


    --- Update ---

    The text with <>s is meta data. The text inside the <>s describes a piece of data.
    The two line I posted were an example of how to use a special character ( ; ) to separate two fields in the record. Here is an example with two lines with real data:
    rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR;Starting position for the King's gambit
    rnbqkbnr/pp1ppppp/2p5/8/8/8/PPPPPPPP/RNBQKBNR;First move of the King's gambit
    where do i have to add that code with the separation and do i need a scanner
    is my code above correct?
    System.out.println(" dream in code ");

  3. #203
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    but how can i split the subscription from the fen
    Did you read my last two posts? I described how to use a separator character to separate the parts/fields of the lines/records and use the split() method to get the separate fields.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #204
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    Yes i read your posts! But i dont know how to apply that.

    I have to use the bufferedwriter right? and if i write something to the file i need infopanel.getFen and infopanel.getDescription to get the text.

    But i dont know how to apply the meta data

    --- Update ---

    if (!filePath.endsWith(".cmd")){
        		filePath+=".cmd";
        		try{
        			writer = new BufferedWriter(new FileWriter(filePath));{
        			writer.write(infopanel.getSubscription());
        			writer.write(infopanel.getFen());
        			writer.close();
     
        			}
    System.out.println(" dream in code ");

  5. #205
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    how to apply the meta data
    You don't "apply meta data". It's a way of describing things in general terms when talking about data.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #206
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    yes but you have to code this in java right? so the program knows we want to save it that way

    --- Update ---

    its more like how can i code that? I access the data with infopanel.getFen() and infopanel.getDescription. What code in java tells i want to save it that way field record<> and ; separator and how can i access them if i open a file and put the fen and description back in the jtextfield
    System.out.println(" dream in code ");

  7. #207
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    The <>s were used to delimit some text for describing data. They are not part of the data or intended to be used in the program.

    post#201 had two sample lines that a program could have written to save a FEN and a description.
    When each line was written the two fields were separated by a ;
    When each line is read, it can be separated into the two fields by using the split() method.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #208
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    this piece of code is working. It reads the lines i have saved in a file but this is the result and puts both lines in the subscription bar. Any idea how i can separate them?

    Default Positionrnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

    private void openFile(File file){
           if(file.canRead()){
        	   String filePath = file.getPath();
        	   String fileContents = "";
     
        	   if (filePath.endsWith(".cmd")){
     
        		   try{
        			   Scanner scan = new Scanner(new FileInputStream(file));
        			   while (scan.hasNextLine()){
        				   infopanel.setSubscription(scan.nextLine());
        				   infopanel.setFen(scan.nextLine());
     
        				   chessboard.setSquaresEmpty();
        				   fen.scanFenInput(infopanel.getFen());
     
        				   this.remove(chessboardview);
        					//this.pack();
        				   chessboardview = new ChessBoardView(chessboard);
        				   contentpane.add(chessboardview, BorderLayout.WEST);
        				   this.pack();   
        			   }
        			   scan.close();
        		   } catch (FileNotFoundException e){}
        		   currentFile = file;
        	   }
        	   else{
        		   JOptionPane.showMessageDialog(null, "Only cmd files");
        	   }
     
           }
           else{
        	   JOptionPane.showMessageDialog(null, "Error loading file");
           }
        }

    it has to do with the scan.nextLine() part

    --- Update ---

    maybe as you suggested the .split() method but dont know how to separate them with scan.nextLine();
    System.out.println(" dream in code ");

  9. #209
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    how i can separate them?
    Put a special character between them when they are written.
    Use that character with the String class's split() method to separate them after the line has been read.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #210
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    i understand sorry i am slow witted

    --- Update ---

    one question after i split them how can i add them to the different .setFen() and .setSubscription() methods
    System.out.println(" dream in code ");

  11. #211
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    after i split them how can i add them to the different
    Look at where they are when they have been split. Then you'll know.
    I assume that you know what an array is and how to access its elements.

    BTW the code in post#208 is poorly designed. The openFile() method should read the data from the file, separate it into parts, put the parts into a class object and add it to an arraylist. After all the lines have be read/split/added to the arraylist, the openFile() ,method should return the arraylist to a better place in the program where it could build the GUI using the contents of the arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #212
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    fucked up the program

    the imageicons in chesspiecesview class are null pointers tried to add them but doesnt work. I made a folder in eclipse with the name assets copied the .gif files in this map and in eclipse

    private final static ImageIcon BPW = new ImageIcon
    (ChessPiecesView.class.getResource("assets/bpw.gif"));

    but i get nullpointers and the program doesnt work anymore how is this possible

    --- Update ---

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at controller.Controller.setupProgram(Controller.java :219)
    at controller.Controller.<init>(Controller.java:54)
    at controller.Main.main(Main.java:9)
    Caused by: java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at view.ChessPiecesView.<clinit>(ChessPiecesView.java :17)
    ... 3 more

    --- Update ---

    the code.... how is this possible? its crap

    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.ChessPiece;
    import model.ChessBoard;
     
    public class ChessPiecesView {
     
    	private final static ImageIcon BPW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bpw.gif"));
     
    	private final static ImageIcon BRW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/brw.gif"));
     
    	private final static ImageIcon BNW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bnw.gif"));
     
    	private final static ImageIcon BBW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bbw.gif"));
     
    	private final static ImageIcon BQW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bqw.gif"));
     
    	private final static ImageIcon BKW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bkw.gif"));
     
    	private final static ImageIcon BRB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/brb.gif"));
     
    	private final static ImageIcon BNB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bnb.gif"));
     
    	private final static ImageIcon BBB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bbb.gif"));
     
    	private final static ImageIcon BQB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bqb.gif"));
     
    	private final static ImageIcon BKB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bkb.gif"));
     
    	private final static ImageIcon BPB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/bpb.gif"));
     
    	private final static ImageIcon WPW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wpw.gif"));
     
    	private final static ImageIcon WRW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wrw.gif"));
     
    	private final static ImageIcon WNW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wnw.gif"));
     
    	private final static ImageIcon WBW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wbw.gif"));
     
    	private final static ImageIcon WQW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wqw.gif"));
     
    	private final static ImageIcon WKW = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wkw.gif"));
     
    	private final static ImageIcon WRB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wrb.gif"));
     
    	private final static ImageIcon WNB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wnb.gif"));
     
    	private final static ImageIcon WBB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wbb.gif"));
     
    	private final static ImageIcon WQB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wqb.gif"));
     
    	private final static ImageIcon WKB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wkb.gif"));
     
    	private final static ImageIcon WPB = new ImageIcon 
    	(ChessPiecesView.class.getResource("assets/wpb.gif"));
     
    	private ChessBoard chessboard;
     
    	public ChessPiecesView(ChessBoard chessboard){
     
    		this.chessboard = chessboard;
    		addPieceImage();
    	}
     
    	public void addPieceImage(){
    		 chessboard.getAllPieces().get(0).setImageWhite(BRW);
    		 chessboard.getAllPieces().get(0).setImageBrown(BRB);
    		 chessboard.getAllPieces().get(1).setImageWhite(BNW);
    		 chessboard.getAllPieces().get(1).setImageBrown(BNB);
    		 chessboard.getAllPieces().get(2).setImageWhite(BBW);
    		 chessboard.getAllPieces().get(2).setImageBrown(BBB);
    		 chessboard.getAllPieces().get(3).setImageWhite(BQW);
    		 chessboard.getAllPieces().get(3).setImageBrown(BQB);
    		 chessboard.getAllPieces().get(4).setImageWhite(BKW);
    		 chessboard.getAllPieces().get(4).setImageBrown(BKB);
    		 chessboard.getAllPieces().get(5).setImageWhite(BPW);
    		 chessboard.getAllPieces().get(5).setImageBrown(BPB);
     
    		 chessboard.getAllPieces().get(6).setImageWhite(WRW);
    		 chessboard.getAllPieces().get(6).setImageBrown(WRB);
    		 chessboard.getAllPieces().get(7).setImageWhite(WNW);
    		 chessboard.getAllPieces().get(7).setImageBrown(WNB);
    		 chessboard.getAllPieces().get(8).setImageWhite(WBW);
    		 chessboard.getAllPieces().get(8).setImageBrown(WBB);
    		 chessboard.getAllPieces().get(9).setImageWhite(WQW);
    		 chessboard.getAllPieces().get(9).setImageBrown(WQB);
    		 chessboard.getAllPieces().get(10).setImageWhite(WKW);
    		 chessboard.getAllPieces().get(10).setImageBrown(WKB);
    		 chessboard.getAllPieces().get(11).setImageWhite(WPW);
    		 chessboard.getAllPieces().get(11).setImageBrown(WPB);
     
    	}
     }
    System.out.println(" dream in code ");

  13. #213
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    but i get nullpointers and the program doesnt work anymore how is this possible
    The folder with the resources must be on the classpath. Is assets folder in a folder at the end of the classpath?

    If the path to an image file is: C:\AFolder\assets\afile.gif
    then the classpath should have: C:\AFolder
    If you don't understand my answer, don't ignore it, ask a question.

  14. #214
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    i have a map in the package assets. this map contains the chesspieces .gif files. Next i made a new package in the src folder and imported the assets map in the view package
    System.out.println(" dream in code ");

  15. #215
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    i have a map in the package assets.
    What is a "map"? A class? A file? A folder? ???
    Your description of your actions sounds like you are doing something with your IDE.
    I don't understand how to use your IDE and can not help you configure it for your program.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #216
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    hello any idea how i can set a accelerator for a menu item with shift, mac, letter

    item.setAccelerator(KeyStroke.getKeyStroke(
    java.awt.event.KeyEvent.VK_S,
    java.awt.Event.META_MASK));

    this but with the shift key before the meta_mask mac
    System.out.println(" dream in code ");

  17. #217
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Sorry, I have no ideas how to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #218
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    and do you know how i can make an desktop executable? i have my code in eclipse with a main method in the controller class but i want something with a pictogram so i starts like a normal program should do
    System.out.println(" dream in code ");

  19. #219
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Create an executable jar file. On Windows with a JRE installed, double clicking in an executable jar file will start the program executing.
    If you don't understand my answer, don't ignore it, ask a question.

Page 9 of 9 FirstFirst ... 789

Similar Threads

  1. Simple chess program (no AI) - Need help with structuring my code
    By MineeMo in forum Object Oriented Programming
    Replies: 6
    Last Post: June 18th, 2012, 10:12 AM
  2. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 08:42 PM
  3. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 02:40 AM
  4. Chess Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 12:07 PM
  5. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM