Tuesday, February 24, 2009

March 5: Menus, part two

OK, so now you have a menubar with one menu and one menuitem on that menu. It doesn't DO anything though, now does it? That's simple enough to fix; I bet some of you already added to your action performed statement like so:



else if (e.getSource() == quit)

{

System.exit(0);

}// lets quit using using the menu



If you haven't tried that, why don't you do so now?

If you have done that, why don't you try adding the other menuitems to the menu



Cheers,

Uncle Paulie

March 5: Menus, part one

Greetings, JavaNoidz: today, we will be adding menus to your programs. As always, your ever so helpful instructor and all around good guy has set up the base level of the program for you; it can be found here:


http://www.box.net/shared/p2gx5ecl5b

the file is named JavaMenus. Please download it and compile it and make sure it runs. Got it? K3WLLLLLLL!!!!! Let's go!


A) first, let's understand some concepts. A "MenuBar" is just that: a bar that contains menus. It's like the different toolbars you can have in IE or Firefox, and they can hold many


"Menus". Menus have some sort of text label, such as FILE, EDIT, VIEW, so on and so forth. When you click on a Menu, you get to see a big list of


"MenuItems". Under FILE, you might see MenuItems such as NEW, OPEN, SAVE, PRINT, EXIT


B) OK, so let's start by declaring one MenuBar, one Menu and several MenuItems, like so:

MenuBar myHouse;
Menu myRoom;
MenuItem inky,pinky,blue,quit;



do so just below the line that declares your exit button:
Button exit; //declare button


C) Next, let's get into your constructor and actually create a menubar and a menu:
myHouse = new MenuBar(); //create a MenuBar
myRoom = new Menu("FILE"); //create a Menu



do this right below where I added the "exit" button to the frame
Gumby.add(exit);


D) Just below where you created the menubar and menu, you will create one menuitem, give it an actionListener, then add it to the menu, like so:
quit = new MenuItem("End the program"); //create a menu item
quit.addActionListener(this);//give it a listener
myRoom.add(quit); //add this menuitem to the menu



E) Just below that, add these two lines to add the menu to the menubar, and the menubar to the frame:
myHouse.add(myRoom);//add the menu to the menubar
Gumby.setMenuBar(myHouse);//add the menubar to the frame



F) At this point, run the program to make sure you can see the menubar, its one menu and that menu's only menuitem. When you click on "End the program" notice that it doesn't do that. I now give you some time to figure out how to make that happen. Hint: all the code you need is sitting right in front of you.


cheers,
Uncle Paulie

Friday, February 20, 2009

Thurs March 5: importing custom classes, do your own thing

OK, so now we come to the part where you "do your own thing" You may make any combination of what we've learned in the past several days about creating your own classes with what you already knew from before. As always, do the following:

A) send me your updated program by email, with your name and "custom classes" in the subject line

B) Post a discussion on our Google group telling me what you did. Be sure to label it "custom classes"

Cheers,
Uncle Paulie



PS Today was the day back in 1953 that Josef Stalin died.

"I am Ozymandias, king of kings....."

Wed March 4: importing custom classes, part three

OK, so now we've made a class that creates a method we can use. Can we do the same for components? For instance, can we create a custom Panel with buttons already on it that we can stick onto any program we want.

You betcha we can!

Here's how we do it:

A) In the same folder as your other two classes, you will create a new class. Let's call it "ButtonPanel"; it'll start off looking like this:

import java.awt.*;
import java.awt.event.*;
class ButtonPanel extends Panel
{


}//end class

B) compile it and make sure its OK, then we'll declare three buttons in between those curly braces:
Button a,b,c;

C) next, we will make a constructor for this class, right below where we declared those buttons:
ButtonPanel()
{


}

D) Inside the curly braces for that constructor, lets give this custom Panel a GridLayout:
setLayout(new GridLayout(1,3));

and then add in those three buttons we declared. Here's how you do the first one:
a = new Button("Button A");
add(a);

E) COMPILE that new ButtonPanel class, an when you get no errors, get back to your main program. Declare a new object based on the Button Panel class by entering this line:
ButtonPanel myNewPanel;

