PDA

View Full Version : Jave Applets: I need help!




bobber205
Nov 29, 2005, 08:37 PM
/**
* 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
Nov 29, 2005, 09:54 PM
You cannot use something like System.out.println to display output in an Applet. The following code shows you what works:


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
Nov 29, 2005, 10:09 PM
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
Dec 1, 2005, 06:48 AM
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
Dec 1, 2005, 07:30 AM
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 (http://www.oreilly.com/catalog/javanut5/) back in 1998 or 1999. I'd still say it's the best Java book.

bousozoku
Dec 1, 2005, 01:48 PM
I thought myself Java (I could already program in quite a few languages) using Java In a Nutshell (http://www.oreilly.com/catalog/javanut5/) 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
Dec 3, 2005, 12:01 PM
Thanks for the help so far.

Here's my latest 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
Dec 3, 2005, 12:53 PM
I don't now how many times I've had to say this but, did you try reading the documentation (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html)?

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
Dec 3, 2005, 01:26 PM
Thanks for the help so far.

Here's my latest 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
Dec 13, 2005, 05:04 AM
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
Dec 13, 2005, 06:50 PM
Weird. I got an email saying there was a reply...

but nope. None here.

BTW thanks for the code.