Java Applets
Author |
Message |
andrew.
|
Posted: Tue Dec 09, 2008 7:41 pm Post subject: Java Applets |
|
|
Hi guys,
I recently started learning Java and have been checking out applets. The problem is that whenever I make an applet and I have the methods main and paint in there, paint is automatically executed and main is not. I think it may be because I've been programming on Mac or Linux and my friends have been programming on Windows, but still, there should be a way around it. Here is an example program:
Java: |
import java.applet.Applet;
import java.awt.*;
import javax.swing.JFrame;
public class MyApplet extends Applet
{
public void paint (Graphics g)
{
setBackground (Color.GRAY.brighter());
g.setColor(Color.black);
g.fillRect(0, 0, 200,50);
System.out.println ("Anything in this method is performed, but is only done once unless it is called again by repaint (). The weird thing is that it should return to main, shouldn't it?");
}
public static void main (String [] args)
{
MyApplet appID = new MyApplet();
System.out.println ("This line would not happen. Anything that I put in main doesn't work. I don't even think the JFrame things work fully.");
JFrame frameID = new JFrame();
frameID.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameID.add("Center", appID);
}
}
|
EDIT: Also, I am using Eclipse in both Mac and Linux and when I don't run the program as an applet, it does main, but doesn't do paint. When I do run it as an applet, it doesn't do paint, but does main.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
[Gandalf]
|
Posted: Tue Dec 09, 2008 7:46 pm Post subject: RE:Java Applets |
|
|
In applications, main() is the entry point of the program, however Applets work differently. You won't be using a main(), but rather methods such as start() init() destroy() and paint(), which are inherited from the Applet class.
http://java.sun.com/javase/6/docs/api/java/applet/Applet.html
|
|
|
|
|
|
S_Grimm
|
Posted: Tue Dec 09, 2008 7:49 pm Post subject: Re: Java Applets |
|
|
First. Applications use "main"
applets use "init"
Second. Shouldn't matter what OS you program on, java is platform independent.
Heres a snippet of code that uses Applets
Java: |
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChatSimulation extends JApplet implements ActionListener, Runnable {
private JTextArea transcript; // A text area that shows transmitted and
// received messages.
private JTextField input; // A text-input box where the use enters
// outgoing messages.
private Thread runner; // The thread the simulated the incoming messages.
private boolean running; // This is set to true when the thread is created.
// when it becomes false, the thread should exit.
// It is set to false in the applet's stop() method.
/**
* Initialize the applet.
*/
public void init() {
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
content.setLayout(new BorderLayout(3,3));
content.setBorder(BorderFactory.createLineBorder(Color.GRAY,3));
transcript = new JTextArea();
transcript.setEditable(false);
content.add(new JScrollPane(transcript),BorderLayout.CENTER);
JPanel bottom = new JPanel();
bottom.setBackground(Color.LIGHT_GRAY);
bottom.setLayout(new BorderLayout(3,3));
content.add(bottom,BorderLayout.SOUTH);
input = new JTextField();
input.setBackground(Color.WHITE);
input.addActionListener(this);
bottom.add(input,BorderLayout.CENTER);
JButton send = new JButton("Send");
send.addActionListener(this);
bottom.add(send,BorderLayout.EAST);
bottom.add(new JLabel("Text to send:"),BorderLayout.WEST);
setContentPane(content);
}
/**
* This will be called when the user clicks the "Send" button or presses
* return in the text-input box. It posts the contents of the input box
* to the transcript. If the thread is not running, it creates and starts
* a new thread.
*/
public void actionPerformed(ActionEvent evt) {
postMessage("SENT: " + input.getText());
input.selectAll();
input.requestFocus();
if (running == false) {
runner = new Thread(this);
running = true;
runner.start();
}
}
/**
* Add a line of text to the transcript area.
* @param message text to be added; two line feeds is added at the end.
*/
private void postMessage(String message) {
transcript.append(message + "\n\n");
// The following line is a nasty kludge that was the only way I could find to force
// the transcript to scroll so that the text that was just added is visible in
// the window. Without this, text can be added below the bottom of the visible area
// of the transcript.
transcript.setCaretPosition(transcript.getDocument().getLength());
}
/**
* The run method just posts messages to the transcript
* at random intervals of 2 to 10 seconds.
*/
public void run() {
//
postMessage("RECEIVED: Hey, hello there! Nice to chat with you.");
while (running) {
}
}
/**
* When applet is stopped, make sure that the thread will
* terminate by setting running to false.
*/
public void stop() {
running = false;
runner = null;
}
} // end class ChatSimulation
|
Heres an application that uses JFrame
Edit : Darn it. Gandalf beat me to it....... I hate having to search for files
Description: |
JFrame Application. Run the file called ChatClient |
|
Download |
Filename: |
Java IM.zip |
Filesize: |
2.65 KB |
Downloaded: |
88 Time(s) |
|
|
|
|
|
|
andrew.
|
Posted: Tue Dec 09, 2008 7:51 pm Post subject: RE:Java Applets |
|
|
Yeah, that was just sample code. My real applet has init, main, paint, action and a few others. Init, main, paint, and action should all run automatically. I know that when I run the program as an applet, paint, init, and action are all run, but when I run it as a program, only main is run.
So Gandalf, where would I put my code I want to use first if I wanted to initialize all the elements in an array? Normally, I would do that in main.
EDIT : So inside of applets, you don't need to use main? You can initialize things in init and draw things in paint?
|
|
|
|
|
|
[Gandalf]
|
Posted: Tue Dec 09, 2008 8:03 pm Post subject: RE:Java Applets |
|
|
Yes. The control flow of an applet goes something like:
init() -> start() -> paint() -> stop() -> destroy()
Edit: Err, fixed.
|
|
|
|
|
|
|
|