Redemption's Java Tut #2: Using Instance Variables and Colors (ObjectDraw)

Discussion in 'Programming General' started by Redemption, Feb 3, 2007.

Redemption's Java Tut #2: Using Instance Variables and Colors (ObjectDraw)
  1. Unread #1 - Feb 3, 2007 at 6:40 PM
  2. Redemption
    Joined:
    Jan 22, 2007
    Posts:
    429
    Referrals:
    0
    Sythe Gold:
    0

    Redemption Forum Addict
    Banned

    Redemption's Java Tut #2: Using Instance Variables and Colors (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.
     
< Opening links in a custom webbrowser | Custom Drawn Controls >

Users viewing this thread
1 guest


 
 
Adblock breaks this site