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.
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 ?
– Miguel Soeiro