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:
Here is the function that calculates the Area:
Here is where the initialization of the object in question is (Rectangulocheio):
Here’s where the coordinates X1,Y1,x2,Y2 (are all of the whole type):
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.
– Miguel Soeiro
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.
– user28595
Thanks for the suggestion, I’ve added the requested information.
– Miguel Soeiro
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
– user28595
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.
– Miguel Soeiro