9 Replies - 801 Views - Last Post: 19 August 2007 - 01:17 PM

#1 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Intro

Post icon  Posted 13 August 2007 - 04:11 AM

Hi all

I am pleased to join the group and learn about Java here.Thanks for your site and resources. I started learning Java to help me program chess GUI and chess related programs.It is hard but enjoyable using and learning Java.
My current chess Java project is building a program to strip the comments from chessbase pgn (chess notation text based files) files.
I have been looking at Java I/O file handling and regular expressions to help with this. When I have written an appropriate class that will strip the comments out I want to add a function to the menu of a simple java text editor and that will complete the program!
I wonder if anyone can help me here please with this project?!

For starters here is my code for removing the comments using regex, but I need to develop this so I can apply it to a complete pgn file and then add it to the text editor
class CbPgnDecommenter{
 public static void main(String[] args) {
	  
String str = "1. e4 {[%emt 0:00:01]} c6 {[%emt 0:00:10]} 2. d4 {[%emt 0:00:03]} d5 {[%emt 0:00:01]} " + "3. e5 {[%emt 0:00:01]} Bf5 {[%emt 0:00:01]} 4. h4 {[%emt 0:00:02]} e6 {[%emt 0:00:02]}";
str = str.replaceAll("\\s*;.*?\\n|\\{.*?\\}", " ");

System.out.println(str);
}//end main
}



and here is my attempt to add this to NutPad a Java text editor code from the web:
//CleanCBpgnPad by RPD based on NutPad text editor
// editor/NutPad.java -- A very simple text editor -- Fred Swartz - 2004-08
// Illustrates use of AbstractActions for menus.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class CleanCBpgnPad extends JFrame {
	//-- Components 
	private JTextArea mEditArea;
	private JFileChooser mFileChooser = new JFileChooser(".");
	
	//-- Actions
	private Action mOpenAction;
	private Action mSaveAction;
	private Action mExitAction;
	private Action mCleanAction;
	//===================================================================== main
	public static void main(String[] args) {
		new CleanCBpgnPad().setVisible(true);
	}//end main
	
	//============================================================== constructor
	public CleanCBpgnPad() {
		createActions();
		this.setContentPane(new contentPanel());
		this.setJMenuBar(createMenuBar());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("CleanCBpgnPad");
		this.pack();
	}//end constructor

	///////////////////////////////////////////////////////// class contentPanel
	private class contentPanel extends JPanel {		
		//========================================================== constructor
		contentPanel() {
			//-- Create components.
			mEditArea = new JTextArea(15, 80);
				mEditArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
				mEditArea.setFont(new Font("monospaced", Font.PLAIN, 14));
			JScrollPane scrollingText = new JScrollPane(mEditArea);
			
			//-- Do layout
			this.setLayout(new BorderLayout());
			this.add(scrollingText, BorderLayout.CENTER);
		}//end constructor
	}//end class contentPanel
	
	//============================================================ createMenuBar
	/** Utility function to create a menubar. */
	private JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
			JMenu fileMenu = menuBar.add(new JMenu("File"));
				fileMenu.add(mOpenAction);  // Note use of actions, not text.
				fileMenu.add(mSaveAction);
				fileMenu.add(mCleanAction);
				fileMenu.addSeparator(); 
				fileMenu.add(mExitAction);
		return menuBar;
	}//end createMenuBar
	
	//============================================================ createActions
	/** Utility function to define actions. */
	private void createActions() {
		mOpenAction = new AbstractAction("Open...") {
			public void actionPerformed(ActionEvent e) {
				int retval = mFileChooser.showOpenDialog(CleanCBpgnPad.this);
				if (retval == JFileChooser.APPROVE_OPTION) {
					File f = mFileChooser.getSelectedFile();
					try {
						FileReader reader = new FileReader(f);
						mEditArea.read(reader, "");  // Use TextComponent read
					} catch (IOException ioex) {
						System.out.println(e);
						System.exit(1);
					}
				}
			}
		};
		
		mSaveAction = new AbstractAction("Save") {
			public void actionPerformed(ActionEvent e) {
				int retval = mFileChooser.showSaveDialog(CleanCBpgnPad.this);
				if (retval == JFileChooser.APPROVE_OPTION) {
					File f = mFileChooser.getSelectedFile();
					try {
						FileWriter writer = new FileWriter(f);
						mEditArea.write(writer);  // Use TextComponent write
					} catch (IOException ioex) {
						System.out.println(e);
						System.exit(1);
					}
				}
			}
		};
		
		mCleanAction = new AbstractAction ("Clean"){
		public void actionPerformed (ActionEvent e) {
		int retval = mFileChooser.showCleanDialog(CleanCBpgnPad.this);
		if (retval== JFileChooser.APPROVE_OPTION){
		File f = mFileChooser.getSelectedFile();
		try {
		FileCleaner cleaner = new FileCleaner (c);
		mEditArea.clean(cleaner); // Use TextComponent clean
		  
		}catch (IOException ioex){
		String str = "1. e4 {[%emt 0:00:01]} c6 {[%emt 0:00:10]} 2. d4 {[%emt 0:00:03]} d5 {[%emt 0:00:01]} " + "3. e5 {[%emt 0:00:01]} Bf5 {[%emt 0:00:01]} 4. h4 {[%emt 0:00:02]} e6 {[%emt 0:00:02]}";
str = str.replaceAll("\\{.*?\\}", "");
System.out.println(str);
System.exit(1);
		
		}
		
		}
		
		}
				
		};
				
		
		mExitAction = new AbstractAction("Exit") {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		};
	}//end createActions	
}//end class CleanCBpgnPad



And finally for interest I include here a full chessbase pgn file as an example having the comments I want removed:
[Event "Rated game, 5m + 0s"]
[Site "Main Playing Hall"]
[Date "2007.08.02"]
[Round "?"]
[White "TR2"]
[Black "FOX062"]
[Result "1-0"]
[ECO "B12"]
[WhiteElo "1097"]
[BlackElo "1279"]
[PlyCount "55"]
[EventDate "2007.08.02"]
[TimeControl "300"]

1. e4 {[%emt 0:00:01]} c6 {[%emt 0:00:10]} 2. d4 {[%emt 0:00:03]} d5 {
[%emt 0:00:01]} 3. e5 {[%emt 0:00:01]} Bf5 {[%emt 0:00:01]} 4. h4 {
[%emt 0:00:02]} e6 {[%emt 0:00:02]} 5. Nc3 {[%emt 0:00:07]} Nd7 {[%emt 0:00:06]
} 6. g4 {[%emt 0:00:01]} Be4 {[%emt 0:00:02]} 7. Nxe4 {[%emt 0:00:03]} dxe4 {
[%emt 0:00:02]} 8. Bc4 {[%emt 0:00:17]} Qa5+ {[%emt 0:00:04]} 9. c3 {
[%emt 0:00:05]} b5 {[%emt 0:00:02]} 10. Bb3 {[%emt 0:00:08]} Be7 {
[%emt 0:00:02]} 11. Bc2 {[%emt 0:00:14]} Qd8 {[%emt 0:00:01]} 12. Bxe4 {
[%emt 0:00:03]} Bxh4 {[%emt 0:00:01]} 13. Nf3 {[%emt 0:00:06]} Be7 {
[%emt 0:00:04]} 14. Bxc6 {[%emt 0:00:17]} Rc8 {[%emt 0:00:07]} 15. Bxb5 {
[%emt 0:00:02]} Rb8 {[%emt 0:00:14]} 16. Bxd7+ {[%emt 0:00:12]} Qxd7 {
[%emt 0:00:01]} 17. g5 {[%emt 0:00:05]} f6 {[%emt 0:00:02]} 18. Qe2 {
[%emt 0:00:31]} fxe5 {[%emt 0:00:02]} 19. Qxe5 {[%emt 0:00:03]} Qd6 {
[%emt 0:00:07]} 20. Qxg7 {[%emt 0:00:02]} Bf8 {[%emt 0:00:41]} 21. Qxh8 {
[%emt 0:00:02]} Ne7 {[%emt 0:00:02]} 22. Rxh7 {[%emt 0:00:10]} Kd7 {
[%emt 0:00:11]} 23. Qe5 {[%emt 0:00:10]} Qxe5+ {[%emt 0:00:03]} 24. Nxe5+ {
[%emt 0:00:01]} Kd6 {[%emt 0:00:02]} 25. Rh8 {[%emt 0:00:07]} Nf5 {
[%emt 0:00:03]} 26. g6 {[%emt 0:00:06]} Ng7 {[%emt 0:00:08]} 27. Bf4 {
[%emt 0:00:05]} Ke7 {[%emt 0:00:01]} 28. Nc6+ {
FOX062 abbandona (Lag: Av=0.41s, max=2.2s) [%emt 0:00:05]} 1-0

