Using Instance Variables and Colors in ObjectDraw

Discussion in 'Archives' started by Redemption, Feb 7, 2007.

Using Instance Variables and Colors in ObjectDraw
  1. Unread #1 - Feb 7, 2007 at 3:12 PM
  2. Redemption
    Joined:
    Jan 22, 2007
    Posts:
    429
    Referrals:
    0
    Sythe Gold:
    0

    Redemption Forum Addict
    Banned

    Using Instance Variables and Colors in ObjectDraw

    Chances are if you're reading this guide, you've read my first one, or alreaady know the basics of Java. If you're not this far, this will make no sense to you. If you already know the basics, this is the place for you.

    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.

    Code:
    import java.awt.*;
    import objectdraw.*;
    
    public class touchthewindow extends WindowController{
    
    private Text touched;
    
    public void onMousePress (Location point){
    touched = new Text ("Hello World!", 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:

    Code:
    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:

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

    Code:
    new Text ("Hello World!", 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:

    Code:
    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:

    Code:
    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 ("Hello World!", 20, 20, canvas); 
    
    } 
    }
    To the new version:

    Code:
    import java.awt.*;
    import objectdraw.*;
    
    public class touchthewindow extends WindowController{
    
    private Text touched;
    
    public void onMousePress (Location point){
    touched = new Text ("Hello World!", 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.

    THIS GUIDE WAS WRITTEN AND SHOWN TO THE PUBLIC BY REDEMPTION, AND IT IS HIS PROPERTY. YOU MAY NOT COPY OR REPRODUCE ANY OF THE GUIDE WITHOUT FIRST ASKING THE AUTHOR OR GIVING CREDIT TO THE AUTHOR.
     
  3. Unread #2 - Mar 18, 2007 at 2:36 PM
  4. Brandeis
    Joined:
    Mar 9, 2007
    Posts:
    1,154
    Referrals:
    0
    Sythe Gold:
    0

    Brandeis AKA WoW Blows
    Banned

    Using Instance Variables and Colors in ObjectDraw

    Gj Redemption, I used this earlier last week.
     
  5. Unread #3 - May 1, 2007 at 10:00 AM
  6. -------owned-------
    Joined:
    Jan 27, 2007
    Posts:
    1,225
    Referrals:
    0
    Sythe Gold:
    0

    -------owned------- Guru
    Banned

    Using Instance Variables and Colors in ObjectDraw

    Nice guide :D
    * Off the record * Finally everyone figured out about redemption (...)
     
< editing posts has been kinda slow | Perl Education >

Users viewing this thread
1 guest


 
 
Adblock breaks this site