below this line:
int firstNum, secondNum;

F) Finally, create this new object inside your main program by entering these two lines:
myNewPanel = new ButtonPanel();
Valhalla.add(myNewPanel);

above this final line in your constructor:
Valhalla.show();

Compile, then run the program. Does the custom panel with three buttons show up in your program? Hurrah!

"think good thoughts"
Uncle Paulie

Fried A Feb 27: importing custom classes, part two

OK, let's review what we just saw:

1) you can create a class, and give it a method

2) you can create an object in another class based on that first class

3) once you've done that, then use the method from the first class to do stuff in the second class

OK, so let's make this program a little more useful. Right now, all it does is print the number 6 to the screen!

Here's what we do:

A) right below this line
CalcOne myAdd; //a new object named myAdd based on CalcOne class

add these two lines to declare two TextFields and two ints
TextField numOne, numTwo;
int firstNum, secondNum;

B) in the constructor, just below this line:
myAdd = new CalcOne();//put this new object in memory

put both those TextFields into memory and add them to the Frame. Also, set both ints equal to zero (the number 0 that is)

C) Lastly, we are going to use the Integer.parseInt method to make an int out of whatever number is typed into those textFields, then add those two ints. To do that, we are going to need to change your "else if" statement from this:

System.out.println(myAdd.addTwo(3,3));

to this
firstNum = Integer.parseInt(numOne.getText());
secondNum = Integer.parseInt(numTwo.getText());
System.out.println(myAdd.addTwo(firstNum,secondNum));

This way, your new method from another class is going to use whatever integers you type into the two TextFields. Try it and see what you get!

Cheers,
Uncle Paulie

Fried Hay, Feb 26: importing custom classes, part one

Greetings, Java-Nation!

Today, we are going to start exploring a concept that is at the very heart of JAVA: the class. Java, as they say, is class-based; everything in it is derived from a class that you create, or from a class that's been there from the beginning, one that's written into the language. Today, we are going to learn how to create two separate classes, each of which does something completely different, and then import these classes into a main program, create objects out of them, and do something with each one. So let's get going by downloading your starter program from here:
http://www.box.net/shared/inp8pt71ph

It should be a file called "FunWithClasses" Download it to a folder named after yourself, PLUS the phrase "fun with classes". Compile it and run it just to make sure its A-OK. I'll wait right here.

zzzz.......
zzzz............
zzz.....huh?
Oh, you're all set; GREAT! Now let's get down to business.

The first thing you will have to do is create a separate class file in TextPad by clicking File then New. Call it "CalcOne" like so:
class CalcOne
{

}//end CalcOne

then save it IN THE SAME FOLDER AS YOUR MAIN PROGRAM. make sure the filename matches the class name -- duh!

The next thing is to give this class something to do. Let's make it add two integers and return the result, like so:

int addTwo(int stuffOne, int stuffTwo)
{
return stuffOne + stuffTwo;
}//end int add method

You need to put this int add method ABOVE the closing line of this new class. SAVE, COMPILE, then get back into your main program

The next step is to make an object from this new class, and put this object into your program. We do it in the same way we've added other classes; let's sing along with Uncle Paulie, OK?

beneath this line
Button exit; //declare button

add this line
CalcOne myAdd; //a new object named myAdd based on CalcOne class

The next step is to actually put this new object into your program. Go to this line:
Valhalla.add(exit);

and add this line
myAdd = new CalcOne();//put this new object in memory

RE-COMPILE, then run the program. Hmmmmmmm...it doesn't LOOK any different! Well that's because this class has no visible components, just one method for adding numbers. So let's add some stuff that can make use of this method:
Below this line
Button exit; //declare button

type in this line to create a second button
Button AddEm; //a button to add stuff

Just ABOVE this line:
myAdd = new CalcOne();//put this new object in memory

create this new button you've just declared, give it an actionListener, add it to the Frame

Now for the good stuff: you are going to add an "else if" to your actionPerformed. This else if will make use of the one method found in your new class to add two ints together. Here's what it looks like:

