Adblock breaks this site

[TUT] Making a Basic Java Applet

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

  1. crapkiller

    crapkiller Forum Addict

    Joined:
    Jul 19, 2006
    Posts:
    426
    Referrals:
    0
    Sythe Gold:
    0
    [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!
     
  2. Eric

    Eric Grand Master
    Banned

    Joined:
    Feb 23, 2007
    Posts:
    3,344
    Referrals:
    13
    Sythe Gold:
    25
    [TUT] Making a Basic Java Applet

    Hi, I'm Eric, and I approve of this message.
     
  3. x&#8313;

    x&#8313; Guest

    Referrals:
    1
    [TUT] Making a Basic Java Applet

    Why use Graphics to draw text when you can use an AWT Label?
     
  4. bennytheberry

    bennytheberry Grand Master
    Banned

    Joined:
    Dec 23, 2007
    Posts:
    2,037
    Referrals:
    0
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    thanks, i've been wanting to learn some java.
     
  5. sammy2708

    sammy2708 Member
    Banned

    Joined:
    Jan 5, 2008
    Posts:
    63
    Referrals:
    1
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    Cheers it was very useful.
     
  6. mystic-own

    mystic-own Forum Addict
    Banned

    Joined:
    Nov 27, 2007
    Posts:
    535
    Referrals:
    0
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    Thanks very usefull :)
     
  7. Line

    Line Forum Addict

    Joined:
    Jan 20, 2008
    Posts:
    271
    Referrals:
    1
    Sythe Gold:
    5
    [TUT] Making a Basic Java Applet

    Nice tut, thanks for this :)
     
  8. miner899

    miner899 Forum Addict
    Banned

    Joined:
    Dec 19, 2007
    Posts:
    256
    Referrals:
    0
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    very good guide
     
  9. The Dark

    The Dark Guru
    Banned

    Joined:
    Jan 21, 2007
    Posts:
    1,601
    Referrals:
    0
    Sythe Gold:
    0
    [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
     
  10. DonnyB4e56

    DonnyB4e56 Member
    Banned

    Joined:
    Apr 25, 2007
    Posts:
    49
    Referrals:
    0
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    Wow thx this is really cool.
     
  11. odt

    odt Guru
    Banned

    Joined:
    Mar 28, 2008
    Posts:
    1,307
    Referrals:
    18
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    Nice, easy to follow tutorial
     
  12. Clank

    Clank Guru
    Banned

    Joined:
    Mar 21, 2008
    Posts:
    1,608
    Referrals:
    2
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    Nice, very easy to follow ^ Sorry for the duplicate comment ^
    Worth reading.
     
  13. 0ld syth3 m3mb3r

    0ld syth3 m3mb3r Forum Addict
    Banned

    Joined:
    Mar 14, 2008
    Posts:
    546
    Referrals:
    1
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    very nice... even i learned somthing new..... and im a pro at this lol
     
  14. gordge

    gordge Apprentice
    Banned

    Joined:
    Sep 1, 2007
    Posts:
    656
    Referrals:
    4
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    Got to be one of the best tuts ive seen thanks!
     
  15. bolty

    bolty Guest

    Referrals:
    0
    [TUT] Making a Basic Java Applet

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

    Thanks alot
     
  16. Neofobic

    Neofobic Member

    Joined:
    May 22, 2008
    Posts:
    28
    Referrals:
    0
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    very nice tutorial , worth reading.. thanks!
     
  17. Kimo rulz

    Kimo rulz Forum Addict

    Joined:
    Jul 29, 2007
    Posts:
    427
    Referrals:
    0
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    The best simple and lovely guide:)
     
  18. joking

    joking Apprentice
    Do Not Trade

    Joined:
    Nov 9, 2007
    Posts:
    736
    Referrals:
    3
    Sythe Gold:
    0
    [TUT] Making a Basic Java Applet

    nice tutorial im glad i read
     
  19. Irable384

    Irable384 Guest

    Referrals:
    0
    [TUT] Making a Basic Java Applet

    fairly understandable...and real nice guide
     
  20. Bob Pab

    Bob Pab Guest

    Referrals:
    0
    [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 >


 
 
Adblock breaks this site