JAVA - Draw Polygons (triangles, pentagons)

Asked

Viewed 1,986 times

4

I was wondering if anyone could help me,

I’m doing a project where I have to draw various figures, between them triangles and pentagons, I’ve already got the rectangles working, 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);
}
}

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

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 )};
    }
}

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

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 hint or help ? Thank you.

2 answers

2

And if instead of using coordinates, consider the vertex of it, so creates a vector that increases with the amount, but then you should consider the angles, sides from a point. Using the point. Here is an example of polygon:http://www.cin.ufpe.br/~ccc2/PG/Poligono.java.

  • Exactly, that’s exactly what I’d like to do, but unfortunately I don’t know how to do it, I don’t know how to pass it to code, I don’t know how to create this vector from the coordinates of getx() and gety(), that’s exactly what I’d like to learn to do, you know how to help me ?

0


That’s how I was able to solve my problem and design a pentagon, in this case it’s a pentagon emptiness, or just have the lines of your 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 anyone wants to make a pentagon but full throughout your area, you only need 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 whole project you can find it on: soeiromass Github

Browser other questions tagged

You are not signed in. Login or sign up in order to post.