Doubts about the jTextArea.append (add the value of the 'area' variable to the BorderLayout) - Java

1

I have a problem in a line of code, in this line that is underlined in the image, it gives me an error that says that the coordinates x1, y1, x2, y2 must be static, it happens that this application consists of drawing a rectangle with the mouse, so its size is undefined, and I need to calculate your area and put in the underneath of BorderLayout, I'd like to know how I can do it.

Line of code that is giving error and that I tell you:

HereisthefunctionthatcalculatestheArea:

Hereiswheretheinitializationoftheobjectinquestion(FullRectangle):

Hereiswherethecoordinatesx1,y1,x2,y2(allofintegertype)comefrom:

Forthosewhopreferwhatisinimagesincode:

LineofcodethatisgivingerrorandthatItellyou:

publicstaticvoidmain(String[]args){Editore=newEditor();JTextAreajTextArea=newJTextArea();PointerInfoa=MouseInfo.getPointerInfo();Pointb=a.getLocation();intx=(int)b.getX();inty=(int)b.getY();e.add(jTextArea,BorderLayout.SOUTH);e.setVisible(true);e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//jTextArea.append("(x,y) = ("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");

    while (true) {
    jTextArea.append("(x,y) =("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+") ");
        try
        {
            Thread.sleep(30);
            jTextArea.setText("");
        }
        catch (InterruptedException e3)
        {
            e3.printStackTrace();
        }
    }

}

Here is the function that calculates the Area :

 @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);
    area = largura * altura;
}

public int setCoordenadasB(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);
    area = largura * altura;
    return area;
}

Here is where the initialization of the object in question (Full Rectangle):

bRetanguloCheio = new JButton ("RetânguloCheio");

    pBotoes.add(bRetanguloCheio);

    ActionListener acRetanguloCheio = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            r = new RetanguloCheio();
            r.setColor(cor);
        }
    };
    bRetanguloCheio.addActionListener(acRetanguloCheio);

Here is where the coordinates x1, y1, x2, y2 (all of integer type) come from:

@Override
public void mousePressed(MouseEvent e) {
    x1 = e.getX();
    y1 = e.getY();
    mousePressionado = true;
}

@Override
public void mouseDragged(MouseEvent e) {
    x2 = e.getX();
    y2 = e.getY();
    r.setCoordenadas(x1, y1, x2, y2);
    r.setCoordenadasB(x1, y1, x2, y2);
    pEdicao.repaint();
}

If anyone knows how to help, thank you, thank you .

    
asked by anonymous 27.12.2015 / 23:46

1 answer

0

This is the code used in the main function, which calls the showInfo () function of each class, my project had several figures, but in question in my question was the rectangle , the code placed in the main function is:

public void textoSul () {
        Thread t = new Thread(new Runnable(){
            @Override
            public void run() {
                while (true) {
                    info.setText("(x,y) =("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
                    if(r != null && r.p.x != 0) {
                        info.append (r.mostrarInfo());
                    }
                    else if (lista.size()>0) {
                        info.append(lista.get(lista.size()-1).mostrarInfo());
                    }
                    try
                    {
                        Thread.sleep(100);
                        info.setText("");
                    }
                    catch (InterruptedException e3)
                    {
                        e3.printStackTrace();
                    }
                }   
            }

        });
        t.start();
    }
  

NOTE: This code shows the mouse coordinates of the entire screen and displays the information of the figures only as they begin to be drawn.

The code placed inside the Rectangle class is:

@Override
    public String mostrarInfo() {
        return (" Area = " +Area() +" Perimetro = " +Perimetro()+
                " Ponto Inicial = " +p.x + "," +p.y +" largura = " + largura +" altura = "+altura);
    }
  

NOTE: This code will show information such as the Area or Perimeter , the Starting Point Width and the Height of the Rectangle.   To do this, you must have predefined functions corresponding to the Area and other information.

If someone has doubts about how to do these functions or doubts on how to show this information in other figures can consult the following link : soeiromass GitHub , the java files are located in the directory:" Editor-GeometricalFigures / src / editor /".

    
06.03.2016 / 15:58