Advertisement
Guest User

java project

a guest
Nov 22nd, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JComponent;
  3.  
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.geom.Line2D;
  8.  
  9. public class Herencia
  10. {
  11. public static void main (String args[])
  12. {
  13. JFrame marco = new JFrame();
  14. marco.setSize(640, 480);
  15. marco.setTitle("Primitivos Gr�ficos");
  16. marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. marco.setVisible(true);
  18.  
  19. GraphicComponent grafico = new GraphicComponent();
  20. marco.add(grafico);
  21. }
  22. }
  23.  
  24. class GraphicComponent extends JComponent
  25. {
  26. public void paintComponent(Graphics g)
  27. {
  28.  
  29. Poligono miPoligono = new Poligono(5, 40, 25);
  30. miPoligono.setCoordForOneVertex(0,20,40);
  31. miPoligono.setCoordForOneVertex(1,60,40);
  32. miPoligono.setCoordForOneVertex(2,60,10);
  33. miPoligono.setCoordForOneVertex(3,20,10);
  34. miPoligono.setCoordForOneVertex(4,30,30);
  35. miPoligono.moveDown(100);
  36. miPoligono.draw(g);
  37.  
  38. Triangulo triangle1 = new Triangulo(10,10,30,50,40,20,25,30);
  39. triangle1.draw(g);
  40.  
  41. Rectangulo rectangle1= new Rectangulo(80,60,140,80);
  42. rectangle1.draw(g);
  43.  
  44. g.setColor(Color.BLUE);
  45. rectangle1.grow(2);
  46. rectangle1.draw(g);
  47.  
  48.  
  49. g.drawString("Ejemplo de c�mo dibujar un string", 10, 160);
  50. }
  51. }
  52.  
  53. class Poligono
  54. {
  55. double x[], //contains x coordinate for vertices
  56. y[], //contains y coordinate for vertices
  57. xc, //(xc,yc) = figure's center or
  58. yc; //reference point
  59.  
  60. int totalPoints; //number of vertices
  61.  
  62. //Given method: Explain function
  63. Poligono()
  64. {
  65. }
  66.  
  67. Poligono(int totalPoints, double xc, double yc)
  68. {
  69. this.totalPoints = totalPoints;
  70. x = new double[totalPoints];
  71. y = new double[totalPoints];
  72. this.xc = xc;
  73. this.yc = yc;
  74. }
  75.  
  76. void setCenterCoords(double xc, double yc)
  77. {
  78. this.xc = xc;
  79. this.yc = yc;
  80. }
  81.  
  82. void setCoordForOneVertex(int index, double x, double y)
  83. {
  84. if ((index < totalPoints)&& (index >= 0))
  85. {
  86. this.x[index] = x;
  87. this.y[index] = y;
  88. }
  89. }
  90.  
  91. void grow(double factor)
  92. {
  93. int i;
  94.  
  95. for (i = 0; i < totalPoints; i++)
  96. {
  97. x[i] = (x[i] - xc) * factor + xc;
  98. y[i] = (y[i] - yc) * factor + yc;
  99. }
  100. }
  101.  
  102. //New method: TO DO
  103. double getPerimeter()
  104. {
  105. double perimeter = 0;
  106. {
  107. //requires the code to calculate the perimeter
  108. }
  109. return (perimeter);
  110. }
  111.  
  112. void moveDown(double displacement)
  113. {
  114. for (int i = 0; i < totalPoints; i++)
  115. {
  116. y[i] = y[i] + displacement;
  117. }
  118. yc = yc + displacement;
  119. }
  120.  
  121. void draw(Graphics g)
  122. {
  123. Graphics2D g2 = (Graphics2D) g;
  124. int i;
  125. Line2D.Double linea = new Line2D.Double(150, 200, 200, 240);
  126. for (i = 0; i < totalPoints - 1; i++)
  127. {
  128. linea = new Line2D.Double(x[i], y[i], x[i+1], y[i+1]);
  129. g2.draw(linea);
  130. }
  131. linea = new Line2D.Double(x[0], y[0], x[totalPoints - 1], y[totalPoints - 1]);
  132. g2.draw(linea);
  133. }
  134. }
  135.  
  136. class Triangulo extends Poligono
  137. {
  138. Triangulo(double x1, double y1, double x2, double y2,
  139. double x3, double y3, double xc, double yc)
  140. {
  141. super(3,xc,yc);
  142. setCoordForOneVertex(0, x1, y1);
  143. setCoordForOneVertex(1, x2, y2);
  144. setCoordForOneVertex(2, x3, y3);
  145. }
  146.  
  147. //New method: TO DO
  148. double getArea()
  149. {
  150. double area = 0.0;
  151. {
  152. //requires code to calculate area
  153. }
  154. return (area);
  155. }
  156.  
  157. //New method: TO DO
  158. void displayStringInside(String theString)
  159. {
  160. //requires code to display theString centralized
  161. //within the figure
  162. }
  163. }
  164.  
  165. class Rectangulo extends Poligono
  166. {
  167. //New: Given constructor
  168. Rectangulo ()
  169. {
  170. super(4,0.0,0.0);
  171. }
  172.  
  173. Rectangulo (double x1, double y1, double x2, double y2)
  174. {
  175. super(4,0.0,0.0);
  176. double midX,
  177. midY;
  178.  
  179. midX = (x1 + x2)/2.0;
  180. midY = (y1 + y2)/2.0;
  181. setCenterCoords(midX,midY);
  182. setCoordForOneVertex(0, x1, y1);
  183. setCoordForOneVertex(1, x2, y1);
  184. setCoordForOneVertex(2, x2, y2);
  185. setCoordForOneVertex(3, x1, y2);
  186. }
  187.  
  188. //New method: TO DO
  189. double getArea()
  190. {
  191. double area = 0.0;
  192. {
  193. //requires code to calculate area
  194. }
  195. return (area);
  196. }
  197.  
  198. //New method: TO DO
  199. void displayStringInside(String theString)
  200. {
  201. //requires code to display theString centralized
  202. //within the figure
  203. }
  204. }
  205.  
  206. //*** New class: Given and to be Explained
  207. class Cuadrado extends Rectangulo
  208. {
  209. Cuadrado (double x1, double y1, double width)
  210. {
  211. super(x1,y1,x1+width,y1+width);
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement