Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
Using negative height in g.fillRect()

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);
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!