Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java Applet Beginner

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Applet Beginner

    This is my assignment from teacher:
    Copy and paste a SIMPLE method (that does not take input) from one of your other programs. The entire method should be pasted after the paint method, and called BY the paint method, passing the appropriate parameters. If the method displays output, you will need to pass the graphics object. Do not chooose a method that has multiple lines of output unless you really want a challenge.

    Is this a finished product? It says to call the method to the paint method, which I have done, is this a finished product?

    So this is my code:
    import java.applet.Applet;   // Imports the Applet class
    import java.awt.Graphics;    // Imports the Graphics class, used to draw lines, circles, squares, text, etc
     
    /** 
      * The HelloWorld class implements an applet that simply displays "Hello World!". 
      */ 
     
    // Our HelloWorld class extends  the Applet class, giving it access to all the methods of Applet.
    public class HelloWorld extends Applet {         
     
        // The paint method draws anything that is in our applet on the applet screen.  
        // It takes a graphics object (g), that is used to draw
     
        public void paint(Graphics g, double distance) 
        {     
     
            double x1;
            double x2;
            double y1;
            double y2;
            x1 = 1;
            x2 = 1;
            y1 = 1;
            y2= 1;
            g.drawString("Logan Carl Crone", 50, 25); 
            g.drawString("Can You Find Me??", 250,150);
     
     
            distance (x1, y1, x2, y2);
     
        }    
    public static double distance 
                   (double x1, double y1, double x2, double y2) { 
             double dx = x2 - x1;
             double dy = y2 - y1;
             double dsquared = dx*dx + dy*dy;
             double distance = Math.sqrt (dsquared);         
             return distance; 
     
    } 
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java Applet Beginner

    Quote Originally Posted by LoganC View Post
    This is my assignment from teacher: ....

    Is this a finished product? It says to call the method to the paint method, which I have done, is this a finished product?
    Does it compile? Does it run? Does it do what you want it to do?

    One hint: your instructor told you to have the paint method call the other method -- you'll want to do something with the double value returned by distance, otherwise it's a futile method.

    Another hint: your instructor never told you to change the parameters of the paint method, something it looks like you've done. This may not be a wise thing to do.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Applet Beginner

    How do I go about taking the return and printing it using drawString?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java Applet Beginner

    You get the double returned and put it into a double variable same as you normally would.

    double dbl = distance(x1, y1, x2, y2);

    Then use the dbl variable in drawString.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Applet Beginner

    Thats where I am confused, how do i make it show up on the applet? g.drawString(dbl, 150,150); isnt working

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java Applet Beginner

    Quote Originally Posted by LoganC View Post
    Thats where I am confused, how do i make it show up on the applet? g.drawString(dbl, 150,150); isnt working
    Ah, you need to translate it into a String. One "cheat" is to create a String on the spot by concatenating the numeric value with the empty String literal, "". i.e.,

       g. drawString("" + dbl, 150, 150);

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Applet Beginner

    Thanks a lot curmudgeon, was a great help.

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java Applet Beginner

    You're welcome! Also note that there are other ways to change a number into a String, one being String.valueOf(dbl).

  9. The Following User Says Thank You to curmudgeon For This Useful Post:

    LoganC (October 8th, 2012)

Similar Threads

  1. Im a java beginner and I need help :|
    By javaDO in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 8th, 2012, 03:28 PM
  2. Java Beginner
    By kney in forum Java Theory & Questions
    Replies: 10
    Last Post: October 18th, 2011, 06:38 AM
  3. Another beginner to Java!
    By jwise95 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 21st, 2011, 04:27 PM
  4. Very beginner - What's wrong in my applet declaration?
    By rforte in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2010, 04:54 AM
  5. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM