Guest User

java project

a guest
Nov 20th, 2020
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4.  
  5. class Polygon
  6. {
  7.   double x[], //contains x coordinate for vertices
  8.          y[], //contains y coordinate for vertices
  9.          xc,  //(xc,yc) = figure's center or
  10.          yc;  //reference point
  11.      
  12.   int    totalPoints; //number of vertices
  13.  
  14.   //Given method: Explain function
  15.   Polygon()
  16.   {
  17.   }
  18.      
  19.   Polygon(int totalPoints, double xc, double yc)
  20.   {
  21.     this.totalPoints = totalPoints;
  22.     x = new double[totalPoints];
  23.     y = new double[totalPoints];
  24.     this.xc = xc;
  25.     this.yc = yc;
  26.   }
  27.  
  28.   void setCenterCoords(double xc, double yc)
  29.   {
  30.     this.xc = xc;
  31.     this.yc = yc;
  32.   }
  33.  
  34.   void setCoordForOneVertex(int index, double x, double y)
  35.   {
  36.     if ((index < totalPoints)&& (index >= 0))
  37.     {
  38.       this.x[index] = x;
  39.       this.y[index] = y;
  40.     }
  41.   }
  42.  
  43.   void grow(double factor)
  44.   {
  45.     int i;
  46.    
  47.     for (i = 0; i < totalPoints; i++)
  48.     {
  49.       x[i] = (x[i] - xc) * factor + xc;
  50.       y[i] = (y[i] - yc) * factor + yc;
  51.     }
  52.   }
  53.  
  54.   //New method: TO DO
  55.   double getPerimeter()
  56.   {
  57.     double perimeter = 0;
  58.     {
  59.       //requires the code to calculate the perimeter
  60.     }
  61.     return (perimeter);
  62.   }
  63.  
  64.   void moveDown(double displacement)
  65.   {
  66.     for (int i = 0; i < totalPoints; i++)
  67.     {
  68.       y[i] = y[i] + displacement;
  69.     }
  70.     yc = yc + displacement;
  71.   }
  72.  
  73.   void draw(Graphics g)
  74.   {
  75.     int i;
  76.     for (i = 0; i < totalPoints - 1; i++)
  77.     {
  78.       g.drawLine((int)x[i],(int)y[i],(int)x[i+1],(int)y[i+1]);
  79.     }
  80.     g.drawLine((int)x[0],(int)y[0],(int)x[totalPoints - 1],(int)y[totalPoints - 1]);
  81.   }
  82. }
  83.  
  84. class Triangle extends Polygon
  85. {
  86.   Triangle(double x1, double y1, double x2, double y2,
  87.            double x3, double y3, double xc, double yc)
  88.   {
  89.     super(3,xc,yc);
  90.     setCoordForOneVertex(0, x1, y1);
  91.     setCoordForOneVertex(1, x2, y2);
  92.     setCoordForOneVertex(2, x3, y3);
  93.   }
  94.  
  95.   //New method: TO DO
  96.   double getArea()
  97.   {
  98.     double area = 0.0;
  99.     {
  100.       //requires code to calculate area
  101.     }
  102.     return (area);
  103.   }
  104.  
  105.   //New method: TO DO
  106.   void displayStringInside(String theString)
  107.   {
  108.     //requires code to display theString centralized
  109.     //within the figure
  110.   }
  111. }
  112.  
  113. class Rectangle extends Polygon
  114. {
  115.   //New: Given constructor
  116.   Rectangle ()
  117.   {
  118.     super(4,0.0,0.0);
  119.   }
  120.  
  121.   Rectangle (double x1, double y1, double x2, double y2)
  122.   {
  123.     super(4,0.0,0.0);
  124.     double midX,
  125.            midY;
  126.            
  127.     midX = (x1 + x2)/2.0;
  128.     midY = (y1 + y2)/2.0;
  129.     setCenterCoords(midX,midY);
  130.     setCoordForOneVertex(0, x1, y1);
  131.     setCoordForOneVertex(1, x2, y1);
  132.     setCoordForOneVertex(2, x2, y2);        
  133.     setCoordForOneVertex(3, x1, y2);    
  134.   }
  135.  
  136.   //New method: TO DO
  137.   double getArea()
  138.   {
  139.     double area = 0.0;
  140.     {
  141.       //requires code to calculate area
  142.     }
  143.     return (area);
  144.   }
  145.  
  146.   //New method: TO DO
  147.   void displayStringInside(String theString)
  148.   {
  149.     //requires code to display theString centralized
  150.     //within the figure
  151.   }
  152. }
  153.  
  154. //*** New class: Given and to be Explained
  155. class Cuadrado extends Rectangle
  156. {
  157.   Cuadrado (double x1, double y1, double width)
  158.   {
  159.     super(x1,y1,x1+width,y1+width);
  160.   }
  161. }
  162.  
  163. public class HerenciaGeometria2 extends java.applet.Applet
  164. {
  165.   Polygon poly1;
  166.   Triangle triangle1;
  167.   Rectangle rectangle1;
  168.   Graphics g;
  169.    
  170.   void delayMiliSecs(int delay)
  171.   {
  172.     try
  173.      {
  174.        Thread.sleep(delay);
  175.      }
  176.      catch(InterruptedException e)
  177.      {
  178.      }
  179.   }
  180.  
  181.   public void init()
  182.   {
  183.    
  184.     triangle1 = new Triangle(10,10,30,50,40,20,25,30);
  185.     rectangle1 = new Rectangle(80,80,140,100);
  186.    
  187.     poly1 = new Polygon(5,40,25);
  188.     poly1.setCoordForOneVertex(0,20,40);
  189.     poly1.setCoordForOneVertex(1,60,40);
  190.     poly1.setCoordForOneVertex(2,60,10);
  191.     poly1.setCoordForOneVertex(3,20,10);  
  192.     poly1.setCoordForOneVertex(4,30,30);  
  193.     poly1.moveDown(100);
  194.                
  195.     setBackground(Color.red);
  196.     setForeground(Color.black);
  197.   }
  198.    
  199.   public void start()
  200.   {
  201.       //no code here
  202.   }
  203.    
  204.   public void paint(Graphics g)
  205.   {
  206.     triangle1.draw(g);
  207.     triangle1.moveDown(50);
  208.     delayMiliSecs(1000);
  209.     triangle1.draw(g);
  210.       poly1.draw(g);
  211.     delayMiliSecs(1000);
  212.     poly1.grow(2);
  213.       poly1.draw(g);
  214.     delayMiliSecs(1000);
  215.     rectangle1.grow(2);
  216.     rectangle1.draw(g);
  217.     }
  218. }  
RAW Paste Data