Drawing through a . txt

Asked

Viewed 387 times

0

I have a big question about how to pass the data from the Quivo2D milking method to the hairX and Hairy variables, in the Quivo2D milking it goes through a file populating vx and Vy with the values of . txt, can someone give me a light on how to do this?

Follow the class below:

public class Monalisa extends JFrame {
Polygon hair;
int[] hairX=new int[15];
int[] hairY=new int[15];

public Monalisa(){
        hair = new Polygon(hairX,hairY,15);
        setBackground(Color.lightGray);
        repaint();
    }

    public static boolean leituraArquivo2D(String arquivo, int vx[],int vy[]){
        try{
            FileReader arq = new FileReader(arquivo);
            BufferedReader lerArq = new BufferedReader(arq);

            String linha = lerArq.readLine();

            int id=0;
            while((linha=lerArq.readLine())!=null){
                String vetXY[]=linha.split(" ");
                vx[id]=Integer.parseInt(vetXY[0]);
                vy[id]=Integer.parseInt(vetXY[1]);
                id=id+1;
            }
            arq.close();
            return true;

    }catch(IOException e){
        System.out.println("Erro na abertura do arquivo:"+arquivo);
        return false;
    }


}


    public void paint(Graphics g){
        g.setColor(Color.white);
        g.fillRoundRect(147, 84, 103, 74,23,23);
        g.fillOval(147, 94, 103, 132);

        g.setColor(Color.black);
        g.fillPolygon(hair);

        int[] eyebrow1X={151,168,174,171,178,193};
        int[] eyebrow1Y={145,140,148,184,191,188};
        g.drawPolyline(eyebrow1X, eyebrow1Y, 6);

        int[] eyebrow2X={188,197,213,223};
        int[] eyebrow2Y={146,141,142,146};
        g.drawPolyline(eyebrow2X, eyebrow2Y, 4);

        int[] mouthX={166,185,200};
        int[] mouthY={199,200,197};
        g.drawPolyline(mouthX, mouthY, 3);

        g.fillOval(161, 148, 10, 3);
        g.fillOval(202, 145, 12, 5);

    }

    public static void main(String[] args) {
        Monalisa mona = new Monalisa();
        mona.setSize(400, 400);
        mona.setVisible(true);


    }

}

1 answer

2


First, you have to define the variables hairX and hairY as static:

static int[] hairX = new int[15];
static int[] hairY = new int[15];

So just call the function leituraArquivo2D passing the required parameters from Main:

public static void main(String[] args){
  boolean b = leituraArquivo2D("C:\abc.txt", hairX, hairY);
  /*
  ...
  */
}
  • can you tell me why Static?

  • 1

    1º Because you are inside the main (Static, *statics methods can only access variables and other statics methods directly). 2º To ensure that the variable will be unique in memory, so it itself will be changed within the method it received by parameter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.