Java for web

Discussion in 'Mac Programming' started by Jasonbot, Oct 2, 2007.

  1. Jasonbot macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #1
    I want to make some basic java applications and use them in a standard web browser. How do I do this? What do I use instead of JOptionPane to display stuff.

    I know basic java and can create some pretty 1337 code (compared to my classmates). I haven't made my own GUI's yet but I'd like to learn some time.

    Basically, can someone show me how to do a basic hello world to display on a website. I dont know if I'm being vague but please try and help me.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
  3. Jasonbot thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #3
    Will do, thanks!
     
  4. psingh01 macrumors 65816

    Joined:
    Apr 19, 2004
    #4
    You can use Java Applets to make desktop like applications appear on your webpage. The other way to use Java is on the backend of your webserver. They are called Java Servlets and basically handle requests from the web and return HTML. There are two types of servlets too, those written in pure Java code and compiled like any other program and those written in JSP which is like a mixture of HTML and Java. For the backend stuff you will need to download something like Apache Tomcat which is a webserver enabled to run Servlets.
     
  5. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
  6. Jasonbot thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #6
    I kinda got that XD

    Now, lets say I wanted to convert this .jar into a web applet. How would I go about doing that?

    Code:
    import javax.swing.*;
    import java.io.*;
    
    public class Gam3r
    {
    	public Gam3r()
     {
    		
    //stinky programme
    //new design brief: make hangman!
    		String input,guess,sx="",pr="";
    		int i,cnt,x=0,y,p,q,r,left=10,m=0,f=0,l=0;
    		boolean gah=true,win=false,end=false;
    	
    		input=JOptionPane.showInputDialog(null, "insert your word");
    		
    //insert the word
    		cnt=input.length();
    		String hm[]=new String[cnt];
    		String disp[]=new String[cnt];
    		
    //put the word in an array
    		for(i=0;i<cnt;i++)
    		 {
    			hm[i]=input.substring(i,i+
    								  1);
    			disp[i]="_";
    			System.out.println(disp[i]+" "+hm[i]);
    		 }
    		
    //guess
    		do{	
    			x++;
    			guess=JOptionPane.showInputDialog(null, "make yer guess");
    			
    			l=0;
    			
    			for(y=0;y<cnt;y++)
    			 {
    				sx="";	
    				if(hm[y].equalsIgnoreCase(
    										  guess))
    				 {
    					f++;
    					System.out.println("goes into loop true");
    					disp[y]=guess;
    					
    					for(p=0;p<cnt;p++)
    					 {
    						sx=sx+disp[p];
    					 }
    					gah=true;
    				 }
    				
    				else 
    				 {
    					l++;
    					System.out.println("goes into loop false");
    					
    					for(q=0;q<cnt;q++)
    					 {
    						sx=sx+disp[q];
    					 }
    					if(l==cnt)
    					 {
    						gah=false;
    						left=left-1;
    					 }
    				 }	
    			 }
    			
    			pr=pr+guess+",";
    			r=0;
    			
    			for(p=0;p<cnt;p++)
    			 {
    				if(disp[p]!="_")
    				 {
    					System.out.println("this works");
    					r++;
    					if(r==cnt)
    					 {
    						System.out.println("this works2");
    						win=true;
    						end=true;
    					 }
    				 }
    				else
    				 {
    					System.out.println("win=false");
    					win=false;
    				 }
    			 }
    			
    			if(gah==true)
    			 {
    				JOptionPane.showMessageDialog(null,"Correct guess ya beast\n"+sx+"\n "+left+" guesses left\nold guesses: "+pr);
    			 }
    			
    			else if(gah==false)
    			 {
    				JOptionPane.showMessageDialog(null,"Wrong guess ya beast\n"+sx+"\n "+left+" guesses left\n old guesses: "+pr);
    			 }
    			
    			if (end=true) 
    			 {
    				left=0;
    			 }
    			
    		}while(left!=0);
    		
    		
    		System.out.println("It gets here");
    		
    		if(win==true)
    		 {
    			JOptionPane.showMessageDialog(null,"Congratulations, you won\nThe correct word was '"+input+"'");
    			new Gam3r();
    		 }
    		else if(win==false)
    		 {
    			JOptionPane.showMessageDialog(null, "All out of guesses, try again");
    			new Gam3r();
    		 }
     }
    	public static void main(String[]args)
     {
    		new Gam3r();
     }
    }
     
  7. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  8. Texas04 macrumors 6502a

    Texas04

    Joined:
    Jul 2, 2005
    Location:
    Texas
    #8
    Well, I ran your program on my mac, and it didnt seem to work.... It only lets you have 1 guess versus 10. :-/

    I am uploading my Hangman to my website now...

    My Hangman
    My friend has the final version, had a bigger canvas to play on, but this was our last Comp Sci Project... you might be able to get the class files out of it and reference it for the loops and stuff.. good luck!

    And have fun playing Hangman Ole! :D
     
  9. Jasonbot thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #9
    Damn! Must be the wrong one :( gha thats annoying!

    Fortunately not! Since in my class we are only just learning about buffered readers/writers- nevermind the really hard java web app. stuff :O
     
  10. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    OK then, I fixed your version. Perhaps this evening I'll convert it to an applet.

    I would suggest you change the way you code a little bit:

    1) use meaningful variable names.
    2) validate user input - always
    3) comment your code sections with what you are trying to accomplish

    Todd

    Code:
    import javax.swing.*;
    import java.io.*;
    
    public class Gam3r {
    	public Gam3r() {
    		
    	//stinky programme
    	//new design brief: make hangman!
    	
    	String input, guess ; 
    	String current_guesses = "" ;  
    	String guessed_letters = "" ;
    	String display_string ; 
    	int i, j, cnt, y ;  
    	int guesses_left = 10 , matched_letters = 0 ; 
    	boolean letter_match , full_match, unique_guess ; 
    
    	input = JOptionPane.showInputDialog(null, "insert your word");
    	if (input == null) return ;   // if user presses cancel.  
    	
    	//insert the word
    	cnt= input.length();
    	if (cnt==0) return ; 
    	if (cnt > guesses_left ) { 
    		JOptionPane.showMessageDialog(null,"String too long.  Make it shorter than " + guesses_left );
    		new Gam3r();
    		return ; 
    	} 
    	
    	String hm[]   =new String[cnt];   // The user's string. 
    	String disp[] =new String[cnt];   // Display the matching guess results
    		
    	//put the word in an array, a letter at a time
    	for (i=0 ; i<cnt ; i++) {
    		hm[i] = input.substring(i,i+1);    // start position, end position 
    		disp[i] = "_";
    		System.out.println(disp[i]+" "+hm[i]);
    	 }
    
    		
    	//  Main loop 
    	for ( --guesses_left ; guesses_left >= 0 ; guesses_left-- )  {	
    
    		unique_guess = true ; 
    		do { 
    			guess = JOptionPane.showInputDialog(null, "Guess a single Letter you have not Guessed before");  // The user guesses a letter only. 
    			if (guess==null) return; // user pressed cancel 
    			if (guess.length()==1) { 
    				//System.out.println("indexof = " + guessed_letters.indexOf(guess) ) ; 
    				if (guessed_letters.indexOf(guess) < 0 ) unique_guess = true ;   // -1 means not found 
    				else unique_guess=false ; 
    			} 
    		} while (guess.length() != 1 | (!unique_guess) ) ;    // Force the user to only enter 1 letter 
    
    		if  (guessed_letters.length() == 0) guessed_letters = guess ; 
    		else guessed_letters = guessed_letters + ", " + guess ;   // String of guessed characters. 
    		
    		//l=0;
    		letter_match = false ;   // assume no match 
    		for ( y=0 ; y<cnt ; y++ ) {    // Look for a matching character. 
    
    			if (hm[y].equalsIgnoreCase(guess)) {  // Does the letter match?  
    				letter_match = true ; 
    				matched_letters++ ; 
    				disp[y] = hm[y] ;  // Set output string to the proper letter & proper case 
    			}  // if letter matches 				
    		}    // for (y=0)
    
    		// Compare the guess so far to what the user first entered.  
    		full_match = true ;  //Assume a full match   
    		
    	 	for (y = 0 ; y < cnt ; y++) { if (!disp[y].equals(hm[y])) full_match = false ; } 
    		
    		if ( full_match ) {  
    			JOptionPane.showMessageDialog(null,"Congratulations, you won\nThe correct word was '"+input+"'");
    			new Gam3r();
    			return ; 
    		}
    
    		for (j=0, display_string = "" ; j < cnt ; j++ ) display_string = display_string + disp[j] ;  
    
    		if (letter_match) { 
    			JOptionPane.showMessageDialog(null,"Correct guess ya beast\n"+ 
    			display_string + "\n "+guesses_left+" guesses left\nold guesses: "+ guessed_letters);
    			} 
    
    		else { 
    			JOptionPane.showMessageDialog(null,"Wrong guess ya beast\n"+
    				display_string + "\n "+guesses_left+" guesses left\n old guesses: "+guessed_letters);
    		} 
    
    	}   // for (guesses_left...) 
    		
    	JOptionPane.showMessageDialog(null, "All out of guesses, try again");
    	new Gam3r(); 
    
    }
    
    public static void main(String[]args) {
    	new Gam3r();
    	}
    }
    
     
  11. Jasonbot thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #11
    Thanks! I appreciate the code cleaning and assistance. I saw my own errors before looking @ your code tho. Except I'm amazed that yours is so clean :O
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #12
    OK, here's a link to the applet, loaded up and ready to run:

    http://www.burchwoodusa.com/java/HangMan.html

    Here's the wrapper code to run as an applet (It's a shell I use that I generate programmatically - so ignore (and learn from) the unused methods).

    Code:
    public class HangMan extends java.applet.Applet {
    
    	public void init() { } 
    	public void start() { } 
    	public void stop() { } 
    	public void destroy() { } 
    	
    	public void paint(java.awt.Graphics g) {  
    		new Gam3r();
    	}
    }
    Not much to it. I had to remove the System.out.println... statements from Gam3r.java and recompile. And, I had to upload the HangMan.class and Gam3r.class files to my website as well.

    Todd

    (edit - I'll delete it in a few days)
     
  13. Texas04 macrumors 6502a

    Texas04

    Joined:
    Jul 2, 2005
    Location:
    Texas
    #13
    What is up with your public void paint method? What does that do? And why do you pass it java.awt.Graphics g?
     
  14. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #14
  15. Jasonbot thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #15
    So taht wrapper is for the class file 'HangMan.class' right? now in HTML how do I get the code to open up the web applet?

    From my understanding you have 2 separate class files (HangMan.class and Gam3r.class) right?
     
  16. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #16
    Right.

    When viewing the HTML page, right click to view source.

    Exactly.

    Todd
     
  17. Jasonbot thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
  18. Jasonbot thread starter macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #18
    Really sorry to res this old thread but now I really need this now, may I please have the source code for the webpage that launched the java app. Thanks!
     
  19. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #19
    Obviously, you'll have to customize it for your website.

    Code:
    <html>
    <head>
    <title>HangMan</title>
    </head>
    <body>
    <applet 
    codebase="[color=red]http://www.some_web_site.com/cgi-bin/[/color]" 
    code="HangMan" width="400" height="400">
    </applet> 
    </body>
    </html> 
    
    Todd
     
  20. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
  21. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #21
    Scrolling up a screen or two and reading Using the applet Tag in that link tells me this:
    :)
     

Share This Page