Apologies if this is too much to put in an intro section!
Anyway I look forward to any help and advice with this, best wishes to all

Is This A Good Question/Topic? 0
  • +

Replies To: Intro

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Intro

Posted 13 August 2007 - 04:51 AM

Hi rpd, welcome. Post this same question in the Java forum, and we'll see what we can do ;)
Was This Post Helpful? 0
  • +
  • -

#3 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: Intro

Posted 13 August 2007 - 05:05 AM

View PostPennyBoki, on 13 Aug, 2007 - 04:51 AM, said:

Hi rpd, welcome. Post this same question in the Java forum, and we'll see what we can do ;)

Hi PennyBoki
Thanks for your reply- I will go to the JavaForum and post this there!
Best wishes
Was This Post Helpful? 0
  • +
  • -

#4 Thorian  Icon User is offline

  • Pirate Medic
  • member icon

Reputation: 22
  • View blog
  • Posts: 5,904
  • Joined: 06-June 02

Re: Intro

Posted 15 August 2007 - 01:31 PM

Welcome I am sure you will get the help you seek.
Was This Post Helpful? 0
  • +
  • -

#5 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: Intro

Posted 15 August 2007 - 02:06 PM

Hi Thorian
Thanks for your welcome and I'm glad to say Martyr2 has been very helpful already.I need to adjust the code to deal with carriage returns and to incorporate it into a text editor.However I am away on holiday for a while tomorrow so this will have to wait till I get back to the computer.
Anyway it seems I will get good help and advice here and I look forward to exploring the site more on my return.
Best wishes
Was This Post Helpful? 0
  • +
  • -

#6 Thorian  Icon User is offline

  • Pirate Medic
  • member icon

Reputation: 22
  • View blog
  • Posts: 5,904
  • Joined: 06-June 02

Re: Intro

Posted 16 August 2007 - 02:25 PM

Groovy.

Well if you ever have a hardware or networking problem i'm your man.
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Intro

Posted 16 August 2007 - 11:31 PM

Welcome to </dream.in.code> rpd, glad you could join us! Hope to see you around in the forums :)
Was This Post Helpful? 0
  • +
  • -

#8 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Intro

Posted 18 August 2007 - 09:37 AM

Welcome to DIC, glad u decided to join.
Was This Post Helpful? 0
  • +
  • -

#9 MarkoDaGeek  Icon User is offline

  • Dirty Technophile
  • member icon

Reputation: 11
  • View blog
  • Posts: 11,158
  • Joined: 13-October 01

Re: Intro

Posted 19 August 2007 - 12:30 AM

Hey! Welcome to </dream.in.code>! I'm looking forward to seeing you around the community.
Was This Post Helpful? 0
  • +
  • -

#10 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Re: Intro

Posted 19 August 2007 - 01:17 PM

Hi! My name is Chris. Welcome to Dream.In.Code!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1