<< Chapter < Page Chapter >> Page >

Draw the solid green filled ellipse in the upper-left quadrant

In case you are unfamiliar with the term, an ellipse is the 2D shape shown in the upper-left quadrant of Figure 3 . A circle is an ellipse with the major and minor axes having the same lengths.

Figure 3 - A drawing of an ellipse.

Image showing three circles and one ellipse.

There is no circle class

Java does not provide a Circle2D.Double class but it does provide an Ellipse2D.Double class, which you can use to draw circles.

(The Graphics class also provides a drawOval method that can be used to draw circles but those circles won't suffice for whatwe will be doing in this module.)

Four constructor parameters

The constructor for the Ellipse2D.Double class requires four parameters of type double . The first two parameters are the X and Y coordinates of the upper-left corner of an imaginary rectangle.

The next two parameters are the width and the height of the imaginary rectangle.

The sides of the imaginary rectangle are parallel to the X and Y axes, but can be rotated using affine transforms.

An ellipse inside an imaginary rectangle

The ellipse is constructed inside the imaginary rectangle such that it is symmetrical about its horizontal and vertical axes and it touches all four sidesof the rectangle. (The documentation refers to the rectangle as a framing rectangle.)

How do you construct a circle?

If the rectangle is actually a square, then the ellipse becomes a circle.

Construct an ellipse inside an imaginary square

The first statement in Listing 6 constructs an object of type Ellipse2D.Double inside a 128x128 square that just fits in the upper left quadrant of our Cartesian coordinate system shown in Figure 1 . The circle object's reference is saved in the reference variable named circle1 .

Listing 6 - Draw the solid green filled ellipse in the upper-left quadrant.
//Upper left quadrant Ellipse2D.Double circle1 =new Ellipse2D.Double(-128,-128,128,128); //Solid GREEN fillg2.setPaint(Color.GREEN); g2.fill(circle1);g2.draw(circle1);

Set the painting color

The statement near the middle of Listing 6 sets the painting color to Color.GREEN .

(Note that Listing 6 calls setPaint whereas Listing 5 calls setColor . I will leave it as an exercise for the student to study the documentation in order to understand thedifference between setColor and setPaint .)

Fill the circle referred to by circle1

The second statement from the bottom in Listing 6 calls the fill method of the Graphics2D class passing the circle object's reference as a parameter. Although this can get quite complex, in this simplecase, it causes the circle object to be filled with the paint color (green) .

Draw the filled circle object

Finally, the last statement in Listing 6 calls the draw method of the Graphics2D class to cause the filled circle to be drawn inside the framing rectangle that was specified when the circle wasconstructed. This results in the filled green circle in the upper-left quadrant in Figure 1 .

Draw a circle with a gradient fill in the upper-right quadrant

This is where things tend to get a little complicated.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask