Doubt about jTextArea.append (add the value of the area variable to Borderlayout) - Java

Asked

Viewed 367 times

1

I have a problem with a line of code, this line that is underlined in the image, gives me an error that says that the coordinates X1, Y1, x2, Y2 have to be static, it turns out 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 part underneath of Borderlayout, I would like to know how to do it.

Line of code that’s going wrong and I’m telling you: Linha de código que me está a dar o erro:

Here is the function that calculates the Area:

Função que calcula a area:

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

Inicialização do Objecto:

Here’s where the coordinates X1,Y1,x2,Y2 (are all of the whole type):

Coordenadas:

For those who prefer what is in coded images:

Line of code that’s going wrong and I’m telling you:

public static void main(String[] args) {
    Editor e = new Editor();
    JTextArea jTextArea = new JTextArea();
    PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    int x = (int) b.getX();
    int y = (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 is (Rectangulocheio):

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’s where the coordinates X1,Y1,x2,Y2 (are all of the whole type):

@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 can help me, thank you.

  • I have already edited my publication, I am just trying to make there is a greater understanding of my doubt, since already end a topic only with code and some people did not understand it. Thank you, if you can help me or know someone, thank you.

  • 1

    Where do these variables X1, x2, Y1 and Y2 come from? What is the r variable? Is it started inside the main? It seems that the code posted is not complete.

  • Thanks for the suggestion, I’ve added the requested information.

  • At least for me, it is not yet clear where the variables I mentioned come from, the line of error. I saw what you pointed out in bold, but the code around the error line is unclear where the variables I quoted come from. I suggest you read on How to create a Minimum, Complete and Verifiable example

  • X1, Y1, x2, Y2 are coordinates that come from getx() and gety(), which are values obtained by the mouse, when selecting a point and dragging the mouse, it will make a rectangle, the only thing I want is to calculate the area of that rectangle as the rectangle is being created.

1 answer

0


This is the code used in the function main, where the function makes the call show insight() of each class, my project had several figures, but in question in my question was the figure 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 coordinates of the mouse of the whole screen and shows the information of the figures only when they start to be drawn.

The code placed inside the class Rectangle is:

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

NOTE: With this code the information is shown as the Area the Perimeter, the Starting Point to Width and the Height of the Rectangle. For this to happen it is necessary to have predefined functions corresponding to the Area and other information.

If anyone has doubts about how to do these functions or doubts about how to show this information in other figures, see the following link: soeiromass Github, java files are located in the directory: "Editor-Geometricalfigures/src/editor/".

Browser other questions tagged

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