<< Chapter < Page Chapter >> Page >

Also, according to the Sun documentation, the method returns "an off-screen drawable image, which can be used for double buffering. The returnvalue may be null if the component is not displayable." The returned value is type Image .

The getGraphics method

There are eleven different versions of the getGraphics method in the JDK 1.7 standard library. The version of the method used in this programis defined in the Image class. This method is called on the Image object returned by the createImage method in Listing 3 .

According to the Sun documentation, this method creates a graphics context for drawing to an off-screen image. You will note that the reference tothe graphics context was cast to type Graphics2D and saved in a reference variable of the type Graphics2D . I explained the relationship between the Graphics class and the Graphics2D class in an earlier module.

At this point, we have two off-screen image objects of type Image and we have a graphics context on each of them. This makes it possible to usethe methods of the Graphics class and/or the methods of the Graphics2D class to draw on either of the off-screen images.

Remaining code in the constructor

Listing 4 shows the remainder of the constructor for the GUI class.

Listing 4 . Remaining code in the constructor.
drawOffscreen(g2Da,g2Db); myCanvas.repaint();}//end constructor

Listing 4 begins by calling the method named drawOffscreen to draw some points, lines, and vectors on the two off-screen images. Notethat references to each of the two graphics contexts are passed as parameters to the drawOffscreen method.

Then Listing 4 calls the repaint method belonging to the MyCanvas object to cause the overridden paint method belonging to that object to be executed. Later, we will see that the overridden paint method copies the two off-screen images to the canvas in the side-by-side format shownin Figure 1 causing the contents of those off-screen images to be displayed in a JFrame object.

Beginning of the drawOffscreen method

The beginning of the drawOffscreen method is shown in Listing 5 . The purpose of this method is to define points, lines, and vectors as underlyingdata objects and then to cause a visual manifestation of some of the points, lines, and vectors to be drawn onto the two off-screen images.

Listing 5 . Beginning of the drawOffscreen method.
void drawOffscreen(Graphics2D g2Da,Graphics2D g2Db){//Draw a label on each off-screen image. g2Da.drawString("Off-screen image A",osiWidth/8, osiHeight/8);g2Db.drawString("Off-screen image B", osiWidth/8,osiHeight/8);

Before getting into the underlying data objects, however, Listing 5 calls the drawString method belonging to the Graphics class to draw the two text strings shown in Figure 1 on the two off-screen images.

Instantiate four Line objects that will be used to draw borders

Listing 6 instantiates four GM2D02.Line objects that will ultimately be used to draw the borders around the left and right images shown in Figure 1 .

Listing 6 . Define four lines that will be used to draw borders.
//First define four points that will be used to define // the ends of the four lines.GM2D02.Point upperLeftPoint = new GM2D02.Point( new GM2D02.ColMatrix(1.0,1.0));GM2D02.Point upperRightPoint = new GM2D02.Point( new GM2D02.ColMatrix(osiWidth-1,1.0));GM2D02.Point lowerRightPoint = new GM2D02.Point( new GM2D02.ColMatrix(osiWidth-1,osiHeight-1));GM2D02.Point lowerLeftPoint = new GM2D02.Point( new GM2D02.ColMatrix(1.0,osiHeight-1));//Now define the four lines based on the endpoints.GM2D02.Line top = new GM2D02.Line(upperLeftPoint, upperRightPoint);GM2D02.Line rightSide = new GM2D02.Line( upperRightPoint,lowerRightPoint); GM2D02.Line bottom = new GM2D02.Line(lowerLeftPoint,lowerRightPoint); GM2D02.Line leftSide = new GM2D02.Line(upperLeftPoint,lowerLeftPoint);

Questions & Answers

where we get a research paper on Nano chemistry....?
Maira Reply
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
learn
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
learn
Google
da
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Bhagvanji
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
revolt
da
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
Nasa has use it in the 60's, copper as water purification in the moon travel.
Alexandre
nanocopper obvius
Alexandre
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask