|
|
Re: adding ovals
|
Posted: Oct 16, 2004 9:17 PM
|
|
Sorry no one has helped you out so far, its a really simple snippet to understand, so I'll throw in whatever I remember (I don't usually mess with awt applet modules - but this one is simple enough), really a downer that it has taken this long for someone to respond... Another thing, please format your code by indeting and maintain the format with the code tags (check out on the right when you are posting for the format procedure)... Indenting your code makes it easier on the eye for those of us who have been coding for a while and are used to reading neat code...
Here goes... > hi thank god i find this place , i need your help can you > please tell me how to add two or more ovals to this lablet > this is the code and please bare with me cause i am new in > computer programming so i will apriciete if you can tell > me step by step heres the code and thanks in advance : >
[code] import java.applet.*; /** import all classes from ** the applet package so we ** are able to use the library ** methods from the classes in ** that package **/ import java.awt.*; /** similar to above comment **/
//========================== Wormhole CLASS //============================ /** * This applet draws a scene consisting of ten nested blue e ovals * on a black background, along with a text message. */ public class Wormhole extends Applet { /** * Do all the drawing. * this method is necessary for an applet * responsible for all the stuff you will * be able to see when you fire up your * applet using appletviewer or when you * embed it in an html page */ public void paint(Graphics g) { // Draw the black background. g.setColor(Color.black); g.fillRect(0, 0, 500, 350); // Draw ten nested "holes." /** You are passing in the necessary ** arguments when you call drawHole from ** this class. This relates to your ** later question where you ask what the ** purpose of n is ** e.g drawHole(0,g) n is the parameter ** when you define draw hole and its value ** when you make the call using drawHole(0,g) ** is 0 **/ drawHole(0, g); drawHole(1, g); drawHole(2, g); drawHole(3, g); drawHole(4, g); drawHole(5, g); drawHole(6, g); drawHole(7, g); drawHole(8, g); drawHole(9, g); /** You may add ovals here by calling your ** drawHole method. Use whatever arguments ** you see fit... Play around with the values ** e.g try 10 or 12 or whatever, ensure there ** are no collisions with other objects in your ** applet (usually a run time problem - you'll ** probably experience it by playing around with ** the values, and eliminating some of the ** drawHole method calls you have made above **/ // Draw the text, in white with a blue drop // lue drop shadow. g.setColor(Color.blue); /** Set font of this graphic called from awt ** I believe **/ g.setFont(new Font("TimesRoman", Font.BOLD, 36)); /** Place the String "On to Cynthia" in the ** applet at coordinates 22, 300 relative to ** the top left hand corner of the applet ** window **/ g.drawString("On to Cydonia!", 22, 300); g.setColor(Color.white); // as explained above g.drawString("On to Cydonia!", 20, 298); } /** * Using the supplied Graphics object, draw the n-th * n-th hole. * n should be between 0 (darkest, largest) and 9 * and 9 (lightest, * smallest). */
/** You are now defining drawHole method ** which you call in your paint method ** It seems this draws your ovals **/ private void drawHole(int n, Graphics g) { int X0 = 50, // x-anchor Y0 = 50, // y-anchor W0 = 350, // initial width H0 = 200, // initial height X_INC = 30, // x-anchor increment Y_INC = 10, // y-anchor increment RG_INC = 10, // red-green increment BLUE_INC = 25; // blue increment g.setColor(new Color(n * RG_INC, n * RG_INC, (n + NC, (n + 1) * BLUE_INC)); /** fillOval is a method provided by the ** Graphics class found in java.awt package ** You may use the Java API for more detailed ** description of the fillOval method ** learn to use this API ** "IT WILL BE YOUR FRIEND" if you plan to ** program in Java. fillOval creates the Oval ** objects. You may find the Java API at ** http://java.sun.com/j2se/1.4.2/docs/api/ **/ g.fillOval(X0 + n * X_INC, Y0 + n * Y_INC, W0 - n * X_INC, H0 - n * 2 * Y_INC); } }
[/code]
I hope this clarifies a few things. If you have any further problems, fire away this is usually a very useful site. The fact that it has taken this long for a response is an oddity. Welcome to the Java and the Programming community. I hope you're not discouraged by the belated response, programming really is fun and challenging (and Java is one of the easier programming languages to learn).
One, Spike
> by the way one of the parts i dont get is the letter (n) > what is this letter for ?
|
|