Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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!
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,726
1,895
Lard
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);
  }
}
 

sk3pt1c

macrumors 6502a
Nov 29, 2005
918
6
a simulacrum
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
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
bobber205 said:
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

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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,726
1,895
Lard
robbieduncan said:
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.

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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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!
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,726
1,895
Lard
bobber205 said:
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.

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?
 

sk3pt1c

macrumors 6502a
Nov 29, 2005
918
6
a simulacrum
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
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
Weird. I got an email saying there was a reply...

but nope. None here.

BTW thanks for the code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.