Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

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

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.