Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with drawing a polygon correctly Posted by Methos93 on 26 Jan 2012 at 2:41 AM
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.



 

Recent Jobs