Search code examples
javaswingcanvasarea

Finding pixels covered by an oval in Java Canvas


I'm creating a graphical display to show whether a user is approaching a danger zone. For this i'm using a display similar to that of an archery board. The idea is to start in the center, but as the user gets closer to the danger zone enter the orange zone. And once the user breaks the 'safety threshold' enter the red zone. My idea is to draw a point on the board depending on what value the user gives me. To draw this board I do the following.

Graphics2D g2d = (Graphics2D) bs.getDrawGraphics();
g2d.setColor(Color.white);
        g2d.fillRect(0, 0, 800, 600); //set screen to white
g2d.setColor(Color.red);
    g2d.fillOval(250, 250, 175, 175); //draw outerlayer of board as red
    g2d.setColor(Color.black);
    g2d.drawOval(250, 250, 175, 175);//give this red an outline
    g2d.setColor(Color.orange);
    g2d.fillOval(275, 275, 125, 125); //draw innerlayer as orange
    g2d.setColor(Color.black);
    g2d.drawOval(275, 275, 125, 125);//give this orange an outline
    g2d.setColor(Color.green);
    g2d.fillOval(300, 300, 75, 75); //draw innermost layer as green
    g2d.setColor(Color.black);
    g2d.drawOval(300, 300, 75, 75); //give the green an outline

First of all I could probably improve this, but that's not the main issue for now.

Instead my issue is finding exactly the pixels covered by each part of the board. I've been using x+(width/2), y+(height/2) to get the center point Thus using:

g2d.drawString(String.valueOf("X"), 338, 338);

To draw the center point of my green oval. This doesn't seem overly accurate but anyway. I thought I would be able to simply give the outer edge as the x,y co-ords and the inner edge as the x+height, y+width co-ords as so:

g2d.drawOval(500, 500, 75, 75);
g2d.drawString(String.valueOf("X"), 537, 537); //centre???
g2d.drawString(String.valueOf("X"), 575, 575); //inneredge?x+width, y+height
g2d.drawString(String.valueOf("X"), 500, 500); //outer edge x+y co-ords

However, I think due to the oval shape this doesn't work.

I would be very grateful if someone could show me how to find the pixel range that each oval covers.

EDIT: Below is the board that i'm using, is it possible to find the pixel range for each color(Red,Orange,Green)? Example of the board


Solution

  • Understanding a bit more what you wanted, here is the trick (using trigonometry):

    int force = ...; // from 0 to 250
    int radius = ...; // radius of the target (50 for example)
    int centerX = ...; // X-coordinate of the center of the target
    int centerY = ...; // Y-coordinate of the center of the target
    double direction = Math.toRadians(Math.random() * 360); // a random angle between 0° and 360°
    double x = (force * radius / 250.0d) * Math.cos(direction) + centerX; // X-coordinate of the new point
    double y = (force * radius / 250.0d) * Math.sin(direction) + centerY; // Y-coordinate of the new point
    Point2D point = new Point2D.Double(x,y); // Then you can plot this point on your target