Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Drawing Polygrams

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Drawing Polygrams

    Hey, everyone. I'm having trouble with a class I'm making called Turtle. I want it so that you can choose to draw a rectangle or a "regular shape". A "regular shape" is any shape that has every side as the same length (the number of sides is decided by the user). The rectangle method works just fine but I can't seem to get the angles right on the regular shape method so my shape always comes out wrong. Here is the code.



    import java.util.Scanner;
    public class Turtle extends SimpleTurtle
    {
     
      public Turtle (int x, int y, Picture picture) 
      {
     
        super(x,y,picture);
      }
     
     
      public Turtle (int x, int y, ModelDisplay modelDisplayer) 
      {
     
        super(x,y,modelDisplayer);
      }
     
     
      public Turtle (ModelDisplay modelDisplay) 
      {
     
        super(modelDisplay);
      }
     
      public Turtle (Picture p)
      {
     
        super(p);
      }
      public void drawRectangle(int rectwidth, int rectlength)
      {
        /*Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the length.");
        rectlength = keyboard.nextInt();
        System.out.println("Enter the width.");
        rectwidth = keyboard.nextInt();*/
        int value1 = 2;
        this.turnRight();
        this.forward(rectwidth);
        this.turnRight();
        this.forward(rectlength);
        this.turnRight();
        this.forward(rectwidth);
        this.turnRight();
        this.forward(rectlength);
     
      }
       public void drawRegular(int numside, int lengthside)
      {
         int angle = (((numside - 2) * 180) / (numside));
         this.turn(angle);
         for (int i=0;i<numside;i++){
     
     
        this.forward(lengthside);}
         this.turn(angle);
       }
      public static void main(String[] args)
      {
        World world1=new World();
        Turtle turtle1=new Turtle(world1);
        Scanner keyboard = new Scanner(System.in);
     
        int choice= 3;
        System.out.println("Input '1' for a rectangular figure or '2' for a regular figure.");
     
     
     
        for (int i=1;i<9999;i++) {
          if (choice == 1)
          {
            int rectwidth=0;
          int rectlength=0;
     
            System.out.println("Enter the width of the rectangle:");
            rectwidth = keyboard.nextInt();
            System.out.println("Enter the length of the rectangle:");
            rectlength = keyboard.nextInt();
            if ((rectwidth < 1 || rectlength < 1))
            {
             System.out.println("Incorrect input. All values of width and length must be nonzero positive numbers."); 
            }
            else{
              turtle1.drawRectangle(rectwidth, rectlength);}
          }
          else if(choice == 2)
          {
            int numside=0;
          int lengthside=0;
            System.out.println("Input the number of sides");
            numside = keyboard.nextInt();
            System.out.println("Input the length of a side");
            lengthside = keyboard.nextInt();
            if ((numside < 1 || lengthside < 1))
            {
             System.out.println("Incorrect input. All values of sides and length must be nonzero positive numbers."); 
            }
            else{
              turtle1.drawRegular(numside, lengthside);}
     
          }
          else
          {
            System.out.println("Input '1' for a rectangular figure or '2' for a regular figure.");
            choice = keyboard.nextInt();
          }
        }
     
      }
    }

    Help would be appreciated.
    Last edited by Methos; January 26th, 2012 at 05:25 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing Polygrams

    I can't seem to get the angles right on the regular shape method
    Can you explain your algorithm for computing the values needed to draw the shapes?

    Without the SimpleTurtle class definition this code can not be compiled

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing Polygrams

    Quote Originally Posted by Norm View Post
    Can you explain your algorithm for computing the values needed to draw the shapes?

    Without the SimpleTurtle class definition this code can not be compiled
    Sure. For the rectangle, I have the turtle turn 90 degrees and go straight for the length of the width of the rectangle. Then, it repeats this process for the length, then the width again, and finally the length once more. For the regular shape, the user enters the number of sides and the length of each side and the turtle travels moves the direction its supposed to. It just doesn't turn the correct amount for the angle. I need the turtle to turn at the same angle of the shape. If you want to compile it yourself, I have attached a zip that has the SimpleTurlte class inside of it.

    The way I am computing the size of the angle is with this formula:
    [(number of sides -2) * 180] / (number of sides)
    Attached Files Attached Files

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing Polygrams

    For the regular shape, the user enters the number of sides and the length of each side and the turtle travels moves the direction its supposed to. It just doesn't turn the correct amount for the angle
    What angle does it turn? How does that compare to the correct amount to turn?

    Way too many classes in the zip file. Can you reduce it to one file that shows the problem?
    Last edited by Norm; January 26th, 2012 at 07:24 PM.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing Polygrams

    Quote Originally Posted by Norm View Post
    What angle does it turn? How does that compare to the correct amount to turn?

    Way too many classes in the zip file. Can you reduce it to one file that shows the problem?
    My bad. Here is a zip file with just SimpleTurtle. That's my problem. I don't know how it should turn exactly. I have no idea how to go about doing that.
    Attached Files Attached Files
    Last edited by Methos; January 26th, 2012 at 07:32 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing Polygrams

    The code works for 4 sided shapes (90deg turns)
    But doesn't work for different shapes with different angles.

    Given an angle and an x,y location, can you compute a future x,y location for a movement?

    Since the pixels are rectangular the movement must be either up-down or left-right.
    Using some trigonometric function and given a current x,y location and an angle you should be able to compute a future x,y location. The problem will be getting one with whole int values.

Similar Threads

  1. Drawing 3D images
    By SrideviSridhar in forum Java Theory & Questions
    Replies: 2
    Last Post: December 30th, 2011, 01:49 AM
  2. drawing is in the same postion :(
    By kisokiso in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 20th, 2011, 08:02 AM
  3. Drawing a pentagon
    By Zomosa in forum AWT / Java Swing
    Replies: 7
    Last Post: October 11th, 2011, 10:21 PM
  4. [SOLVED] Drawing on JFrame
    By kbarrett1989 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 31st, 2010, 03:41 AM
  5. Drawing
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 02:43 PM