else if (e.getSource() == AddEm)
{
System.out.println(myAdd.addTwo(3,3));
}// lets add

This uses the "addTwo" method from the class called "CalcOne". Since myAdd is based on that class, your new "myAdd" object can use ANY METHOD IN THAT CLASS.

After that, we pass this method two integers, and print the result to the System console. Hint: you should see the number "6"

Cheers,
Uncle Paulie

Thursday, February 19, 2009

arrays and vectors, part four

Alrighty then, I said I would explain the difference between size and capacity, so here it is:

"capacity is how much room for elements there is in a Vector, while size is how many actual elements there are"

It's kind of like an egg carton: you can have space for 6 or 12 or 18 eggs, but its possible that NONE of those spaces have been filled. Right now, we have an empty egg carton. SO, the next step is to FILL the carton. Here's how:

A) the first step is to dcelare a button we can click and add an element to the Vector:
Button newOne;//a button for making new vector elements

B) we will also need another TextField, so we can create some stuff that will go in the Vector:
TextField newElement;

C) Next, we will need to create both this button and the TextField, and add them to the Frame. This oughta be easy for you by now, so I won't spell it out. The only thing I ask is that your TextField's initial text should read something like this:
"type here to add to Vector"

D) Last, add yet another "else if" to your actionPerformed, like so:
else if (e.getSource() == newOne)
{
vNess.addElement(new String(newElement.getText() ) );
//take what is typed in TextField and make it a Vector element
}//add element to Vector

What this does is to take whatever you type into the new TextField, and add it as an element in your Vector!!

OK, re-compile, and run the program. Type some stuff into the new TextField, and use the "newOne" button to add that String to your Vector.

After you've played with that, use the "howMany" button to see if the size of your Vector has increased

Cheers,
Uncle Paulie

Tues Feb 24: arrays and vectors part three

OK, now for the new stuff: we are going to create and use a VECTOR (cue dramatic theme music!). A Vector is like an Array, only you can add elements to it while the program is running. You can also subtract elements, or change the value of elements, all while the program is running. It's like having an array with an elastic waistband: very stretchy, very useful. So let's add one, OK?

A) the first step is to add an import statement at the very top of your program; vectors come from quite a different country than arrays:
import java.util.*;

B) just below where we declared our int variable, declare your Vector:
Vector vNess;

D) just below declared the "show" button, declare another button:
Button howMany; //a button to see how big our Vector is

E) in the constructor, just below where we created that TextField, create both the button and the Vector, like so:
howMany = new Button("how Many?");
howMany.addActionListener(this);
Area52.add( howMany);

vNess = new Vector(3);

F) Lastly, add another "else if" to our actionPerformed, like so:
else if (e.getSource() == howMany)
{
System.out.println("Vector vNess has this capacity " + vNess.capacity() );
System.out.println("Vector vNess is this big " + vNess.size() );
}//size and capacity of vector?


Re-compile the program, and run it to see if your new button works. In the next segment, I'll explain the difference between "size" and "capacity" for those of you who are struggling to get a grasp on that.

Cheers,
Uncle Paulie

Tues Feb 24: arrays and vectors, part two

OK, so now we've got "back in the saddle" with Arrays. Now it's time to combine what we just (re)learned with something we looked at just before the vacation: getting a number from a TextField, and making use of it.

You may recall that there is a method called "Integer.parseInt( )" which allows you to take what looks like an integer from a TextField, and make it a real honest to GOD int. Kind of like changing Pinocchio from a puppet to a real boy. What we will do here with our program is to create a TextField, get an int value from it, assign that value to an int variable, and then use that variable to choose an element from our array. Here we go:

A) just below where you set up your Array, declare a TextField and an int variable, like so:
TextField whichOne; int whichQuote;

B) inside the constructor, just below the button we just created, create the TextField, like so:
whichOne = new TextField("type in an integer from 0 to 2"); Area52.add(whichOne);

C) in the "else if" for your "show" button, add these lines of code:
//get number from TextField and change it to an Integer whichQuote = Integer.parseInt(whichOne.getText() ); //pick out that element from array & print it to System console System.out.println(quotes[whichQuote]);

Recompile, then run the program. See if you can choose which element of the array will print out to the System console!

Cheers,
Uncle Paulie

Tues Feb 24: arrays and vectors, part one

In today's lesson, we are going to work with our old friend the Array --yup, they got 'em in Java too!-- and his close personal friend, the Vector. But before we go any further, let's get you a copy of the starting program we will use to explore this new concept; go to this folder and download the file inside:
http://www.box.net/shared/knh8a5evxt

OK, you got it? Good; let's get going!

If you recall, we met the Array way back in the fall. An Array is just a collection of variables that are grouped together. In most languages, all the elements of the Array are the same type. To make it simpler, we will be using a String Array, plus a TextField, plus a Button for retrieving elements of that array and putting them into the TextField. Here's how we do it:



A) First off, let's add an Array, and put some elements in it (do it under the place where declared the "exit" button):

String quotes[] = {"hello","hola","bonjour"};



B) next, let's declare a button just below that Array, so we can stuff with our new Array:

Button show;//a button for playing with arrays



C) The next step is to add that button to your frame, and give it an ActionListener. You can see how to do that from the button that's already in the program; so just add this new Button named "show" in the same way and inside the constructor.

D) Lastly, you'll need to say what that new button will do with your Array. You will need to add an "else if" statement below the first "if". Here is what I made my button do:

else if (e.getSource() == show)
{
System.out.println(quotes[0]);
}// show the quote

Essentially, I took the zero element in the array (that would be the very first element, kimo sabe) and printed it to the System console.

OK, see if you can make that happen!

Uncle Paulie

Tues Feb 24: Panels and layouts, part four, do something unique

OK chilluns, here's the part where I ask you to show me what you can do. Combine your new found knowledge of Panels and layouts with stuff you already knew before.

When you're done:

A) email to me your completed program, with your name and "Panels and Layouts" in the subject line
(if you didnt already do that yesterday)

B) make a discussion post on our Google group detailing what you did. Remember to also label this with your name and the phrase "Panels and Layouts"


"vegetarians...if you cook 'em right, they're delicious!"

Uncle Paulie

Mon Feb 23: Panels and layouts, part three

By now, we've established two basic propositions:
1) Panels are containers for holding other components
2) there are layouts besides the old "flow" layout

So let's explore these ideas a little longer. Another type of layout is called the "border": you have the four points of the compass -- north south east west -- as well as a region called the center. Let's add a second Panel to our frame, give it a border layout, then add some buttons to that panel to see what a border layout looks like

Here's how you do it:

A) declare a second Panel -- just copy it the way you already did it, and give it a different name

B) declare some buttons, like so:
Button uno, dos, tres, quattro;

C) just below the line that dded the first Panel to the frame:
onePanel.add(myFirstPanel); //add panel to frame

Add a second Panel, pretty much the same way you added the first one, only DONT use a grid layout, use a border layout, like so:
panelTwo.setLayout(new BorderLayout());

assuming that you named your Panel "panelTwo"; if you called it something else, then use YOUR name, m'OK?

D) add the first button to the "north" part of your border, like so:
uno = new Button("one"); panelTwo.add("North", uno);

and add one of the other buttons to the south border in a similar fashion; c'mon, its not THAT hard!

re-compile, and see what you get. If it works, then add buttons to the other parts of your border layout.

Cheers,
Uncle Paulie

Mon Feb 23: Panels and layouts, part two

Alrighty then, let's add the rest of those TextFields to your Panel:

right below these lines:

one = new TextField("hello");
myFirstPanel.add(one);

Add five more sets of lines just like those two which will add the other five TextFields to your Panel.

Re-compile, then run the program and see what you get!

Cheers,
Uncle Paulie

Mon Feb 23: Panels and layouts, part one

OK, to begin with, lets set up a new program that is very basic: one frame, one button, and the button can only make the program close. I have treeware handouts; aditionally, you can download this "startup" program at this address:

http://www.box.net/shared/iia5r6npe3

there's only one file in this folder and its called "FebPanel.java". Download it to your Java folder now, then save it, compile it and run it, just to make sure we got the easy stuff done.

