Jave Applets: I need help!

Discussion in 'Mac Programming' started by bobber205, Nov 29, 2005.

  1. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #1
    Code:
    /**
     * Write a description of class FirstJava here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class FirstJava extends java.applet.Applet
    {
    	// instance variables - replace the example below with your own
    
    
    
    
    public static void main(String[] args) 
    {
    
    String nameString = new String("Alex Wait");
    System.out.println(nameString);
    }
    
    	
    	
    	
    	/**
    	 * Constructor for objects of class FirstJava
    	 */
    	public FirstJava()
    	{
    	   
    }
    
    
    
    }
    
    
    http://www.alexwait.com/Java/javapage.html

    Hmm. Nothing comes up. This is my first try at Java programming. It's a cool language, but I really want to use it for web based programming. I have other tools for desktop apps. ;)

    But when I run that page I typed above, nothing happens in Safari. In FF, some weird message appears...

    Help!
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    You cannot use something like System.out.println to display output in an Applet. The following code shows you what works:

    Code:
    import java.applet.*;
    import java.awt.*;
    
    // my declaration of a subclass to use functionality in Applet
    public class MyInfoApplet extends Applet
    {
      public void paint(Graphics g)
      {
        // output various information at x, y
        g.setColor(Color.blue);
        g.drawString("Alex Wait", 50, 50);
      }
    }
    
     
  3. macrumors 6502a

    sk3pt1c

    Joined:
    Nov 29, 2005
    Location:
    a simulacrum
    #3
    hmmm
    got a java exam next week
    right,first of all,i'd suggest you go buy a book
    cause in order to write applets you really need to know all the other basics
    i would also suggest you use swing and JApplet istead of Applet
    an example could be:

    import javax.swing.*;

    public class Welcome extends JApplet {

    public Welcome () {

    getContentPane().add( new JLabel("Welcome", JLabel.CENTER);

    }

    }

    but you also need to make use of the init, start, stop and destroy methods
    and you need to create an html document that would open your applet for you to see on your browser, which would look like this:

    <html>
    <head>
    <title>Welcome</title>
    </head>
    <body>
    <applet
    code="Welcome.class"
    width=300
    height=200
    >
    </applet>
    </body>
    </html>

    but you really need to study the basics and get to grips with how it works
    a good book is "Introduction to Java Programming" by Daniel Y. Liang
     
  4. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #4
    Jave books hmm?

    I have:
    Ground-Up Jave
    Special Edition Using Java 2 Standard Edition
    The Java Developer's Almanac
    Thinking in Java (coming in mail from ebay)

    How are those?:D
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    I thought myself Java (I could already program in quite a few languages) using Java In a Nutshell back in 1998 or 1999. I'd still say it's the best Java book.
     
  6. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #6
    I've worn out that one from using it as a reference. Also, Bruce Eckel's Thinking in Java, as well as Thinking in C++ should be on any programmer's bookshelf.
     
  7. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #7
    Thanks for the help so far.

    Here's my latest code.
    Code:
    import java.applet.*;
    import java.awt.*;
    
    public class firstjava extends Applet
    {
    public void init()
    {
    }
    
    public void start() 
    {
    String myString = new String("Alex Wait");
    }
    
    public void destroy()
    {
    }
    
    public void paint(Graphics g)
    {
    g.setColor(Color.blue);
    g.drawString("Alex Wait");
    }
    
    }
    
    
    
    

    And here are the error messages.

    firstjava.java:22: cannot resolve symbol
    symbol : method drawString (java.lang.String)
    location: class java.awt.Graphics
    g.drawString("Alex Wait");
    ^
    1 error


    I tried putting into java.awt.Graphics instead of g but it didn't work.

    Man. Do I have awaise to go.
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    I don't now how many times I've had to say this but, did you try reading the documentation?

    It is clear that drawString(String) does not exist in the this class but drawString(String,int,int) does. Basically you have to tell it where to draw the String at!
     
  9. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #9
    Funny, it's like the code I did for you, but you fixed it so that it doesn't work. You didn't want to go with what I had (which was already a functional applet) to learn from it?
     
  10. macrumors 6502a

    sk3pt1c

    Joined:
    Nov 29, 2005
    Location:
    a simulacrum
    #10
    i think you should have a go with swing and extend JApplet
    you can leave out the paint method
    here is a more elaborate applet,i've taken some code out to make it smaller
    you also should make it work as an application as well
    so you should include a main method at the bottom


    //importing packages
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;

    public class MyApplet extends JApplet implements ActionListener, Runnable {

    //declare your stuff here


    //Dealing with button clicks
    public void actionPerformed(ActionEvent e) {

    if (e.getSource() == something) {

    do something

    }//end if

    }//end of actionPerformed

    //overriden run method
    public void run(){

    do something

    }//end of run method


    //Initialize the applet
    public void init() {

    this is where you initialise the stuff you declared at the top

    //Declaring panels and layouts
    JPanel textpanel = new JPanel();
    JPanel grid = new JPanel();
    textpanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    grid.setLayout(new GridLayout(11,3,5,5));

    //Adding buttons and text fields to panels
    textpanel.add(something);
    textpanel.setBorder(new TitledBorder("hello"));

    //Adding the buttons to the grid layout
    grid.add(a button);

    //Adding the components to the applet
    getContentPane().add(textpanel, BorderLayout.NORTH);
    getContentPane().add(grid, BorderLayout.CENTER);

    //Registering listeners
    a_button.addActionListener(this);


    }//end of init

    //Start the applet
    public void start() {
    }

    //Stop the applet
    public void stop() {
    }

    //Destroy the applet
    public void destroy() {
    }

    }//end of MyApplet class
     
  11. thread starter macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Location:
    Oregon
    #11
    Weird. I got an email saying there was a reply...

    but nope. None here.

    BTW thanks for the code.
     

Share This Page