Hello I am trying to paint a bar graph using g.fillRect() to make each bar.
The bars will start at the grid height, y: 700, and fill upwards to y: 350 for example.
I am unable to set a negative x-height for my bar:
public void paint(Graphics g) {
for (Bar b : this.bars) {
g.fillRect(b.x_location,b.y_location,b.width,b.height * (-1));
}
}
This won't paint the bars.
Fix:
public void paint(Graphics g) {
for (Bar b : this.bars) {
g.fillRect(b.x_location,b.y_location - b.height,b.width,b.height);
}
}
You can't, because of the way the API works, it extends down and to the right. Instead, subtract the distance from the y coordinate and maintain the height as a positive value
From the JavaDocs
Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.
While not a direct duplicate, this example demonstrates the basic concepts