This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

When using drawArc or drawOval, is there a method that gives all points of that arc or oval?

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

double perc = 0.25; //value between 0 and 1

int ox = (int) origin.getX();

int oy = (int) origin.getY();

int px = (int) Math.floor(Math.sin(perc * 2 * Math.PI) * radius);

int py = (int) Math.floor(Math.cos(perc * 2 * Math.PI) * radius);

g.drawOval(ox-radius, oy-radius, radius*2, radius*2);

g.drawLine(ox, oy, ox+px, oy+py);

This will draw a circle with a needle pointing to the east. If you change the 0.25 to 0.75 it will point to the West, it works counter-clockwise but it's no problem to change the rotation, maybe you even know how, but if you simply negate the perc variable it will work the other way around.

Mark

int xOrigin = 700;

int yOrigin = 500;

int radius = 150;

g.drawArc( xOrigin - radius, yOrigin - radius, radius * 2, radius * 2, 0, 180 ); // top of my meter

g.drawLine( 550, 500, 850, 500 ); // bottom of my meter

double percentage = 0.0;

int xPercentage = ( int ) Math.floor( Math.sin( percentage * 2 * Math.PI ) * radius );

int yPercentage = ( int ) Math.floor( Math.cos( percentage * 2 * Math.PI ) * radius );

g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );

double percentage = .25; // 3 o'clock position

int xPercentage = ( int ) Math.floor( Math.sin( percentage * 2 * Math.PI ) * radius );

int yPercentage = ( int ) Math.floor( Math.cos( percentage * 2 * Math.PI ) * radius );

g.setColor( Color.WHITE );

g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );

percentage = .25; // 3 o'clock position

xPercentage = ( int ) Math.floor( Math.sin( .25 * 2 * Math.PI ) * radius );

yPercentage = ( int ) Math.floor( Math.cos( .25 * 2 * Math.PI ) * radius );

g.setColor( Color.WHITE );

g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );

percentage = .25; // 3 o'clock position

xPercentage = ( int ) Math.floor( Math.sin( 1.570796327 ) * radius );

yPercentage = ( int ) Math.floor( Math.cos( 1.570796327 ) * radius );

g.setColor( Color.WHITE );

g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );

//------------------------

// why are xPercentage and yPercentage different now?

percentage = .25; // "almost" 6 o'clock position

xPercentage = ( int ) Math.floor( 4.111820039 );

yPercentage = ( int ) Math.floor( 149.9436325 );

g.setColor( Color.RED );

g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );

// why are xPercentage and yPercentage different now?

//percentage = .25; // "almost" 6 o'clock position

//xPercentage = ( int ) Math.floor( Math.sin( percentage * 2 * Math.PI ) * radius );

//yPercentage = ( int ) Math.floor( Math.cos( percentage * 2 * Math.PI ) * radius );

g.setColor( Color.BLUE );

g.drawLine( xOrigin, yOrigin, xOrigin + 4, yOrigin + 149 );

If you want to read more about this, search for trigonometry, or take a look at this page:

http://www.clarku.edu/~djoyce/trig/

or in particular:

http://www.clarku.edu/~djoyce/trig/angle.html

Now the code:

//Constants

int xOrigin = 200;

int yOrigin = 200;

int radius = 100;

//Draw meter

g.drawArc( xOrigin - radius, yOrigin - radius, radius * 2, radius * 2, 0, 180 );

g.drawLine( xOrigin - radius, yOrigin, xOrigin + radius, yOrigin );

//Percentage is a value between 0 and 1. If zero, the needle will point to the

//left, if one it will point to the right. If it's 0.5 it will point upwards.

double percentage = i;

int xPercentage = ( int ) Math.round( Math.sin( (-percentage-0.5) * Math.PI ) * radius );

int yPercentage = ( int ) Math.round( Math.cos( (-percentage-0.5) * Math.PI ) * radius );

g.setColor( Color.RED );

g.drawLine( xOrigin, yOrigin, xOrigin + xPercentage, yOrigin + yPercentage );

Good luck,

Mark

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

What do you want to achieve? Maybe there is a easy solution, but therefore we need to know what you want to achieve. :-)

Mark