You ready? OK, let's go then:

the first thing you will want to do is declare your Panel, like so:
Panel myFirstPanel;

I would put that line right below where you declared your Button

just below that, you will declare six TextFields, like so:
TextField one, two, three, four, five, six;

OK, now its time to actually create your Panel, add one TextField to it, and then ADD THE PANEL TO THE FRAME. Here's how it gets done:

A) just below this line:
onePanel.add(exit);

add this block of code:
myFirstPanel = new Panel(); //create the panel
//set a background color for the Panel
myFirstPanel.setBackground(Color.yellow);
//set different layout for Panel
myFirstPanel.setLayout(new GridLayout(2,3));

This creates a new Panel and gives it a yellow back color. MOST IMPORTANTLY, it gives this Panel a different layout than the rest of your frame. A grid layout lines stuff up using a well, a GRID. In this case, you will eventually end up with two rows of three TextFields each.

B) For now, lets just add one TextField, right below the code block you just added:

one = new TextField("hello"); //make a new textField
myFirstPanel.add(one); //add that TextField to Panel

C) Finally , add the Panel to the Frame by adding this line:
onePanel.add(myFirstPanel); //add panel to frame

just above this line:
onePanel.show();

OK, re-compile, then run the program, and see what you get!

Thursday, February 12, 2009

Thursday FEB 12: exploration time

OK, so now we've reached the last part before we bid each other bon voyage into the vacation. Here is all I want you to be able to do:

A) apply some other techniques I've shown you from a previous class to the lessons learned yesterday and today

B) send those program(s) to me by email, with the Subject line being your name and "last programs before the Feb break"

C) post a discussion to our Google group discussing what you did today beyond what I showed you.

Cheers,
Uncle Paulie

Wednesday, February 11, 2009

thurs feb 12, part four: actually transferring data

right below this line of code in your actionPerformed:

else if (e.getSource() == passItOn)
{




add this code block:

wide= Integer.parseInt(across.getText()); //get the text convert it to an int
tall= Integer.parseInt(up.getText()); //get the text convert it to an int
one.setSize(wide,tall);

Tuesday, February 10, 2009

wed Feb 11, part three: transferring data

OK, now its time to get serious: you are going to declare and add two TextFields, declare and a add another button, and declare two integer variables. In the next poisting, I'll show you how to use these tools to do stuff. Here's what you do first:


A) right below this line of code:
Button exit;


add this block of code:
Button passItOn;//declare another button
int wide, tall; //declare two variables
TextField across, up; //declare two TextFields



B) in your constructor, right below the three lines where added the exit button to your frame, you are going to add the three lines to add the "PassItOn" button

C) just below that, add the two lines for each Textfield (they wont need an actionListener)

D) just below that, set both int variables to 0 by typing in this:

wide=0;
tall=0;


E) add an "else if" to your actionPerformed right below the first if. This else if should make the variables wide and tall print to the black screen. See if you can finagle that on your own first.


Cheers,
Uncle Paulie

Wed Feb 11, part two: a button that closes the program!

OK, now you've got an application with a frame that cannot close itself and doesnt do anything EVEN THOUGH it has an "actionPerformed" statement. Let's fix that, and give it a button that can shut down the program in a simple way. Here's what you need to do:


A) just below the line where you declared a Frame:
Frame one;


add this line to declare a button
Button exit;


B) add the button to the frame. You should know the three important lines of code used to add a button to a frame by now; just make you type them in directly below this line:

one.setLayout(new FlowLayout());

C) Tell this button how to close the whole program by adding this code block to the actionPerformed method:

if (e.getSource() == exit)
{//add
System.exit(0); //thats a zero in there, not an O
}// All over, let's quit




Re-compile your program then run it to see if the button shuts it all down!



Cheers,
Uncle Paulie

Wed Feb 11, part one: a whole new world, a whole new program!

OK, before we begin, lets start with making a fresh program. It will be an application GUI, so the bare bones of it will look like this (I will have treeware copies for those who need one):



import java.awt.*;
import java.awt.event.*;

