How to fill a polygon using MouseListener and MouseMotionListener

0

I have a problem with this code in which I have to create a polygon, in this case I have created something like a rectangle, and in it by means of MouseListener and MouseMotionListener when executing the program and clicking on the polygon it is filled with color.

This is the code that you use to create the polygon, but with respect to filling the polygon, I have not figured out how to do it

package polipoli;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class DibuPoli extends JPanel implements MouseListener, MouseMotionListener{

public void paint(Graphics g) {
  int[] xs = {125, 75, 125, 185, 125};
  int[] ys = {125, 75, 50, 100, 125};
  g.drawPolyline(xs, ys, 5);
}

@Override
public void mouseClicked(MouseEvent me) {    
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mousePressed(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseReleased(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseEntered(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseExited(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseDragged(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void mouseMoved(MouseEvent me) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

This is like the second time I ask a question here, and I hope it is well formulated what I need to solve. Thanks for your attention.

    
asked by Juan Andres 23.04.2018 в 00:47
source

1 answer

0

Good friend what you need to do is that in the mouse clicked method you have to get the coordinates from where you clicked simply by assigning the values obtained by me.getX () and me.getY ( ) to a variable in the method, to validate if it was inside the figure, in case that is so you only need to paint the figure again but this time put it as g.fillPolygon (xs, ys, 5) and call the repaint () method , for this the graphic variable g as well as the variables xs and and s must be declared globally

    
answered by 25.04.2018 в 18:37