Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Very basic terms & structure help

    I use PHP, HTML & JavaScript to make games... well, all my newest ideas would be performed better if they had a way to load, store, and alter graphics on the fly. I looked into Flash, and besides it being expensive for the development software, I just could not make it work (a friend gave me an install awhile back, but if I could have made it work, I would have bought my own). So, I turned to Java.

    What I need is a VERY basic guide for explaining everything, or some help here for it.


    My questions so far are very basic;


    1.) I am used to FUNCTIONS - I can have as many functions per document as I want. When I try to compile hello world examples, they work only if the .java file is named the exact same as the "class" declaration inside it. Does this mean that I can only have one 'class' per file? And if so, how would that even work? Are classes not like functions?

    2.) What are all the (what seems to be) unnecessary keywords in classes and stuff? "public class SomeClass extends Applet" -- what is "public"? "extends"? "Applet"? Inside the class, "public void init()" -- what would the 'void' be for?

    3.) Is the first class (or if you can only have one per file, the only one) the one that is called when the applet loads...? I don't understand a class being called without me putting something like "$blah = SomeClass(para,meters);"

    4.) For the hello world example I have, this is used: "public void paint(Graphics g)" -- is "Graphics" a type or something? Is "paint" something that I can change, like a function within a function?



    To be honest, I feel like giving up and working out some sort of JS thing instead, since all of these things are stopping me from understanding what is going on and what I am doing enough to move on and learn more. I rarely can just accept that you need to do X for Y to work -- I need to know WHY you need to do X.

    I have never worked with any form of REAL programming language, so if these things are standard across programming languages, then I'm sorry for being naive. Maybe I am just too used to PHP and JS babying me.
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    The first thing I would recommend is to pick up a book on Java, as you will find that there is a lot more depth here. To answer your questions...

    1. I think you are making the wrong comparison for functions. Functions in other languages would more closely equate to "Methods" in Java. Classes are core to Object Oriented Programming and should define some sort of Object. Here is an example of a simple Java program that you might find in a textbook:

    PHP Code:
    public class Rectangle
    {
        private 
    int length;
        private 
    int width;

        public 
    Rectangle(int lengthint width)
        {
            
    this.length length;
            
    this.width width;
        }

        public 
    int getLength()
        {
            return 
    length;
        }

        public 
    int getWidth()
        {
            return 
    width;
        }

        public 
    int getArea()
        {
            return 
    length width;
        }

    And here is a Class that would use this Rectangle "Object" to create a rectangle and print its area

    PHP Code:
    public class MakeRectangle
    {
        public static 
    void main(String[] args)
        {
            
    Rectangle rect = new Rectangle(57);
            
    System.out.println(rect.getArea())
        }

    In this example, the first class represents a Rectangle Object, and it defines 2 variables that can be thought of as attributes of this object, the length and width. Then we have a constructor, which is what is used to create the object, defining the length and width. Following this, we have 3 methods(similar to functions as you are used to) that perform some type of action that you might do with a rectangle. In this case, I've given it the ability to tell you its length, its width, and its area, using 3 different methods.

    2. The keywords you see all have an important meaning:

    public/private/protected : This defines the types of classes that have access to this object. For example, private variables as in my above example, can only be accessed from within the same class in which they are defined, which is the purpose of the getLength and getWidth methods.

    extends : This is key for inheritance, which is one of the most important concepts in Object Oriented Programming, an example:
    In the above example, a Rectangle is a type of shape, so we could define a shape class, with some basic attributes that apply to ALL shapes, and create a Rectangle, Circle, etc.. class that "extends" the shape class, thus inheriting its attributes.

    void : This is used as a return type when a method does not return a value, it is the same as my example using public int getLength, where int is the return type.

    3. A class that has a public void main method is usually where an application starts, or an init method in the case of an applet. Java classes are called and used as in the above example by making a reference to them such as Rectangle rect = new Rectangle(3, 5), this creates a new rectangle, by passing 3 and 5 as arguments to the Rectangle constructor, and the variable rect is used as a reference to this new object.

    4. In your example, Graphics is another java class that defines functionality for graphics. The definition for the paint method is saying that the paint method accepts a Graphics object as an argument, so that you can perform the actions defined by it, from within your paint method.

  • Users who have thanked brad211987 for this post:

    Eternity Angel (03-20-2008)

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    I still don't get the classes then - I define a class, which I can only have one per file, then how would I use it in another class...? Do I need some sort of include() function in my main .class file?
    Bored? Might as well play Fantasian Kingdoms: http://www.fantasiankingdoms.com/

    Check out my JavaScript Image Project: http://codingforums.com/showthread.php?t=185231

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    You can have multiple classes in a file, but they are used exactly how I showed in my example. The example assumes that the .class files are in the same directory. If they are not in the same directory, you will need to look into import statements and placing the classes on the java classpath.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •