I am practicing inheritance using a graphics program with the Expo class. So far I've got the inheritance part, my problem is with one of the methods of my XmasTree class. Altogether the program is supposed to draw a pine tree (a green triangle, really) while the XmasTree subclass is supposed to draw ornaments (red circles) on the tree.

To do that, I need to be able to set random coordinates within the area of the triangle - but I have no idea how. I know how to limit the range of the Expo.random method so that it finds coordinates inside a shape with consistent width and height, like a rectangle or such. My question is, since the triangle that the ornaments need to be within tappers toward the top, how can I write a code which makes the random method find a number that is within a smaller range of X coordinates, the higher up the triangle the Y coordinate is?

The code I wrote currently creates a red circle that is always to the left of, or on the left edge of, the triangle, but not within the triangle like it needs to be. I got there by figuring that by finding the slope of the triangle, if I multiply the number of units the Y coordinate is above the base of the triangle by the slope and then subtract that amount from the range of the X coordinates at the base, perhaps that would account for the tapering of the triangle, but it hasn't and I'm not sure that even makes sense... My variable naming may have gotten a bit confusing at the end, please tell me if you need further elaboration.

Code :

// Lab09GRFX05st.java // This is the student, starting file of the Lab09GRFX05 assignment. // This file provides the completed Tree class which is the superclass for the PineTree class. // PineTree is partially written. Students need to complete it. // XmasTree will inherit from PineTree and is not provided. import java.awt.*; import java.applet.*; public class Lab09GRFX05st extends Applet { public void paint(Graphics g) { XmasTree tree = new XmasTree(); tree.drawTrunk(g); tree.drawLeaves(g); tree.drawOrnament(g); } } class Tree { protected int baseCenterX; protected int baseCenterY; protected int trunkWidth; protected int trunkHeight; public Tree() { baseCenterX = 475; baseCenterY = 600; trunkWidth = 50; trunkHeight = 200; } public void drawTrunk(Graphics g) { Expo.setColor(g,128,64,0); int tlX = baseCenterX - (trunkWidth/2); int tlY = baseCenterY - trunkHeight; int brX = baseCenterX + (trunkWidth/2); int brY = baseCenterY; Expo.fillRectangle(g,tlX,tlY,brX,brY);; } public void drawLeaves(Graphics g) { Expo.setColor(g,Expo.green); int radius = 100; int centerX = baseCenterX; int centerY = baseCenterY - (trunkHeight + radius - 5); Expo.fillCircle(g,centerX,centerY,radius); } } class PineTree extends Tree { protected int leavesWidth; protected int leavesHeight; public PineTree() { leavesWidth = 300; leavesHeight = 350; } public void drawLeaves(Graphics g) { Expo.setColor(g,Expo.green); //center point int centerX = baseCenterX; int centerY = baseCenterY - (trunkHeight + leavesHeight); //left point int leftX = baseCenterX - (leavesWidth/2); int leftY = baseCenterY - trunkHeight; //right point int rightX = baseCenterX + (leavesWidth/2); int rightY = baseCenterY - trunkHeight; //(Temporary) Displays point coordinates so I can see what they are g.drawString(" "+centerX, centerX, centerY); g.drawString(" "+leftX, leftX-25, leftY); g.drawString(" "+rightX, rightX, rightY); // Expo.fillTriangle(g,centerX,centerY,leftX,leftY,rightX,rightY); } } class XmasTree extends PineTree { public XmasTree() { leavesWidth = 300; leavesHeight = 350; } public void drawOrnament(Graphics g) { Expo.setColor(g,Expo.red); int radius = 10; int centerX = baseCenterX; int centerY = baseCenterY - (trunkHeight); //sets random location for bulbs int maxH = 50 + radius; int minH = 400 - radius; //Slope of triangle: int slope= 7/3; //random height: int bulbH = Expo.random(maxH,minH); for(int k=1; k < 10; k++){ } if(bulbH < minH){ int rightW = 625; int leftW = 325; int bHeight = minH - bulbH; int rangeW = bHeight*slope; rightW = rightW - rangeW; leftW = leftW + rangeW; int bulbW = Expo.random(leftW,rightW); Expo.fillCircle(g,bulbH,bulbW,radius); } } }

The HTML file is:

HTML Code:`<APPLET CODE = "Lab09GRFX05st.class" WIDTH=1000 HEIGHT=650>`

</APPLET>