user3000135
user3000135

Reputation: 47

Trouble with Colours values in Java

I'm creating a class called Rectangle, where it gets two ponits (the origin one and the final one), and they are used by the FillRect() and DrawRect() functions, that are activated by a method called Visible. The rectangle data are coming from the main java class, which are both point A and B's X's and Y's positions, plus the outline color (color) and the fill color (color2). Inside of the class, I've created a method (type int) called Distance, which allows me to calculate the distance between the two points, giving the height and width values (both also int), so I can use the value to draw the rectangle correctly. The problem is the fact that I need to multiply, in some cases, the height and width values by -1. Even though they are both int types, it is wrongly switching the color and color2 values (ex color = blue, color2 = red. After *-1, color = red, color2 = blue). Of course, this error doesn't happen when I'm not multiplying by -1.

What am I doing wrong? Why is it switching like this?

Check out the code:

public class Rectangle extends Figure {

protected Point p1, p2, p3, p4;
protected int height, width;


public Rectangle (int x1, int y1, int x2, int y2, Color cor, Color cor2) {
super(cor, cor2);

this.p1 = new Point (x1, y1, cor, cor2);
this.p2 = new Point (x1, y2, cor, cor2);
this.p3 = new Point (x2, y1, cor, cor2);
this.p4 = new Point (x2, y2, cor, cor2);
this.color = color;
this.color2 = color2;


if (x2 > x1 && y2 > y1) {
    this.height = Distance(p1.getX(), p1.getY(), p3.getX(), p3.getY());
    this.width = Distance(p1.getX(), p1.getY(), p2.getX(), p2.getY());
}//correct!

if (x2 < x1 && y2 < y1) {
    this.height = -Distance(p1.getX(), p1.getY(), p3.getX(), p3.getY());
    this.width = -Distance(p1.getX(), p1.getY(), p2.getX(), p2.getY());
}//switched color!

if (x2 > x1 && y2 < y1) {
    this.height = Distance(p1.getX(), p1.getY(), p3.getX(), p3.getY());
    this.width = -Distance(p1.getX(), p1.getY(), p2.getX(), p2.getY());
}//switched color!

if (x2 < x1 && y2 > y1) {
    this.height = -Distance(p1.getX(), p1.getY(), p3.getX(), p3.getY());
    this.width = Distance(p1.getX(), p1.getY(), p2.getX(), p2.getY());
}//switched color!

}

public void Visible(Graphics g) {

g.setColor(this.cor2);
g.fillRect(this.p1.getX(), this.p1.getY(), this.height, this.width);
g.setColor(this.cor);
g.drawRect(this.p1.getX(), this.p1.getY(), this.height, this.width);

}

private int Distance(int x1, int y1, int x2, int y2) {

int distance = (int) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2,   2));

return distance;
}

Upvotes: 0

Views: 47

Answers (1)

camickr
camickr

Reputation: 324098

it gets two ponits (the origin one and the final one),

I'm guessing you can't guarantee the second point is down and right from the first point. The point could be above, resulting in a negative y value or to the left resulting in a negative x value.

I would suggest you can simplify your code by creating a Rectangle out of the two points by using code like:

int x = Math.min(startPoint.x, endPoint.x);
int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(startPoint.x - endPoint.x);
int height = Math.abs(startPoint.y - endPoint.y);
Rectangle rectangle = new Rectangle(x, y, width, height);

Now you have a Rectangle the drawRect() and fillRect() methods can use.

By the way, don't call your class Rectangle, that is a class already defined in the JDK.

Upvotes: 1

Related Questions