JAVA - drawing polygons (triangles, pentagons)
Good I wanted to know if anyone could help me,
I'm doing a project where I have to draw several figures, including triangles and pentagons, I 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);
}
}
I in this example used the drawRect()
which is in the Java API, when trying to do the same with the polygons with the drawPolygon()
, I had some problems with this, the code of the polygons I did (poorly) 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 )};
}
}
O drawPolygon()
I'm using it from a wrong way, since xPoints
and yPoins
are not entering 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();
}
Does anyone know how to give me a tip or any help ? Thanks.
2 answers
And if instead of you using coordinates, consider the vertex of it, so you create a vector that increases with the amount, but then you should consider the angles, sides from a point. Using o point . Here is an example Polygon: http://www.cin.ufpe.br / ~ccc2/PG/Poligono.java .
This is how I managed to solve my problem and be able to draw a Pentagon, in this case it is an empty Pentagon , that is, it only has 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 but full in their entire area, they just 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 whole project you can find it in: soeiromass GitHub