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 .