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
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    basic shapes in java

    I am beginning to try to expirement with making basic shapes in java. I looked at some resources and i found the graphics2D class. But i dont get it every tutorial ive looked at they have classes extending off of classes and i dont know what those are. I looked at the language documentation and i dont know all i need to set up to make shapes. I found 1 tutorial that had a lot of code and then i put in the shapes like a circle and stuff but i dont know what was set up before that that allowed me to just put in the coordinates and colors and stuff and then when i compiled it it made the shape i told it too. Can someone give me some tips for starting to learn how to make basic shapes and stuff in java or does anyone know of any good websites i can get help from. Thanks
    Last edited by cwl157; 01-04-2005 at 05:06 AM.

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I'd say to begin with, you have two different ways of making shapes (and gui's):

    1) AWT / applet
    2) Swing

    In java, there are thousands of pieces of code that provide functionality. If every one of those pieces of code had to be loaded everytime an application was run, the application would take forever to launch! To avoid this, these code chunks are placed in packages, and a java developer can then import only the packages containing the functionality she needs.

    I would recommend starting by creating a java applet, and using the graphics class. Here is a very basic example, documented. (I'm gonna type it up really quick...

    Note: the "coordinate" stuff is based on the "java coordinate system." It states that the upper-left corner of the display surface (applet, window, whatever) is coordinate 0,0 (x,y). everything to the right of that corner is an "X" and everything below the top line (0) is a "Y".... a graph:

    Code:
    <0,0> 
        *---------------------x----------> X axis
         |                    |
       y |--------------------* <x,y>
         |
         |
       y axis


    And now the code, 2 parts. The first is just the code (with minimal documentation), the 2nd is the code LADEN with documentation.


    Code:
    import java.applet.Applet;
    import java.awt.*;
    
    
    public class DrawTestNoDoc extends Applet {
    
        public void paint (Graphics page) {
        
            
            /* Color Setup */
            setBackground(Color.lightGray);   //sets the applet background to lightGray
            page.setColor(Color.blue);        //sets the default paint color to blue
           
            /* Draw Ovals */
            page.drawOval(20, 30, 20, 20);    //this draws a perfect circle
            page.fillOval(60, 30, 20, 20);    //this draws an opaque oval
             
            /* Switch Colors */
            Color myColor = new Color(255,200,0);     //create a new RGB-defined color.
            page.setColor(myColor);                   //set the foreground to the new color.
    
            /* Draw Rectangles */
            page.fillRect(10, 70, 30, 60);            //draw an opaque rectangle
            page.fill3DRect(50, 70, 30, 30, true);    //draw a 3d rectangle.
             
    
        } // method paint
    
    } // class DrawTest

    and the documented one:
    Code:
    /* Import the code to draw pictures & shapes */
    import java.awt.*;
    
    /* Import the code to display as an applet */
    import java.applet.Applet;
    
    
    
    /* 
     *  our main class. by 'extending' the 'Applet' package,
     *  we have access to all of the Applet's methods and
     *  functionality. Good Object-Oriented design means that you
     *  create "sub classes" of main classes. It helps when reading
     *  the code.
     */
    public class DrawTest extends Applet {
    	
    	
        /* 
         *  the main method for an applet, paint(). Here it accepts a single parameter,
         *  a "Graphics" object, called page. It could just as easily have
         *  accepted a "String" object, but we're painting here. So we need
         *  graphics functionality, not string functionality on the object.
         */
        public void paint (Graphics page) {
        
            
            /*
             *  The setBackground method accepts a value for a color.
             *  This value can either be one of the static (accessible & predefined)
             *  colors already set by the "Color" class (like "Color.white" or
             *  "Color.magenta",  or by defining a new "Color" object, using RGB
             *  values.
             */
            setBackground(Color.lightGray);  //sets the applet background to lightGray
            //setForeground(Color.white);
            
            
            
            
            /*
             *  The setColor() method of the "Graphics" class accepts a "Color" object
             *  as it's parameter. It will set the "painting" color.
             */
            page.setColor(Color.blue);
           
           
            /*
             *  the drawOval method takes 4 parameters:
             *    int x, int y, int width, int height
             *  where:
             *       x is the starting "X" coordinate, (how far to the right)
             *       y is the starting "Y" coordinate, (how far up & down)
             *       width is the width of the oval ~
             *       height is the height of the oval ~
             *
             *       ~not actually the width & height of the oval. there is an
             *        invisible rectangle in which the oval is drawn. these are
             *        technically the dimensions of the invisible rectangle
             */
            page.drawOval(20, 30, 20, 20);    //this draws a perfect circle
            
             
             
            /*
             *  the fillOval() method is the same as drawOval, except the shape
             *  is filled with the current "setColor"
             */
            page.fillOval(60, 30, 20, 20);    //this draws an opaque oval
             
             
             
            // Lets create a new, custom color, besides the predefined ones!
             
             
            /*
             *  The "Color" class can be used to create new color objects.
             *  When creating the object instance, we pass in the RGB value of the
             *  color we want to create. Here we create an "orange" color object.
             *    **Please note that we 'could' have used Color.orange (it's a predefined
             *     color), but for demo purposes, this works well.**
             */
            Color myColor = new Color(255,10,0);    //create a new RGB-defined color.
             
             
            /*
             *  Here we use the "setColor()" method to change the color we "paint" in
             */
            page.setColor(Color.orange);  //set the foreground color the color object we created
             
             
            /*
             *  Here we use the "fillRect()" Graphics method. It works similarly to the
             *  "fillOval()" method, and accepts 4 parameters:
             *       int x, int y, int width, int height
             *  where:
             *       x is the distance across the applet (the "X" coordinate)
             *       y is the distance "vertically" down the applet (the "Y" coordinate)
             *       width is the width of the rectangle
             *       height is the height of the rectangle
             */
             
            page.fillRect(10, 70, 30, 60);  //draw an opaque rectangle
    
    
    
            /*
             *  Call the "fill3DRect()" method, which creates an opaque, 3D rectangle.
             *  The first 4 params are the same as the "fillRect()" method, but adds a 5th
             *  parameter:        boolean raised
             *      if this is set to true, the object appears "raised." otherwise, it will
             *  appear to be "sunken" into the page.
             */
            page.fill3DRect (50, 70, 30, 30, true);   //draw a 3d rectangle.
             
             
        } // method paint
    
    } // class DrawTest

    I called it by creating an html page with the following:
    Code:
    <html>
    <body>
    <applet 
        code="DrawTest.class"
        height="400" width="300" 
        alt="Your browser is not displaying Java"
    >
    Your browser does not support Java</applet>
    </body>
    </html>
    and placed it in the same directory as my compiled DrawTest.class.



    Let me know how those files work for you.

    -Celt


    *note that I didn't use any non-standard libraries (code packages), just what comes bundled with the standard java sdk. *
    Last edited by Celtboy; 01-05-2005 at 04:08 AM.

  • #3
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow. Thanks so much thats exactly what i was looking for.

  • #4
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so now lets say instead of running this as an applet how would i run it as a program? Also how do i draw a triangle and lines in java? Thanks
    Last edited by cwl157; 01-07-2005 at 01:06 AM.


  •  

    Posting Permissions

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