<< Chapter < Page Chapter >> Page >

Define the behavior of the X-button and make the JFrame visible

Finally, the constructor defines the behavior of the X-button in the top right corner of the JFrame and causes the JFrame to become visible on the screen.

Hopefully, everything that I have discussed so far is familiar to you. If not, you may want to spend some time studying my earlier tutorials in thisarea. You will find links to many of those tutorials at www.DickBaldwin.com .

Beginning of the inner class named MyCanvas

Listing 3 shows the beginning of an inner classdefinition named MyCanvas including the beginning of an overridden paint method.

(The Canvas class was extended into the MyCanvas class to make it possible to override the paint method.)

This overridden paint method will be executed whenever the JFrame containing the Canvas is displayed on the computer screen, whenever that portion of the screen needs to berepainted, or whenever the repaint method is called on the canvas.

Listing 3 . Beginning of the inner class named MyCanvas.
class MyCanvas extends Canvas{ public void paint(Graphics g){//Downcast the Graphics object to a Graphics2D // object. The Graphics2D class provides// capabilities that don't exist in the Graphics // class.Graphics2D g2 = (Graphics2D)g;//By default, the origin is at the upper-left corner // of the canvas. This statement translates the// origin to the center of the canvas. g2.translate(this.getWidth()/2.0,this.getHeight()/2.0);

An object of the Graphics class

The paint method always receives an incoming reference to an object of the class Graphics . As a practical matter, you can think of that object as representing a rectangular area on the screen on which the instructions inthe paint method will draw lines, images, and other graphic objects.

Incoming reference:

The paint method actually receives a reference to an object of the class Graphics2D , but to maintain backward compatibility, Sun elected to pass it in as the superclass type Graphics .

The Graphics2D class

The Graphics class was defined early in the life of the Java programming language, and it is of limited capability. Later on, Sun defined the Graphics2D class as a subclass of Graphics . Significant new capabilities, (including the ability to deal with coordinate values as real numbers instead of integers) , were included in the Graphics2D class. In order to access those new capabilities, however, it is necessary to downcastthe incoming reference to type Graphics2D as shown by the cast operator (Graphics2D) in Listing 3 .

Translate the origin

The last statement in Listing 3 translates the origin from the default position at the upper-leftcorner of the canvas to the center of the canvas. This is equivalent to creating a new coordinate frame as explained by Kjell.

The default coordinate frame has the origin at the upper-left corner of the canvas. The new coordinateframe has the origin at the center of the canvas.

Note, however, that even though the origin has been translated, the positive vertical direction is still toward the bottom of the canvas.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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