Results 1 to 14 of 14
  1. #1
    ami64 is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Smile Some code problem?

    hi every body.
    I 'm going to show you my first app related to learning java and that is hangman.

    Some code problem?-screenshot.png

    this is my classes

    Some code problem?-packages.png

    this is main class.

    /*************************************/
    Java Code:
    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import model.XMLData;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import objects.creator.KeyboardCreator;
    import objects.keyboard.Keyboard;
    import objects.word.Word;
    import objects.word.WordConstant;
    import objects.word.WordHolder;
    
    public class Hangman extends Application {
    
    	private StackPane stack;
    	private WordHolder wordHolder;
    	private Word myword ;
    	private ArrayList<String> _data;
    	private Keyboard keyboard;
    	
    	public void start(Stage stage){
    		initPage(stage);
    		
    		
    		
    		testWordHolder();
    		testKeyboard();
    		testXML();
    		
    		testWord();
    	}
    	
    	private void testXML() {
    		XMLData data = new XMLData();
    		_data = data.getData();
    	}
    
    	private void testWordHolder() {
    		wordHolder = new WordHolder();
    		wordHolder.getWordHolder().setId("words");
    		stack.getChildren().add(wordHolder.getWordHolder());
    		
    		//wordHolder.getWordHolder().setTranslateY(300);
    	}
    
    	private void testWord() {
    		myword= new Word();
    		
    		startGame();
    	}
    
    	private void startGame() {
    		String words = _data.get((int)(Math.floor(Math.random()*_data.size())));
    		myword.setWord(words);
    		myword.start();
    		wordHolder.setLetters(myword.getQues());
    	}
    
    	private void testKeyboard() {
    		EventHandler<ActionEvent> onClick = new EventHandler<ActionEvent>() {
    			
    			@Override
    			public void handle(ActionEvent event) {
    				
    				Button btn = (Button)(event.getTarget());
    				if(myword.check(btn.getText().charAt(0))){
    					wordHolder.putLetter(myword.getQues());
    				}
    				
    				if(myword.isWin()){
    					System.out.println("You win.. Next Level");
    					keyboard.disable();
    					new Timer().schedule(new TimerTask() {
    						
    						@Override
    						public void run() {
    							Platform.runLater(new Runnable() {
    								
    								@Override
    								public void run() {
    									startGame();
    									keyboard.enable();
    								}
    							});
    						}
    					}, WordConstant.DELAY);
    					
    					
    				}else if(myword.isFinish()){
    					System.out.println("You Lose!!");
    					keyboard.disable();
    				}
    			}
    		};
    		keyboard =  KeyboardCreator.create(onClick);
    		keyboard.getKeyboard().setId("keyboard");
    		stack.getChildren().add(keyboard.getKeyboard());
    		
    		
    	}
    
    	private void initPage(Stage stage) {
    		stack = new StackPane();
    		
    		Scene scene = new Scene(stack, 800, 450);
    	
    		stage.setTitle("Hangman");
    		stage.setScene(scene);
    		stage.show();
    			
    		scene.getStylesheets().add(Hangman.class.getResource("Style.css").toExternalForm());
    	}
    
    	public static void main(String[] args){
    		try{
    			System.out.println("hangman start");
    			launch(args);
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }
    /*************************************/




    XMLdata class.
    /*************************************/
    Java Code:
    package model;
    
    import java.io.File;
    import java.util.ArrayList;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    
    
    public class XMLData {
    	
    	private ArrayList<String> _words;
    
    	public XMLData(){
    		_words = new ArrayList<>();
    		
    		try{
    			File xmlFile = new File("src/words.xml");
    			DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    			DocumentBuilder db =  dbfactory.newDocumentBuilder();
    			Document doc = db.parse(xmlFile);
    			
    			doc.getDocumentElement().normalize();
    			
    			NodeList nList = doc.getElementsByTagName("word");
    			int len = nList.getLength();
    			for(int i=0; i<len; i++){
    				Node node = nList.item(i);
    				Element element = (Element)node;
    				_words.add(element.getAttribute("value"));
    			}
    			
    		}catch(Exception e){
    				e.printStackTrace();
    		}
    	}
    	
    	public ArrayList<String> getData(){
    		return _words;
    	}
    }
    /*************************************/



    KeboardCreator
    /*************************************/
    Java Code:
    package objects.creator;
    
    import java.util.ArrayList;
    
    import objects.keyboard.Keyboard;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Node;
    
    public class KeyboardCreator {
    
    	public static Keyboard create(EventHandler<ActionEvent> onClick){
    		Keyboard keyboard = new Keyboard(onClick);
    		
    		ArrayList<String> letters = new ArrayList<>();
    		letters.add("Q");
    		letters.add("W");
    		letters.add("E");
    		letters.add("R");
    		letters.add("T");
    		letters.add("Y");
    		letters.add("U");
    		letters.add("I");
    		letters.add("O");
    		letters.add("P");
    		keyboard.addNextRow(letters);
    				
    		letters.clear();
    		letters.add("A");
    		letters.add("S");
    		letters.add("D");
    		letters.add("F");
    		letters.add("G");
    		letters.add("H");
    		letters.add("J");
    		letters.add("K");
    		letters.add("L");
    		keyboard.addNextRow(letters);
    		
    		letters.clear();
    		letters.add("Z");
    		letters.add("X");
    		letters.add("C");
    		letters.add("V");
    		letters.add("B");
    		letters.add("N");
    		letters.add("M");
    		keyboard.addNextRow(letters);
    	
    		
    		
    		return keyboard;
    	}
    }
    /*************************************/



    Keyboard
    /*************************************/
    Java Code:
    package objects.keyboard;
    
    import java.util.ArrayList;
    
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Pos;
    import javafx.scene.Node;
    import javafx.scene.control.Button;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    
    public class Keyboard {
    
    	private VBox _keyboard;
    	private EventHandler<ActionEvent> _onClick ;
    	
    	public Keyboard(EventHandler<ActionEvent> onclick){
    		_onClick = onclick;
    		_keyboard= new VBox();
    		
    	}
    	
    	public void addNextRow(ArrayList<String> letters){
    		int len = letters.size();
    		Button keyBtn;
    		HBox hb = new HBox();
    		for(int i=0; i<len; i++){
    			keyBtn = new Button(letters.get(i));
    			keyBtn.setOnAction(_onClick);
    			hb.getChildren().add(keyBtn);
    		}
    		hb.setId("hboxid");
    		
    		_keyboard.getChildren().add(hb);
    	}
    	
    	public void disable(){
    		_keyboard.setDisable(true);
    	}
    	
    	public void enable(){
    		_keyboard.setDisable(false);
    	}
    	
    	public Node getKeyboard(){
    		return _keyboard;
    	}
    }
    /*************************************/



    Word
    /*************************************/
    Java Code:
    package objects.word;
    
    import java.util.ArrayList;
    
    import utils.Utils;
    
    public class Word {
    
    	private float _wrongFactor;
    	private float _visibleFactor;
    	private ArrayList<Character> _quesWord;
    	private ArrayList<Character> _realWord;
    	private int _wrongCount;
    	private int _emptyCount;
    	
    	
    	public Word(){
    		_quesWord = new ArrayList<>();
    		_realWord = new ArrayList<>();
    		
    		_visibleFactor = 0.33f;
    		_wrongFactor = 2;
    	}
    	
    	
    	public void setwrongFactor(float value){
    		_wrongFactor = value;
    	}
    	
    	public void setVisibleWordFactor(float value){
    		 _visibleFactor = value;
    	}
    	
    	public void setWord(String word){
    		_realWord.clear();
    		_quesWord.clear();
    		int len = word.length();
    		for(int i=0; i<len; i++){
    			_realWord.add(word.charAt(i));
    			_quesWord.add(WordConstant.NULL_CHAR);
    		}
    		
    	}
    	
    	public ArrayList<Character> start(){
    		int len = _realWord.size();
    		
    		_wrongCount = len;
    		
    		//check visible position;
    		int cnt = (int)Math.round((len * _visibleFactor));
    		int[] random = Utils.getRandomArray(len);
    		for(int i=0; i<cnt; i++){
    			char select = _realWord.get(random[i]);
    			_quesWord.set(random[i], select);
    			_wrongCount--;
    		}
    		_emptyCount = _wrongCount;
    		_wrongCount =(int)(_wrongCount*_wrongFactor);
    		return _quesWord;
    	}
    	
    	
    	public ArrayList<Character> getQues(){
    		return _quesWord;
    	}
    	
    	public boolean isFinish(){
    		if(_wrongCount<=0){
    			_wrongCount = 0;
    			return true;
    		}
    		return false;
    	}
    	
    	public boolean isWin(){
    		if(_emptyCount<=0){
    			return true;
    		}
    		return false;
    	}
    	
    	public boolean check(Character character){
    		character =Character.toLowerCase(character);
    		int len = _realWord.size();
    		boolean has =false;
    		for(int i=0; i<len; i++){
    			if(_quesWord.get(i).equals('$') && _realWord.get(i).equals(character)){
    				_quesWord.set(i,  character);
    				has = true;
    				_emptyCount--;
    			}
    		}
    		if(has){
    			return true;
    		}
    		_wrongCount--;
    		return false;
    	}
    }
    /*************************************/



    WordConstant
    /*************************************/
    Java Code:
    package objects.word;
    
    public class WordConstant {
    
    	public static final char NULL_CHAR = '$';
    	public static final long DELAY = 2000;
    }
    /*************************************/



    WordHolder
    /*************************************/
    Java Code:
    package objects.word;
    
    import java.util.ArrayList;
    
    import javafx.scene.Node;
    import javafx.scene.control.Label;
    import javafx.scene.layout.GridPane;
    
    public class WordHolder {
    	
    	private GridPane _wordHolder;
    	private ArrayList<Label> _labels;
    	private String _empty="   ";
    	
    	public WordHolder(){
    		_wordHolder = new GridPane();
    		_labels = new ArrayList<>();
    	}
    	
    	public Node getWordHolder(){
    		return _wordHolder;
    	}
    	
    	public void putLetter(ArrayList<Character> letters){
    		int len2 =letters.size();
    		for(int i=0; i<len2; i++){
    			String select = letters.get(i).equals(WordConstant.NULL_CHAR) ? _empty : String.valueOf(letters.get(i));
    			if(select != _empty){
    				_labels.get(i).setUnderline(false);
    			}else{
    				_labels.get(i).setUnderline(true);
    			}
    			_labels.get(i).setText(String.valueOf(select));
    		}
    	}
    	
    	public void setLetters(ArrayList<Character> letters){
    		int len1 =_labels.size();
    		int len2 =letters.size();
    		manageSize(len1, len2);
    		
    		putLetter(letters);
    	}
    
    	private void manageSize(int len1, int len2) {
    		if(len1<len2){
    			int column = len1;
    			for(int i=len1; i<len2; i++){
    				Label label = new Label();
    				_wordHolder.add(label, column, 0);
    				column++;
    				_labels.add(label);
    				
    			}
    		}else if(len1>len2){
    			for(int i=len2; i<len1; i++){
    				Label label = _labels.get(i);
    				label.setVisible(false);
    			}
    		}
    		//show them if they are hidden in last parts
    		for(int i=0; i<len2; i++){
    			_labels.get(i).setVisible(true);
    			_labels.get(i).setText(_empty);
    		}
    	}
    	
    	
    }
    /*************************************/




    utils
    /*************************************/
    Java Code:
    package utils;
    
    import java.util.ArrayList;
    
    public class Utils {
    
    	public static int[] getRandomArray(int size){
    		ArrayList<Integer> real = new ArrayList<Integer>();
    		int[] random = new  int[size];
    		for(int i=1; i<=size; i++){
    			try{
    				real.add(i-1, Integer.valueOf(i-1));
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    		
    		for(int i=1; i<=size; i++){
    			
    			int index = (int)Math.floor(Math.random()*real.size());
    			random[i-1] = real.get(index);
    			real.remove(index);
    		}
    		
    		return random;
    	}
    }
    /*************************************/



    style
    /*************************************/
    Java Code:
    .root{
    	-fx-background-image :url("../back.png");
    	-fx-background-size: 100% 100%;
    }
    
    .label{
    	-fx-font-size:30;
    	-fx-font-weight:bold;
    	-fx-text-fill:#000000;
    	
    	-fx-text-alignment:center;
    	-fx-min-width:40;
    	
    }
    
    
    .button{
    	-fx-font-size:20;
    	-fx-text-fill:#5b54b3;
    	-fx-font-weight:bold;
    	-fx-text-alignment:center;
    	-fx-font-family:"Arial Narrow";
    	-fx-background-color:linear-gradient(from 0% 0% to 0% 50% ,#ffffff,#eff6a4 99% ,#eff6a4);
    	-fx-effect:innershadow(three-pass-box,#808337, 10, 0.3, -2,-2);
    	-fx-padding:0;
    	-fx-min-width:30;
    	-fx-min-height:30;
    	
    }
    
    .button:hover{
    	-fx-font-size:20;
    	-fx-background-color:linear-gradient(#f6de58 50%, #FFF );
    	-fx-effect:innershadow(three-pass-box,#808337, 10, 0.3, 2,2);
    }
    
    #words{
    	-fx-hgap:0;
    	-fx-padding:10;
    	-fx-alignment:center;
    }
    
    #keyboard{
    	-fx-padding:10;
    	-fx-spacing:3;
    	-fx-alignment:top-left;
    }
    
    #hboxid{
    	-fx-spacing:3;
    	-fx-alignment:center;
    	-fx-max-width:325;
    }
    /*************************************/


    I want to know if there is any problem in my class design or coding. or if there is a better way to implement the program and I choose the bad way.

    the other thing is that i do not know how to export my project that can be run in any machine. when I export in folder that is not the folder which my classes is in, the program exit suddenly. if I export the program in my project folder, then the background image isn't shown.
    Last edited by ami64; 07-15-2014 at 11:01 PM.

  2. #2
    ami64 is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: Beginner in Java Programming

    I solve the problem which makes the program exit.it was the image folder, should be near the jar file.
    jarFile.zip

  3. #3
    Zwander is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Beginner in Java Programming

    Dat necro.
    (Now I'm guilty too )

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Beginner in Java Programming

    Very cool that its a JavaFX program though! Looks like a job very well done.

    Perhaps it should be isolated to a unique thread to not make that code a lure for people who are learning to copy that code instead of writing their own stuff and actually learning something.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Some code problem?

    Please don't hijack someone else's thread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Beginner in Java Programming

    Quote Originally Posted by gimbal2 View Post
    Very cool that its a JavaFX program though! Looks like a job very well done.

    Perhaps it should be isolated to a unique thread to not make that code a lure for people who are learning to copy that code instead of writing their own stuff and actually learning something.
    Missed this one in my Grand Move!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Some code problem?

    Ah but I'm very elusive.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    ami64 is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: Some code problem?

    thank you guys for replying.
    if there is any problem or OOP rule that I do not pay attention to that, in my code, I'll be so happy to know that.

    Very cool that its a JavaFX program though! Looks like a job very well done.

    Perhaps it should be isolated to a unique thread to not make that code a lure for people who are learning to copy that code instead of writing their own stuff and actually learning something.
    thank you gimbal2 , it's kind of you.if I put some animation in that ,perhaps it becomes better.

    what is next step. should I continue making program like this for being a good java developer?!
    what do you suggest?!

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Some code problem?

    Your next project should be a little more complicated than hang man of course, you need to push yourself into new territory to learn new things and refine what you have learned so far. But it depends on what you are interested in before you can make the decision on what project it is going to be. Do you want to make another game?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    ami64 is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: Some code problem?

    it makes no difference, games or other stuff. just I want to learn.
    another thing is that ,I don't know to practice in which branch of java, game or application, mobile, servlet, etc ...
    my goal is making money.
    which of them is better to dive into?!

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Some code problem?

    You're taking a really lazy approach to this now, I can't make any recommendations when you just want to get told what to do.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,030
    Rep Power
    4

    Default Re: Some code problem?

    If I say "mobile" and you are bankrupt, homeless and STD ridden within a year, you won't blame me, right? RIGHT?

    Edit: You want to become a coder because you like to code. A lot. You can't be in it for the money. If you're *really* interested in making money, you should become a manager at a bank. You'll drown in your salary and bonuses.
    Last edited by SurfMan; 07-16-2014 at 04:41 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  13. #13
    ami64 is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: Some code problem?

    You're taking a really lazy approach to this now
    why lazy?!
    just I want to know, because I want to use other experience.

    you mean it's better that I make my hand dirty and dive into problem (no matter what )and solve it in best way?!

  14. #14
    ami64 is offline Member
    Join Date
    Jun 2014
    Posts
    12
    Rep Power
    0

    Default Re: Some code problem?

    You want to become a coder because you like to code. A lot. You can't be in it for the money.
    Very Good statement. really I love programming.

    no, I do not want to become a manager in a bank.money is important for me but not in that level.

    Just I want to make it interesting to learn and do code in next level.

Similar Threads

  1. I'm not sure where there is a problem with the code...
    By Blondedude092 in forum New To Java
    Replies: 5
    Last Post: 10-01-2012, 01:24 AM
  2. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  3. Replies: 9
    Last Post: 09-21-2010, 05:15 PM
  4. problem in my code
    By wannabe in forum New To Java
    Replies: 5
    Last Post: 04-12-2010, 05:38 AM
  5. Help me!!!!!!!! Problem with Code!!
    By Miyaki in forum New To Java
    Replies: 0
    Last Post: 03-08-2009, 01:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •