JAVA - Draw Polygons (triangles, pentagons)

4

Well, I wanted to know if anyone could help me,

I'm doing a project in which I have to draw several figures, including triangles and pentagons, I've already managed to get the rectangles to work, rectangles:

import java.awt.Color;
import java.awt.Graphics;

public class Retangulo extends Figura {
int largura, altura;

public Retangulo() {
    super();
    largura = altura = 0;
}

public Retangulo(int x, int y, int l, int a, Color cor) {
    super(x, y, cor);
    largura = l;
    altura = a;
}

@Override
public void desenha(Graphics g) {
    g.setColor(cor);
    g.drawRect(p.x, p.y, largura, altura);
} 

@Override
public void setCoordenadas(int x1, int y1, int x2, int y2) {
    p.x = Math.min(x1, x2);
    p.y = Math.min(y1, y2);
    largura = Math.abs(x1-x2);
    altura = Math.abs(y1-y2);
}
}

In this example I used the drawRect() that is in the JAVA API, when trying to do the same with the polygons with drawPolygon() , I had some problems with that, the code of the polignos I did (bad) was: / p>

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Poligonos extends Figura{
    public void Poligonos (int[] xPoints, int[] yPoints, int nPoints){
        //private int[] xPoints = {(x1/2), x1, (x1+(x1/2))}  // {(getX()/2), getX(), (getX()+(getX()/2))};
        //private int[] yPoints = {( y1 + y1 ), y1 ,( y1 + y1 )};

    }
    @Override   
    public void desenha(Graphics g) {
        g.setColor(cor);
        g.drawPolygon(  xPoints, yPoints, 3);
    }
    @Override
    public void setCoordenadas(int x1, int y1, int x2, int y2) {
        p.x = Math.min(x1, x2);
        p.y = Math.min(y1, y2);

        int xPoints[] = {(p.x /2), p.x , ( p.x +( p.x /2))};  // {(getX()/2), getX(), (getX()+(getX()/2))};
        int yPoints[] = {( p.y + p.y ), p.y ,( p.y + p.y )};
    }
}

I am using it in the wrong way, since drawPolygon() and xPoints are not entering yPoins .

This is to be drawn with the mouse, through:

   @Override
     public void mousePressed(MouseEvent e) {
            x1 = e.getX();
            y1 = e.getY();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            x2 = e.getX();
            y2 = e.getY();
            r.setCoordenadas(x1, y1, x2, y2);
            pEdicao.repaint();
        }

Can anyone give me a tip or some help? Thank you.

    
asked by anonymous 18.12.2015 / 20:31

2 answers

0

This is how I was able to solve my problem and draw a pentagon, in this case it is an empty pentagon , that is only the lines of its perimeter:

public class Pentagono extends Figura {
    int[] xPoints;
    int[] yPoints;
    Ponto p2;

    public Pentagono () {
        super();
        p2 = new Ponto();
        xPoints = new int [5];
        yPoints = new int [5];

    }
    public Pentagono (int x, int y, int x2, int y2, Color cor){
        super(x, y, cor);
        p2 = new Ponto(x2, y2);
        xPoints = new int [5];
        yPoints = new int [5];
        setCoordenadas(x, y, x2, y2);
    }

    @Override
    public void setCoordenadas(int x1, int y1, int x2, int y2) {
        p.x = x1;
        p.y = y1;
        p2.x = x2;
        p2.y = y2;

        double raio = Point2D.distance(x1, y1, x2, y2);
        double ang = Math.atan2(y2-y1, x2-x1);
        double inc = 72 * Math.PI / 180;
        for(int i=0; i<xPoints.length; i++) {
            xPoints[i] = (int) (raio * Math.cos(ang) + x1);
            yPoints[i] = (int) (raio * Math.sin(ang) + y1);
            ang += inc;
        }
    }
    public void desenha(Graphics g) {
        g.setColor(cor);
        g.drawPolygon(xPoints, yPoints, 5);
    }
    public double Area() {
        Ponto p2 = new Ponto(xPoints[1], yPoints[1]);
        Ponto p3 = new Ponto(xPoints[2], yPoints[2]);
        double d = distancia(p2, p3);
        return( ((5*(d*d)) * Math.tan(54))/4);
    }
    public double Perimetro() {
        Ponto p2 = new Ponto(xPoints[1], yPoints[1]);
        Ponto p3 = new Ponto(xPoints[2], yPoints[2]);
        return ( 5 * (distancia(p2, p3)));
    }
    private double distancia(Ponto r, Ponto s) {
        return Math.sqrt(
                Math.pow(r.x-s.x, 2) + 
                Math.pow(r.y-s.y, 2)
                );
    }
    public String mostrarInfo() {
        Ponto p = new Ponto(xPoints[0], yPoints[0]);
        return (" Area = " +Area() +" Perimetro = " +Perimetro()+
                " Ponto Inicial = " +p.x +"," + p.y);
    }
    public boolean contains(int x, int y) {
        Polygon p = new Polygon(xPoints, yPoints, 5);
        if (p.contains(x, y)) {
            return true;
        }
        return false;
    }
    public void mover (int dx, int dy) {
        for(int i = 0; i < xPoints.length; i++) {
            xPoints[i] += dx;
            yPoints[i] += dy;
        }
    }
}

If someone wants to make a pentagon full throughout your area, you only have to add the following class:

public class PentagonoCheio extends Pentagono {
    public void desenha(Graphics g) {
        g.setColor(cor);
        g.fillPolygon(xPoints, yPoints, 5);
    }
}

If you want to see my entire project you can find it at: soeiromass GitHub

    
06.03.2016 / 15:33
2

And if instead of using coordinates, consider the vertex of it, so it creates a vector that increases with the quantity, but from there you must consider the angles, sides from a point. Using the point . Here's an example of a polygon: link .

    
22.12.2015 / 20:18