Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JComponent;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.geom.Line2D;
- public class Herencia
- {
- public static void main (String args[])
- {
- JFrame marco = new JFrame();
- marco.setSize(640, 480);
- marco.setTitle("Primitivos Gr�ficos");
- marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- marco.setVisible(true);
- GraphicComponent grafico = new GraphicComponent();
- marco.add(grafico);
- }
- }
- class GraphicComponent extends JComponent
- {
- public void paintComponent(Graphics g)
- {
- Poligono miPoligono = new Poligono(5, 40, 25);
- miPoligono.setCoordForOneVertex(0,20,40);
- miPoligono.setCoordForOneVertex(1,60,40);
- miPoligono.setCoordForOneVertex(2,60,10);
- miPoligono.setCoordForOneVertex(3,20,10);
- miPoligono.setCoordForOneVertex(4,30,30);
- miPoligono.moveDown(100);
- miPoligono.draw(g);
- Triangulo triangle1 = new Triangulo(10,10,30,50,40,20,25,30);
- triangle1.draw(g);
- Rectangulo rectangle1= new Rectangulo(80,60,140,80);
- rectangle1.draw(g);
- g.setColor(Color.BLUE);
- rectangle1.grow(2);
- rectangle1.draw(g);
- g.drawString("Ejemplo de c�mo dibujar un string", 10, 160);
- }
- }
- class Poligono
- {
- double x[], //contains x coordinate for vertices
- y[], //contains y coordinate for vertices
- xc, //(xc,yc) = figure's center or
- yc; //reference point
- int totalPoints; //number of vertices
- //Given method: Explain function
- Poligono()
- {
- }
- Poligono(int totalPoints, double xc, double yc)
- {
- this.totalPoints = totalPoints;
- x = new double[totalPoints];
- y = new double[totalPoints];
- this.xc = xc;
- this.yc = yc;
- }
- void setCenterCoords(double xc, double yc)
- {
- this.xc = xc;
- this.yc = yc;
- }
- void setCoordForOneVertex(int index, double x, double y)
- {
- if ((index < totalPoints)&& (index >= 0))
- {
- this.x[index] = x;
- this.y[index] = y;
- }
- }
- void grow(double factor)
- {
- int i;
- for (i = 0; i < totalPoints; i++)
- {
- x[i] = (x[i] - xc) * factor + xc;
- y[i] = (y[i] - yc) * factor + yc;
- }
- }
- //New method: TO DO
- double getPerimeter()
- {
- double perimeter = 0;
- {
- //requires the code to calculate the perimeter
- }
- return (perimeter);
- }
- void moveDown(double displacement)
- {
- for (int i = 0; i < totalPoints; i++)
- {
- y[i] = y[i] + displacement;
- }
- yc = yc + displacement;
- }
- void draw(Graphics g)
- {
- Graphics2D g2 = (Graphics2D) g;
- int i;
- Line2D.Double linea = new Line2D.Double(150, 200, 200, 240);
- for (i = 0; i < totalPoints - 1; i++)
- {
- linea = new Line2D.Double(x[i], y[i], x[i+1], y[i+1]);
- g2.draw(linea);
- }
- linea = new Line2D.Double(x[0], y[0], x[totalPoints - 1], y[totalPoints - 1]);
- g2.draw(linea);
- }
- }
- class Triangulo extends Poligono
- {
- Triangulo(double x1, double y1, double x2, double y2,
- double x3, double y3, double xc, double yc)
- {
- super(3,xc,yc);
- setCoordForOneVertex(0, x1, y1);
- setCoordForOneVertex(1, x2, y2);
- setCoordForOneVertex(2, x3, y3);
- }
- //New method: TO DO
- double getArea()
- {
- double area = 0.0;
- {
- //requires code to calculate area
- }
- return (area);
- }
- //New method: TO DO
- void displayStringInside(String theString)
- {
- //requires code to display theString centralized
- //within the figure
- }
- }
- class Rectangulo extends Poligono
- {
- //New: Given constructor
- Rectangulo ()
- {
- super(4,0.0,0.0);
- }
- Rectangulo (double x1, double y1, double x2, double y2)
- {
- super(4,0.0,0.0);
- double midX,
- midY;
- midX = (x1 + x2)/2.0;
- midY = (y1 + y2)/2.0;
- setCenterCoords(midX,midY);
- setCoordForOneVertex(0, x1, y1);
- setCoordForOneVertex(1, x2, y1);
- setCoordForOneVertex(2, x2, y2);
- setCoordForOneVertex(3, x1, y2);
- }
- //New method: TO DO
- double getArea()
- {
- double area = 0.0;
- {
- //requires code to calculate area
- }
- return (area);
- }
- //New method: TO DO
- void displayStringInside(String theString)
- {
- //requires code to display theString centralized
- //within the figure
- }
- }
- //*** New class: Given and to be Explained
- class Cuadrado extends Rectangulo
- {
- Cuadrado (double x1, double y1, double width)
- {
- super(x1,y1,x1+width,y1+width);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement