Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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.
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
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.
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
kingjr3 said:

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();
 }
}
 

Texas04

macrumors 6502a
Jul 2, 2005
886
1
Texas
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
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
Texas04 said:
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

(I am uploading as we speak so it will be a few seconds if it does not all download)

Damn! Must be the wrong one :( gha thats annoying!

toddburch said:
Is this a homework assignment?

Todd

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

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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();
	}
}
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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)
 

Texas04

macrumors 6502a
Jul 2, 2005
886
1
Texas
What is up with your public void paint method? What does that do? And why do you pass it java.awt.Graphics g?
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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?
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
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!
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
You should really use the object tag, as applet is deprecated. Here's a good cross browser solution.

http://java.sun.com/j2se/1.5.0/docs/guide/plugin/developer_guide/using_tags.html#mixed

Scrolling up a screen or two and reading Using the applet Tag in that link tells me this:
Sun said:
Note: The HTML specification states that the applet tag is deprecated, and that you should use the object tag instead. However, the specification is vague about how browsers should implement the object tag to support Java applets, and browser support is currently inconsistent. Sun therefore recommends that you continue to use the applet tag as a consistent way to deploy Java applets across browsers on all platforms.

:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.