public class FebGui implements ActionListener
{
Frame one;
FebGui( )
{
one = new Frame();
one.setBounds(120,149,400,450);
one.setTitle("Wowsers");
one.setLayout(new FlowLayout());
one.show( );
}//end constructor

public void actionPerformed(ActionEvent e)
{
System.out.println("java lava!");
}


static public void main(String[] args)
{
new FebGui(); //make a new "FebGui" object
}//end main

} //end class

Monday, February 9, 2009

tues Feb 10: now show me what you can do! (part 4)

OK, now y'all have some "free exploration" time. Do something with our current program that is based on what we've learned here in the past, but haven't done with this particular program. When you are done, do these two things:



A) email this program, with your name and "show and hide frames" in the subject line



B) make a discussion post in our Google group, labeled with your name and the phrase "now for something completely different"



Cheers,

Uncle Paulie

Friday, February 6, 2009

Tues feb 10, show and hide frames part three

OK, now to make your second button do something different from the first button: we will make it hide the second frame! What you have to do is this:


A) add an if statement to your actionPerformed method, so that the program can can distinguish between the two buttons


B) make it so that the second button can can HIDE the second frame


Here's all you need to do to make that happen:


REPLACE EVERYTHING IN YOUR ACTION PERFORMED METHOD WITH THIS CODE BLOCK:


if (e.getSource( )== btn )
{
sayWhat = typeText.getText( );//get the text in textfield
System.out.println(sayWhat); //pass it to console
System.out.println("JAVA IS WAY BETTER THAN VB!");
}

else if (e.getSource( )== peek )
{
wazzup.hide( ); //hide the second frame
}//end if statement





th-th-thats all folks!


Uncle Paulie

Thursday, February 5, 2009

Tues feb 10: show and hide frames, part two

OK, now that you have two frames, how's about we do something with them? For starters, lets make it possible for a button on one frame to make another frame hide itself. Here is how you do it:






A) Declare a second button by adding this line
Button peek;


right below this line


Button b;






B) Add this second button to your first frame by adding this block of code into your constructor:
peek = new Button("PEEKABOO!"); //declare button
peek.addActionListener(this); //give it some action
f1.add(peek); //add to frame






C) SAVE, COMPILE THEN RUN THE PROGRAM. Both buttons should do the same thing; we will fix that in the final blog post

Cheers,
Uncle Paulie



Tues feb 10: show and hide frames, part one

As I mentioned in another class, it is more than possible to have many more than just one frame. Today, we are going to prove the point. Here is how you do it:


A) create another frame just like your first frame, only give it different location and size coordinates. Here are the steps to do that:

1) declare a second frame by changing this line
Frame f1;
to look like this
Frame f1, wazzup;
notice that all you did was another name followed by a comma!

2) give it a


B) save compile and run the program.


Voila! Now you have a multiple frame program!


Cheers,
Uncle Paulie

Friday Feb 6: step one, finish up from yesterday!

Hello one and all,
Our first order of business is to get back on track from yesterday. I am on hand to figure out all the stuff that ain't a-workin', so that you can send them all to me by email. I also have a printout of my working copy of this program.

simple, no?

Cheers,
Uncle Paulie

Wednesday, February 4, 2009

Thurs Feb 4, Free Exploration time

OK, now we will try this once again: I want you to apply what you have learned today, and use it with other stuff I've already taught you to create something new. Whatever you create, please be sure to do the following:

A) upload all your files to me by email

B) post a discussion in our Google group, even if you didn't quite get it to work

Cheers,
Uncle Paulie

Thurs Feb 5, Part Three: TextField Printing

OK, so now you've got a TextField, and you're wondering what you can do with it. Well here's one thing: you can use it to determine what gets printed to the system console. All you need is a variable and a way of getting the text from your TextField. Here is how its done:

A) just above your constructor, add a String variable, like so:

String sayWhat;

B) just before f1.show( ), add this line to make this variable empty:
sayWhat=""; //empty string

C ) Inside your actionPerformed statement, add these two lines:

sayWhat = typeText.getText( );//get the text in textfield
System.out.println(sayWhat); //pass it to console

