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.