This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.11 Game 2302-0130: putting the game-math library to work  (Page 3/20)

  • Number of points to define the geometric object.
  • X-component of the displacement vector.
  • Y-component of the displacement vector.
  • A checkbox to specify whether points are to be drawn.
  • A checkbox to specify whether lines are to be drawn.

These user-input features are shown at the bottom of Figure 1 .

Changing the number of points

Changing the number of points causes the number of vertices that describe the geometric object to change. For a large number of points, the geometric objectbecomes a circle as shown in Figure 2 .

Figure 2 Screen output for geometric object with 50 vertices.

For only three points, the geometric object would become a triangle. For four points, it would become a rectangle. For two points, it would become a line,etc.

Translation

Changing the components of the displacement vector causes the geometric object to be translated to a different location before being drawn in red. Inaddition to increasing the number of vertices, Figure 2 also shows the result of translating by 30 units along the x-axis and -40 units along the y-axis.

The positive y-axis:

The positive direction for the y-axis is down in the figures in this module. This is the default for Java graphics. I will resolve that issue and cause the positive direction for the y-axis to be up instead of down in a future module.

Checking and un-checking the checkboxes

Figure 3 shows the result of un-checking one of the checkboxes to prevent the points that define the vertices from being drawn. In this case, only the linesthat connect the vertices were drawn, resulting in the two circles shown.

Figure 3 Screen output without drawing the points at the vertices.

Similarly, the GUI can be used to cause only the points that define the vertices to be drawn without the connecting lines.

Will explain the code in fragments

I will explain the code in this program in fragments. A complete listing of the program is provided in Listing 18 for your convenience.

Much of the code in this module is very similar to code that I have explained in earlier modules, so I won't bore you by repeating those explanations. Rather,I will concentrate on code that is new and different in this module.

Beginning of the class named GUI

The beginning of the class named GUI is shown in Listing 1 .

Listing 1 . Instance variables in the class named GUI.
class GUI extends JFrame implements ActionListener{ //Specify the horizontal and vertical size of a JFrame// object. int hSize = 400;int vSize = 400; Image osi;//an off-screen imageint osiWidth;//off-screen image width int osiHeight;//off-screen image heightMyCanvas myCanvas;//a subclass of Canvas Graphics2D g2D;//Off-screen graphics context.//The following two variables are used to establish the// location of the origin. double xAxisOffset;double yAxisOffset;int numberPoints = 6;//Can be modified by the user. JTextField numberPointsField; //User input field.//The components of the following displacement vector // can be modified by the user.GM2D04.Vector vector = new GM2D04.Vector(new GM2D04.ColMatrix(50,50));JTextField vectorX;//User input field. JTextField vectorY;//User input field.//The following variables are used to determine whether// to draw the points and/or the lines. boolean drawPoints = true;boolean drawLines = true; Checkbox drawPointsBox;//User input fieldCheckbox drawLinesBox;//User input field.//The following variables are used to refer to array // objects containing the points that define the// vertices of the geometric object. GM2D04.Point[]points; GM2D04.Point[]newPoints;
<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.