• Log in or sign up for an account here: https://www.sythe.org/login

[Guide]Making A Cool Java Program![Guide]

Morphis

Apprentice
Joined
Aug 2, 2008
Sythe Gold
0
Messages
734
Cool Java Program Guide​


This is a cool program that you can send to your friends and enemies as well to have fun.


Essentially this is a pretty useless program but it helps you familiarize yourself with java, and just have fun with java. This program will tell a person if they are cool or not and how cool they are based on their name.

Ok now before you begin I suggest you download Notepad++ as it is a very good free java editor and it is what I use.
NOTEPAD++ DOWNLOAD

The next thing you need to do is to configure Notepad++ to edit java code. So open up notepad++ and click on "Languages" on the top toolbar and go down to java and select that and you are all set!

You will also need to download and setup the java jdk to be able to compile your scripts. Look in the java section of sythe to find a guide on setting it up.



O.K. now that you have everything you need to write your own programs in java we will need to create a new project so do that.

IMPORTS - O.K. this basically tells java what classes it needs to import for the program to be able to run. Importing all of the classes will make java be a memory hog. Copy and paste these imports onto notepad++ this is the first part of your code.

Code:
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.Random;

If you want to know what each of those imports are just google them =).


O.K. so you have your imports now you need to setup your class name and other variables buttons and what not. Here is the things I used for my program. So copy and paste this into your notepad++ project.
Code:
public class CoolLevel extends JFrame implements ActionListener, Runnable  { 
     Thread go; 
     JTextField name = new JTextField(30); 
     JButton solve = new JButton("Go");
	 JButton clear = new JButton("Clear");
	 JTextArea awesomeness = new JTextArea(8,30);
	 int awesome = 80;
	 Random r = new Random();
	 String names;
the public class is just the name of the class which is "CoolLevel" extends JFrame means that it will be a window that appears when you run it. Implements ActionListener, Runnable. ActionListener is exactly what the name states it is, it listens for user input. Runnable means that the java program will have a thread in it which means that it will be a lot more efficient.
Thread go;, means I'm making a new thread named "go". JTextField is I'm making a new one same as the rest, I'm just creating buttons and strings and randoms for the program.


The next part is actually setting up the window and how things will be organized in it.
Copy and paste this into your notepad++ project.

Code:
    AwesomeLevel() { 
        super("YOUR PROGRAM NAME"); 
         setSize(430, 200); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         Container content = getContentPane(); 
         BorderLayout bord = new BorderLayout(); 
         content.setLayout(bord);
		 awesomeness.setEditable(false); 
         awesomeness.setLineWrap(true); 
         JScrollPane textPane = new JScrollPane(awesomeness); 
		 solve.addActionListener(this); 
         clear.addActionListener(this);		
		 JPanel centerPanel = new JPanel();
         JPanel topPanel = new JPanel(); 
		 JPanel bottomPanel = new JPanel();
		 bottomPanel.add(solve);
		 bottomPanel.add(clear);
		 centerPanel.add(awesomeness);
         topPanel.add(name);
	     name.setText("Enter your name here.");
         content.add(topPanel, BorderLayout.NORTH);  	
		 content.add(centerPanel, BorderLayout.CENTER);
		 content.add(bottomPanel, BorderLayout.SOUTH); 
         setVisible(true); 
     }

The only thing you need to edit in here is the second line where it says PROGRAM NAME HERE, put the name you want the program to have. This all just sets up the window that will open when this java program is ran.

This next part sets up what happens when each button is clicked, we have two buttons in this program solve and clear.

Code:
    public void actionPerformed(ActionEvent evt) { 
         Object source = evt.getSource();
		 
		 
         if(source == solve) {
         names = name.getText();
		 name.setEnabled(false);
		 if (go == null) { 
		 go = new Thread(this); 
		 go.start(); 
}		 
		
    }else if(source == clear){
    go = null;
	solve.setEnabled(true);
	name.setEnabled(true);
	name.setText("Enter your name here.");
	awesomeness.setText(null);
	awesome -= awesome;
	awesome += 80;
				}
	   }



Ok we are now almost done. Next we need to setup what will happen when the user presses the button "Go". And we will make it happen within a thread.

So copy and paste this once again into notepad++.

Code:
   public void run() {
   if(names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName")){
   awesome += r.nextInt(20);
   awesomeness.setText("Congradulations you have an awesome level of " + awesome + "!");
   }else if(names.equalsIgnoreCase("BESTFRIENDNAME")){
    awesomeness.setText("BESTFRIENDMESSAGE");
   
   }else if(names.equalsIgnoreCase("EnemyName#1")){
   awesomeness.setText("EnemyMessage#1");
   }else if(names.equalsIgnoreCase("EnemyName#2")){

    awesomeness.setText("EnemyMessage#2");
     }else{
	 awesomeness.setText("Sorry you are not awesome, try again when you get a life.");
	 }

    }

Now fill in the things above that have a label of, "FriendName", "BESTFRIENDNAME", "EnemyName#1", "EnemyName#2", "BESTFRIENDMESSAGE", "EnemyMessage#1", "EnemyMessage#2" Fill it in with what the name states IE. for friend name fill in a friends name, for Enemy name fill in the name of an enemy, for the ones that have message in them fill in what you want the message to say respectively. So for an enemy something bad, for you best friend something good.


Ok now the last part. To just create the window and close off the program with a bracket. So copy and paste this last part.

Code:
      public static void main(String[] arguments) { 
      CoolLevel frame = new CoolLevel();
     } 
	 
	 
 }





You whole code should look something along the lines of this

Code:
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.Random;

public class CoolLevel extends JFrame implements ActionListener, Runnable  { 
     Thread go; 
     JTextField name = new JTextField(30); 
     JButton solve = new JButton("Go");
	 JButton clear = new JButton("Clear");
	 JTextArea awesomeness = new JTextArea(8,30);
	 int awesome = 80;
	 Random r = new Random();
	 String names;
     
	  
    CoolLevel() { 
        super("YOUR PROGRAM NAME"); 
         setSize(430, 200); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         Container content = getContentPane(); 
         BorderLayout bord = new BorderLayout(); 
         content.setLayout(bord);
		 awesomeness.setEditable(false); 
         awesomeness.setLineWrap(true); 
         JScrollPane textPane = new JScrollPane(awesomeness); 
		 solve.addActionListener(this); 
         clear.addActionListener(this);		
		 JPanel centerPanel = new JPanel();
         JPanel topPanel = new JPanel(); 
		 JPanel bottomPanel = new JPanel();
		 bottomPanel.add(solve);
		 bottomPanel.add(clear);
		 centerPanel.add(awesomeness);
         topPanel.add(name);
	     name.setText("Enter your name here.");
         content.add(topPanel, BorderLayout.NORTH);  	
		 content.add(centerPanel, BorderLayout.CENTER);
		 content.add(bottomPanel, BorderLayout.SOUTH); 
         setVisible(true); 
     } 
 
    public void actionPerformed(ActionEvent evt) { 
         Object source = evt.getSource();
		 
		 
         if(source == solve) {
         names = name.getText();
		 name.setEnabled(false);
		 if (go == null) { 
		 go = new Thread(this); 
		 go.start(); 
}		 
		
    }else if(source == clear){
    go = null;
	solve.setEnabled(true);
	name.setEnabled(true);
	name.setText("Enter your name here.");
	awesomeness.setText(null);
	awesome -= awesome;
	awesome += 80;
				}
	   }
	
	
	
	


   public void run() {
   if(names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName") || names.equalsIgnoreCase("FriendName")){
   awesome += r.nextInt(20);
   awesomeness.setText("Congradulations you have an awesome level of " + awesome + "!");
   }else if(names.equalsIgnoreCase("BESTFRIENDNAME")){
    awesomeness.setText("");
   
   }else if(names.equalsIgnoreCase("EnemyName#1")){
   awesomeness.setText("EnemyMessage#1");
   }else if(names.equalsIgnoreCase("EnemyName#2")){

    awesomeness.setText("EnemyMessage#2");
     }else{
	 awesomeness.setText("Sorry you are not awesome, try again when you get a life.");
	 }

    } 

	
	
	
	
      public static void main(String[] arguments) { 
      CoolLevel frame = new CoolLevel();
     } 
	 
	 
 }

Now save it as CoolLevel.java and compile it. Find your CoolLevel.class file and drag it onto the desktop. And open up notepad++ again and create a new file. And paste this into it.
Code:
@ECHO off
java CoolLevel

save it as run.bat and save it on your desktop.

Now make a new folder and name it whatever you want. Place CoolLevel.java and run.bat into it and to run the program just double click run.bat . Check it out and redistribute it.

Guide is property of Morphis.​
 
Pokémon Trainer
Take a picture of what it does? Also add colour, bold stuff etc. to make it look nicer. Good start though, nice use of detail.
 
ya i got lazy towards the end. Ill add pictures and formatting soon.
 
good guide. need pictures and when i did all the steps when i run "run.bat"

it get this

Code:
Exception in thread "main" java.lang.NoClassDefFoundError: CoolLevel
Caused by: java.lang.ClassNotFoundException: CoolLevel
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

any help please?
 
looks nice i might try later.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
good guide. need pictures and when i did all the steps when i run "run.bat"

it get this

Code:
Exception in thread "main" java.lang.NoClassDefFoundError: CoolLevel
Caused by: java.lang.ClassNotFoundException: CoolLevel
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

any help please?

is the bat file in the same folder as the java file?


and i hate guides with copy and paste coding so no comment.
 
good guide. need pictures and when i did all the steps when i run "run.bat"

it get this

Code:
Exception in thread "main" java.lang.NoClassDefFoundError: CoolLevel
Caused by: java.lang.ClassNotFoundException: CoolLevel
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

any help please?

You need to compile it first using "javac".

The file name has to be CoolLevel.java exactly. It will not work otherwise (without modifying the class name of course)

Then run "javac CoolLevel.java". In a few seconds, you should see "CoolLevel.class" in the same directory. Save the batch file in that directory and you should be set.

c.png

):
 
The screenshot is a picture of what the program does/is. o.O
 
thank you cp, anyways. Ill post pictures soon, but I mean all I can do is take a picture of the program and thats about it.
 
cp said:
You need to compile it first using "javac".

The file name has to be CoolLevel.java exactly. It will not work otherwise (without modifying the class name of course)

Then run "javac CoolLevel.java". In a few seconds, you should see "CoolLevel.class" in the same directory. Save the batch file in that directory and you should be set.

Im really confused now, i think someone should make a guide on how to compile javac files.
 
Runescape Gamblingrunescape gambling
Back
Top