this takes whatever you've typed into the TextField, and then passes that stuff to your String variable. This String is then sent to your system console

voila!
Uncle Paulie

Thurs Feb 5,Part Two: TextFields!

OK, for our next trick, we are going to create a place where you can make words appear in your program,other than in the system console. That place is called a TextField. Here is how you add one to your program:

A) add this line of code just above your constructor -- you know, that thing that's named the same as your file, but has the ( ) after it

TextField typeText;

B) go just above "f1.show( )" and add these two lines of code to place the TextField into your program:

typeText = new TextField("hello there");
f1.add(typeText);

Run your program,and make sure that you can get a TextField to show up in your program

Cheers,
Uncle Paulie

Thurs Feb 5,Part One: Console Printing

Greetings,caffeinated codewarriors!

When last we met, you were engaged in some exploration time,attempting to extend the usefulness of your first GUI application.There were varying degrees of success, so to further help this process, I have included several different techniques that you will use.Here is the first:

Open up the GUI application we made yesterday -- the one with the main method. Inside your "actionPerformed" statement,add this line:

System.out.println("JAVA IS WAY BETTER THAN VB!");

What this should do is to have this line of text print to your system console every time you click the button in your program

Try it, and then when you're ready, add other lines of text to this message by using the newline character "\n" to add new lines

more is on the way,

Uncle Paulie

Monday, February 2, 2009

Tues Feb 3: let's see what you can do! # 3

OK, I've shown you how to make a basic JAVA gui application; now let's see what you can do with it. Combine any of the lessons I've shown you until now to make a JAVA application which DOES something when you press the button. I'm giving you the whole rest of the period to get it done, and of course you can colloborate with each other.



When you have something, please demonstrate it for me, then make sure to send me all your files. Also,


MAKE A DISCUSSION POST STATING WHAT YOU DID WITH YOUR PROGRAM



Please make the subject header your name, plus the phrase "my program can do this". Write in the body a one or two sentence description of what your program does.



Cheers,

Uncle Paulie

Tues Feb 3: first GUI application with a button! #2

Just like we did yesterday, we are going to add a button to our program. It seems simple enough that all we would need to do is to add this line of code


b = new Button("Zoinks!"); //declare button

f1.add(b); //add button to frame


just above this line of code


f1.show( );



but unfortunately its not that simple. To put any sort of compoent on an application GUI, you have to use a LAYOUT MANAGER. The simplest kind is a "Flow Layout"; this is what you would type:



f1.setLayout(new FlowLayout());



and you should write inside the constructor area, preferably ABOVE where you typed in the code for adding a Button



OK, let's put all this together, and see what we get!


cheers,

Uncle Paulie

Tues Feb 3: first GUI application! #1

OK Java Koderz, as promised, today we will be working with a GUI application. This is a program which doesn't need a webpage to run inside; besides that there are several other differences:


A) An applet just sort of EXISTS; to have an application, you need a "Frame". You declare a frame like this:


Frame f1;


B) applets have init methods; applications have constructors. A constructor is easy to write: you just take your class name and stick a ( ) at the end of it. Throw in an opening and closing curly brace { and }, and you've got a constructor. It looks something like this:


FirstFrame( )
{


}


C) An applet made in textPad automatically sizes itself; in an application, you need to declare the size AND the location, kind of like this:


f1.setSize(400,450);
f1.setLocation(120,149);

btw, the two lines of code above would go inside the constructor!

D) an applet just shows up when you view the webpage; for an application to be visible, you have to SHOW it. You can declare a frame and size it, but it still won't show until you add this line to your constructor:
f1.show( );

E) all applications have main methods, and this one is no exception. The main method here will do only one thing: make a new object from the class you've declared and constructed


Fear not: I will be giving you a complete code sheet, so you won't have to figure out where everything goes. Let's do this one, then move on to code where our application can actually DO something


cheers,
Uncle Paulie

Sunday, February 1, 2009

Mon Feb 2, part four: one button, many random colors!

OK, here's a final lesson of the day: how do you make the button display colors other than black? Simple: use "rgb" values, three random number generators that create values from 0 to 255, and a little thing called "typecasting". Plus, we'll make use of the "new" keyword, and the Java Color class. Allow me to explain them all first, then I'll show you how to mod your program using these new techniques:

A) rgb -- this is a way of combining values for red, green and blue to make any of about 16,000,000 colors. Each value has a range from 0 to 255

B) random numbers -- making random numbers is pretty simple in Java, and pretty close to how we did it in VB; we just invoke a method called Math.random(). We also have to have a way of chopping off all that useless stuff to the right of the decimal, and an easy way of doing that is to pass it to the Java Math.round() method. Also, we would want to multiply the result by whatever value we want to set as our highest possible number, in this case, 255.

C) typecasting -- in Java, you can convert different variable types into other compatible type by using typcasting. All you do is put the varibale type inside parantheses, like so: (int). We need to do that in our current program because the random() method doesn't create intgers, but that's what we need.

OK, so now let's mod your app, OK? Here are the steps to do it:



0) add this import statement at the top of your code:

import java.awt.Color; //allows use of rgb Color values

1) in your actionPerformed method, add these three lines:


int r = (int)Math.round(Math.random()*255);
int g = (int)Math.round(Math.random()*255);
int b = (int)Math.round(Math.random()*255);



they create three random integer values between 0 and 255 every time the button is pushed





2) In your "setBackground" method, replace everything inside the parentheses with this line of code:


new Color (r,g,b)


Essentially, you are passing those int values to a new Color object. That Color object is then passed to the setBackground method, which uses it to change the background color





Cheers,


Uncle Paulie

Monday Feb 2, part three: an applet gui with a button that does something!

OK, so now maybe you're thinking: "this is cool, but how can I make my button DO something?" Well I'm glad you asked, because this is how you do it, and it all has to do with a little item called an "action listener":



A) first, you have to add another import statement at the very top of your program; it looks like this:

import java.awt.event.*; // so you can make stuff happen



B) right after the public class declaration, where you see "extends Applet", you have to add this statement:

implements ActionListener

this allows your program to make use of the event library



C) to make your button able to performk an action, you have to give it access to an actionlistener. Without it, its kind of like having your cellphone on silent: you never hear it ring!

b.addActionListener(this);



D) Finally, you have to add an actionPerformed method. This is what dictates what happens when you click on that button:

public void actionPerformed(ActionEvent e)
{
setBackground(Color.black);
}




As you may have guessed, this button should now be able to make the background of the applet turn black. Don't worry, we will be upgrading this button's capabilities pretty soon!



cheers,

Uncle Paulie

Monday Feb 2, part two: an applet gui with a button!

OK, so now we're caught up to where just gotta be... let's get down and dirty and GUI.



To make a GUI (graphical user interface, in case you were wondering) in JAVA, you need to make an import statement that brings in the Java "awt" library. "Awt" stands for "abstract windowing toolkit", and it contains all the stuff you need in order to make Java programs that look like, well, like programs.



so here's the first step, adding the two import statements for this program:

import java.awt.*;

import java.applet.Applet;



next, lets name our public class just below those two imports, then add the curly braces:



public class OneButton extends Applet

{



}// end class

next, get in between those curly braces, and add an init method. You need to use an init method in a JAVA applet to setup stuff that the rest of the program will use. Yours will add a button to your GUI:


public void init( )
{
Button b = new Button("OK");
add(b);
}



OK, now you need to save the program -- remember, just copy the class name and use that for the file name. Then, compile it, and run it -- remember, it's an applet, so use Ctrl + 3


cheers,
Uncle Paulie


Monday Feb. 2, Part the First: finish up from last Friday!

greetings JAVAnoids!



Our first order of business is to two-fold:


A) finish up the program we were working on last Friday


B) email to me as attachments ALL the programs you did last Friday


Once that's done with, we will commence working on interfaces. Yep, you got it, it's time we got GUI! We will look at both the applet way of making a GUI -- since you've already got a little experience making applets now -- AND the application/main method way of doing it, which is a little bit different.



cheers,
Uncle Paulie