[TUT] Making a Basic Java Applet

Discussion in 'Archives' started by crapkiller, Apr 6, 2008.

[TUT] Making a Basic Java Applet
  1. Unread #1 - Apr 6, 2008 at 7:55 PM
  2. crapkiller
    Joined:
    Jul 19, 2006
    Posts:
    426
    Referrals:
    0
    Sythe Gold:
    0

    crapkiller Forum Addict

    [TUT] Making a Basic Java Applet

    Ok, lets get started.

    1) Imports

    These are the most commonly used imports for java applets

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    The first and second one are mandatory.

    In this tutorial, we will not do anything that uses the last one, but it is very common. Some things the last one is used for is making buttons that do things when clicked, listening for mouse clicks, listening for mouse motion etc.

    2) Class

    The next line of business is the class.

    Code:
    public class name extends Applet
    - The first word public is simple, all it means is that other classes can access the information in our class.

    - The second word just tells the compiler that this is a class.

    - The third word is the name of the java applet. I named mine name.

    - The fourth word means that this class is what ever follows (In this case it says this class is an Applet.)

    - The fifth word is what tells the compiler that this should be an applet.

    3) paint

    Code:
    public void paint (Graphics g)
    - This is a function that paints things onto our applet. (Words, lines, boxes, etc.)

    - The word void means that it doesn't return a value.

    - The word paint is the name of the function

    - This function takes one parameter. This parameter is a type called Graphics.

    - We are naming it g

    4) Things to draw

    Code:
    g.drawString("The text you want to draw", x, y);
    - This draws words on our applet.

    - The first parameter is what ever words you want to draw on the form(In quotes)

    EX:
    Code:
    g.drawString("Hello World", 20, 20);
    -----------------------------------------------------------------
    Code:
    g.drawLine(x1, y1, x2, y2);
    - This draws a line on our applet.

    - x1 is the x coordinate of where the line starts.
    - y1 is the y coordinate of where the line starts.

    - x2 is the x coordinate of where the line ends.
    - y2 is the y coordinate of where the line ends.

    EX:
    Code:
    g.drawLine(10, 10, 40, 40);
    -----------------------------------------------------------------
    Code:
    g.drawRect(x, y, width, height);
    - This draws a rectangle on our applet

    - x is the x coordinate of the upper left corner of the box
    - y is the y coordinate of the upper left corner of the box

    - width is the width of the box
    - height is the height of the box;

    EX:
    Code:
    g.drawRect(20, 20, 40,40);
    -----------------------------------------------------------------
    Code:
    g.fillRect(x, y, width, height);
    - This fills a rectangle.

    - x is the x coordinate of where to start filling
    - y is the y coordinate of where to start filling

    - width is the width of what to fill
    - height is the height of what to fill

    EX:
    Code:
    g.fillRect(20, 20, 40, 40);
    5) Compiling

    This is what you should have so far. NOTE: The str, x, y, x1, x2, y1, y2, width, height etc. should all be substituted for a number (With the exception of str, substitute that with something in quotes.

    Code:
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    
    public class name extends Applet{
    
    	public void paint (Graphics g){
    		g.drawString(str, x, y)
    		
    		g.drawLine(x1, y1, x2, y2);
    		
    		g.drawRect(x, y, width, height);
    		
    		g.fillRect(x, y, width, height);
    	}
    
    }
    
    NOTE: If you are using a program like Eclipse then skip this and just hit run java applet.

    Now save that as name.java

    NOTE: substitute name for the what ever you named your applet(Keep the .java)

    Open up command prompt

    In command prompt navigate to the folder where your .java is.

    type javac name.java

    as usual, substitute name for the name of your applet

    Wait a few seconds and you should get a .class file

    6) Running

    NOTE: if you are using a program like Eclipse then just skip this and hit run java applet

    First, open up notepad and before you type anything, save it as whatever.html

    Second, put this in notepad

    Code:
    <html>
    <body>
    <applet code = "name.class" width = 500 height = 500></applet> // NOTE: you can change the width and the height if you want.
    </body>
    </html>
    NOTE: Substitute name for what ever you named your applet (Keep the .class part)

    Save it.

    Now you can close notepad, and then click on the file you just made.

    It should open up your browser and your applet should load up.

    VIOLA!

    Some potential problems:

    Q: My web browser opens but my applet doesnt load.

    A: Make sure your applet and your html are in the same folder. Also make sure the code you have in your applet matches mine. (Again excluding the variables)

    Enjoy!
     
  3. Unread #2 - Apr 6, 2008 at 8:01 PM
  4. Eric
    Joined:
    Feb 23, 2007
    Posts:
    3,344
    Referrals:
    13
    Sythe Gold:
    25

    Eric Grand Master
    Banned

    [TUT] Making a Basic Java Applet

    Hi, I'm Eric, and I approve of this message.
     
  5. Unread #3 - Apr 7, 2008 at 4:26 AM
  6. x&#8313;
    Referrals:
    1

    x&#8313; Guest

    [TUT] Making a Basic Java Applet

    Why use Graphics to draw text when you can use an AWT Label?
     
  7. Unread #4 - Apr 7, 2008 at 5:19 PM
  8. bennytheberry
    Joined:
    Dec 23, 2007
    Posts:
    2,037
    Referrals:
    0
    Sythe Gold:
    0

    bennytheberry Grand Master
    Banned

    [TUT] Making a Basic Java Applet

    thanks, i've been wanting to learn some java.
     
  9. Unread #5 - Apr 8, 2008 at 1:12 PM
  10. sammy2708
    Joined:
    Jan 5, 2008
    Posts:
    63
    Referrals:
    1
    Sythe Gold:
    0

    sammy2708 Member
    Banned

    [TUT] Making a Basic Java Applet

    Cheers it was very useful.
     
  11. Unread #6 - Apr 13, 2008 at 9:39 AM
  12. mystic-own
    Joined:
    Nov 27, 2007
    Posts:
    535
    Referrals:
    0
    Sythe Gold:
    0

    mystic-own Forum Addict
    Banned

    [TUT] Making a Basic Java Applet

    Thanks very usefull :)
     
  13. Unread #7 - Apr 13, 2008 at 9:44 AM
  14. Line
    Joined:
    Jan 20, 2008
    Posts:
    271
    Referrals:
    1
    Sythe Gold:
    5

    Line Forum Addict

    [TUT] Making a Basic Java Applet

    Nice tut, thanks for this :)
     
  15. Unread #8 - Apr 15, 2008 at 12:48 AM
  16. miner899
    Joined:
    Dec 19, 2007
    Posts:
    256
    Referrals:
    0
    Sythe Gold:
    0

    miner899 Forum Addict
    Banned

    [TUT] Making a Basic Java Applet

    very good guide
     
  17. Unread #9 - Apr 17, 2008 at 3:10 PM
  18. The Dark
    Joined:
    Jan 21, 2007
    Posts:
    1,601
    Referrals:
    0
    Sythe Gold:
    0

    The Dark Guru
    Banned

    [TUT] Making a Basic Java Applet

    Thanks, think you could expand this to a Java basics tutorial? I'm starting to learn it and a list of commands etc would be great :D
     
  19. Unread #10 - Apr 20, 2008 at 5:12 PM
  20. DonnyB4e56
    Joined:
    Apr 25, 2007
    Posts:
    49
    Referrals:
    0
    Sythe Gold:
    0

    DonnyB4e56 Member
    Banned

    [TUT] Making a Basic Java Applet

    Wow thx this is really cool.
     
  21. Unread #11 - Apr 20, 2008 at 5:37 PM
  22. odt
    Joined:
    Mar 28, 2008
    Posts:
    1,307
    Referrals:
    18
    Sythe Gold:
    0

    odt Guru
    Banned

    [TUT] Making a Basic Java Applet

    Nice, easy to follow tutorial
     
  23. Unread #12 - Apr 23, 2008 at 3:19 AM
  24. Clank
    Joined:
    Mar 21, 2008
    Posts:
    1,608
    Referrals:
    2
    Sythe Gold:
    0

    Clank Guru
    Banned

    [TUT] Making a Basic Java Applet

    Nice, very easy to follow ^ Sorry for the duplicate comment ^
    Worth reading.
     
  25. Unread #13 - Apr 29, 2008 at 9:33 AM
  26. 0ld syth3 m3mb3r
    Joined:
    Mar 14, 2008
    Posts:
    546
    Referrals:
    1
    Sythe Gold:
    0

    0ld syth3 m3mb3r Forum Addict
    Banned

    [TUT] Making a Basic Java Applet

    very nice... even i learned somthing new..... and im a pro at this lol
     
  27. Unread #14 - May 19, 2008 at 5:00 PM
  28. gordge
    Joined:
    Sep 1, 2007
    Posts:
    656
    Referrals:
    4
    Sythe Gold:
    0

    gordge Apprentice
    Banned

    [TUT] Making a Basic Java Applet

    Got to be one of the best tuts ive seen thanks!
     
  29. Unread #15 - May 28, 2008 at 12:03 PM
  30. bolty
    Referrals:
    0

    bolty Guest

    [TUT] Making a Basic Java Applet

    Thanks for the tutorial, even if it's long to read, it still is very useful!

    Thanks alot
     
  31. Unread #16 - May 28, 2008 at 12:51 PM
  32. Neofobic
    Joined:
    May 22, 2008
    Posts:
    28
    Referrals:
    0
    Sythe Gold:
    0

    Neofobic Member

    [TUT] Making a Basic Java Applet

    very nice tutorial , worth reading.. thanks!
     
  33. Unread #17 - Jun 22, 2008 at 5:49 PM
  34. Kimo rulz
    Joined:
    Jul 29, 2007
    Posts:
    427
    Referrals:
    0
    Sythe Gold:
    0

    Kimo rulz Forum Addict

    [TUT] Making a Basic Java Applet

    The best simple and lovely guide:)
     
  35. Unread #18 - Aug 30, 2008 at 3:20 PM
  36. joking
    Joined:
    Nov 9, 2007
    Posts:
    736
    Referrals:
    3
    Sythe Gold:
    0

    joking Apprentice
    Do Not Trade

    [TUT] Making a Basic Java Applet

    nice tutorial im glad i read
     
  37. Unread #19 - Sep 14, 2008 at 9:49 AM
  38. Irable384
    Referrals:
    0

    Irable384 Guest

    [TUT] Making a Basic Java Applet

    fairly understandable...and real nice guide
     
  39. Unread #20 - Oct 22, 2008 at 1:44 AM
  40. Bob Pab
    Referrals:
    0

    Bob Pab Guest

    [TUT] Making a Basic Java Applet

    Nice, after i learn basics of java, i'll come straight here ;)
     
< Gods Gospel - The word | Video's UE Re-Application >

Users viewing this thread
1 guest


 
 
Adblock breaks this site