<< 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

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
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
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
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
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?