java 2nd stage

Discussion in 'Programming General' started by paladen, Jun 11, 2007.

java 2nd stage
  1. Unread #1 - Jun 11, 2007 at 12:52 AM
  2. paladen
    Joined:
    Jan 21, 2007
    Posts:
    1,633
    Referrals:
    1
    Sythe Gold:
    0

    paladen Guru
    Banned

    java 2nd stage

    reposted

    In this tut, I will teach you how to use private statements, more properly called instance variables, along with changing colors from black and white.

    What is the use of Instance Variables and color? Well, instance variables can be used throughout the program. It makes coding a heck of a lot easier if you're going to use the same code sequence more than once. Color is also a useful tool because, let's face it, RuneScape would be really boring in black and white.

    As always, let's start out with a program. This is the same program as my first tutorial, only now we're using instance variables.


    import java.awt.*;
    import objectdraw.*;

    public class touchthewindow extends WindowController{

    private Text touched;

    public void onMousePress (Location point){
    touched = new Text ("You touched me!", 20, 20, canvas);
    touched.setColor (Color.RED);

    }
    }In this code, you'll notice a few things that are different from the original. First, you'll notice the following:


    private Text touched;This tells the program that you want to make a variable named touched that is written out in text. The computer can't just read your mind every time you create a variable, it needs to know how the variable is to be made. This is why we need to tell it in the private statement that it is made with Text.

    Next you'll notice this:


    touched = new Text ("You touched me!", 20, 20, canvas);Is a bit different than last time's:


    new Text ("You touched me!", 20, 20, canvas);This is because you are telling the computer exactly what "touched" is. Once you tell the computer what it is, you can use it throughout the program without having to retype anything. It will save you a lot of time and effort.

    Finally, you'll notice this:


    touched.setColor (Color.RED);This piece of code tells the computer that first you want to set the color of "touched" to red. Then, you need to specify exactly what color to chang "touched" to. In the include that holds the names of the colors, all of the letters in the colors are caps. Make sure to remember that Java is case sensitive, and if you put "(color.red)", it wouldn't work and would give you an error.

    So now let's compare the old version of touchthewindow:


    import java.awt.*;
    import objectdraw.*;

    //This program will display text when you click on the canvas

    public class touchthewindow extends WindowController {

    public class onMouseClick (Location point){
    new Text ("You touched me!", 20, 20, canvas);

    }
    }To the new version:



    import java.awt.*;
    import objectdraw.*;

    public class touchthewindow extends WindowController{

    private Text touched;

    public void onMousePress (Location point){
    touched = new Text ("You touched me!", 20, 20, canvas);
    touched.setColor (Color.RED);

    }
    }As you can see, the new version looks a lot more professional, and is actually a lot easier to use. So, there you go. I hope you enjoy the guide.
     
  3. Unread #2 - Jun 11, 2007 at 9:02 AM
  4. WoW Sucks
    Joined:
    Jan 21, 2007
    Posts:
    3,708
    Referrals:
    3
    Sythe Gold:
    0

    WoW Sucks Global Moderator
    Banned

    java 2nd stage

    Put the code in code boxes.

    Code:
     codeHERE [ /code]
     
< Visual Basic .NET 2005 Complete Tutorial - WIP | begining gui programming with java >

Users viewing this thread
1 guest


 
 
Adblock breaks this site