0

How can I find xpoints[] and ypoints[], if I want to draw a polygon with the mouse, using getX() and getY() ?

My code at the moment is:

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

And getX() and getY() part is:

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

How can I make this work? I just want draw a pentagon and a triangle with mouse.

Thanks for your time.

1
  • You could define a Shape using a Path2D and use a AffineTransform to change its size Dec 19, 2015 at 5:34

1 Answer 1

0

You could make a setup where you do something like

int numCoordinates = /*(get the number of coordinates by getting number of clicks after a certain action, etc.)*/
int[] xCords = new int[numCoordinates];
int[] yCords = new int[numCoordinates];


xCords[index] = e.getX();
yCords[index] = e.getY();
index++;

if(index > numCoordinates){
    index = 0;
    pEdicao.repaint();
}
1
  • But I just want one mouse click, for draw, like a paint in windows. In pain I chose 1 point, press this point, move mouse, and get a polygon. link Thanks for you helping me, I appreciate it. Dec 20, 2015 at 17:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.