<< Chapter < Page Chapter >> Page >

Listing 27 . Source code for the program named PointLine03.

/*PointLine03.java Copyright 2008, R.G.BaldwinRevised 02/03/08 This program illustrates the use of draw methods thatwere added to the GM2D02 game-math library to produce visual manifestations of point, line, and vector objectsinstantiated from classes in the game-math library named GM2D02.The program also illustrates drawing on off-screen images and then copying those images to a Canvas object in anoverridden paint method. Tested using JDK 1.6 under WinXP.*********************************************************/ import java.awt.*;import javax.swing.*; class PointLine03{public static void main(String[] args){GUI guiObj = new GUI(); }//end main}//end controlling class PointLine03 //======================================================//class GUI extends JFrame{ //Specify the horizontal and vertical size of a JFrame// object. int hSize = 400;int vSize = 200; Image osiA;//one off-screen imageImage osiB;//another off-screen image int osiWidth;//off-screen image widthint osiHeight;//off-screen image height MyCanvas myCanvas;//a subclass of CanvasGUI(){//constructor//Set JFrame size, title, and close operation. setSize(hSize,vSize);setTitle("Copyright 2008,R.G.Baldwin"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Create a new drawing canvas and add it to the// center of the JFrame. myCanvas = new MyCanvas();this.getContentPane().add(myCanvas);//This object must be visible before you can get an // off-screen image. It must also be visible before// you can compute the size of the canvas. setVisible(true);osiWidth = myCanvas.getWidth()/2; osiHeight = myCanvas.getHeight();//Create two off-screen images and get a graphics // context on each.osiA = createImage(osiWidth,osiHeight); Graphics2D g2Da = (Graphics2D)(osiA.getGraphics());osiB = createImage(osiWidth,osiHeight);Graphics2D g2Db = (Graphics2D)(osiB.getGraphics());//Draw some points, lines, and vectors on the two // off-screen images.drawOffscreen(g2Da,g2Db); //Cause the overridden paint method belonging to// myCanvas to be executed. myCanvas.repaint();}//end constructor //----------------------------------------------------////The purpose of this method is to define points, lines,// and vectors and then to cause a visual manifestation // of some of the points, lines, and vectors to be// drawn onto two separate off-screen images. 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);//Define four lines that can be used to draw borders// on each of the off-screen images. //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);//Now draw a visual manifestation of each line// on g2Da. top.draw(g2Da);rightSide.draw(g2Da); bottom.draw(g2Da);leftSide.draw(g2Da);//Now draw a visual manifestation of each of the same // four lines on g2Dbtop.draw(g2Db); rightSide.draw(g2Db);bottom.draw(g2Db); leftSide.draw(g2Db);//Translate the origin of g2Da to the center of the// off-screen image. g2Da.translate(osiWidth/2.0,osiHeight/2.0);//Define a point at the new origin and draw a visual// manifestation of the point. GM2D02.Point origin = new GM2D02.Point(new GM2D02.ColMatrix(0.0,0.0)); origin.draw(g2Da);//Define six points that define the vertices of a// hexagon that is symmetrically located relative to // the origin. Begin at the right and move clockwise// around the origin. //First define three constants to make it easier to// write the code. final double aVal = osiWidth/4.0*0.5;final double bVal = osiWidth/4.0*0.866; final double cVal = osiWidth/4.0;//Now define the points. GM2D02.Point point0 = new GM2D02.Point(new GM2D02.ColMatrix(cVal,0.0)); GM2D02.Point point1 = new GM2D02.Point(new GM2D02.ColMatrix(aVal,bVal)); GM2D02.Point point2 = new GM2D02.Point(new GM2D02.ColMatrix(-aVal,bVal)); GM2D02.Point point3 = new GM2D02.Point(new GM2D02.ColMatrix(-cVal,0.0)); GM2D02.Point point4 = new GM2D02.Point(new GM2D02.ColMatrix(-aVal,-bVal)); GM2D02.Point point5 = new GM2D02.Point(new GM2D02.ColMatrix(aVal,-bVal));//Now draw a visual manifestation of each of the six // points on g2Da.point0.draw(g2Da); point1.draw(g2Da);point2.draw(g2Da); point3.draw(g2Da);point4.draw(g2Da); point5.draw(g2Da);//Now define six lines using the six points taken in// pairs to define the endpoints of the lines. GM2D02.Line line01 = new GM2D02.Line(point0,point1);GM2D02.Line line12 = new GM2D02.Line(point1,point2); GM2D02.Line line23 = new GM2D02.Line(point2,point3);GM2D02.Line line34 = new GM2D02.Line(point3,point4); GM2D02.Line line45 = new GM2D02.Line(point4,point5);GM2D02.Line line50 = new GM2D02.Line(point5,point0); //Now draw a visual manifestation of each line// on g2Da. line01.draw(g2Da);line12.draw(g2Da); line23.draw(g2Da);line34.draw(g2Da); line45.draw(g2Da);line50.draw(g2Da);//Now define three vectors and draw visual // manifestations of the vectors on g2Db.GM2D02.Vector vecA = new GM2D02.Vector( new GM2D02.ColMatrix(50,100));GM2D02.Vector vecB = new GM2D02.Vector( new GM2D02.ColMatrix(75,25));GM2D02.Vector vecC = new GM2D02.Vector( new GM2D02.ColMatrix(125,125));//Draw vecA in red with its tail at the origin, which// is still at the upper-left corner of the g2Db // off-screen imageg2Db.setColor(Color.RED); vecA.draw(g2Db,new GM2D02.Point(new GM2D02.ColMatrix(0,0)));//Draw three visual manifestations of vecB. Cause // the tail of vecB to coincide with the head of vecA// in one of the three visual manifestations. Make all // three of them green.g2Db.setColor(Color.GREEN); vecB.draw(g2Db,new GM2D02.Point(newGM2D02.ColMatrix( vecA.getData(0),vecA.getData(1))));vecB.draw(g2Db,new GM2D02.Point(new GM2D02.ColMatrix( vecA.getData(0)-10,vecA.getData(1)+15)));vecB.draw(g2Db,new GM2D02.Point(new GM2D02.ColMatrix( vecA.getData(0)+10,vecA.getData(1)-60)));//Draw vecC in blue with its tail at the origin. g2Db.setColor(Color.BLUE);vecC.draw(g2Db,new GM2D02.Point( new GM2D02.ColMatrix(0,0)));}//end drawOffscreen //====================================================////This is an inner class of the GUI class. class MyCanvas extends Canvas{//Override the paint() method. This method will be // called when the JFrame and the Canvas appear on the// screen or when the repaint method is called on the // Canvas object.//The purpose of this method is to display the two // off-screen images on the screen in a side-by-side// format. public void paint(Graphics g){g.drawImage(osiA,0,0,this); g.drawImage(osiB,this.getWidth()/2,0,this);}//end overridden paint()}//end inner class MyCanvas}//end class GUI

Questions & Answers

what is defense mechanism
Chinaza Reply
what is defense mechanisms
Chinaza
I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
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, 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