Fill a figure in Java

0

How can I fill in the figure I draw in java when I made the figure with only g.drawLine?

import java.awt.*;
import javax.swing.*;


class Corazon extends Frame{
   public static void main(String[]args)
   {
      Corazon p=new Corazon();
      JOptionPane.showMessageDialog(null,"Bienvenido al Dibujo de un corazon");
   }
   public Corazon()
   {
      this.setSize(1600,900);
      setBackground(new Color(0,0,150));
      this.setVisible(true);
   }
   public void paint(Graphics g)
   {
      g.setColor(new Color(255,0,0));
      g.drawString("Stephanie B. Orihuela",400,100);
      g.drawLine(630,140,600,140);
      g.drawLine(600,140,570,170);
      g.drawLine(570,170,570,190);
      g.drawLine(570,190,590,210);
      g.drawLine(590,210,640,250);
      g.drawLine(640,250,690,210);
      g.drawLine(690,210,710,190);
      g.drawLine(710,190,710,170);
      g.drawLine(710,170,690,140);
      g.drawLine(690,140,660,140);
      g.drawLine(660,140,640,170);
      g.drawLine(640,170,630,140);

   }
}
    
asked by Stephanie B Bautista 07.02.2017 в 23:24
source

1 answer

1

You have to use the fillPolygon method. In your other question we recommended that you use polylines or polygons instead of loose lines. Even in your other question, I recommended that you first program the forms by programming since they are much more flexible than defining them point by point.

If you insist on doing it point by point, simply pass two arrays of integers (one for X and one for Y as I said @ReneGarnica) and the number of total points to the method fillPolygon(int[] x, int[]y, int n)

And another recommendation: you should call the method super.paint(g) within the method you overwrite if you do not want to lose what the super class does (for example clean the frame when you have to repaint.

    
answered by 07.02.2017 в 23:46