I try create two moving object from one class. I want to control they but JApplet doesn’t open with threads.
My codes:
import java.awt.Graphics;
import javax.swing.JApplet;
class RunnableThread extends JApplet implements Runnable{
Thread runner;
String name;
public RunnableThread(String namex){
name=namex;
}
public void init(){
setSize(200,300);
}
public void paint(Graphics g){
g.drawLine(20, 20, 50, 50);
}
public void run(){
System.out.println("test"+name);
}
}
import java.util.logging.Level;
import java.util.logging.Logger;
public class RunnableTest{
public static void main(String args[]){
RunnableThread bt1=new RunnableThread("test 1");
RunnableThread bt2=new RunnableThread("test 2");
Thread btt1=new Thread(bt1);
Thread btt2=new Thread(bt2);
btt2.start();
btt1.start();
}
}
You can’t run a JApplet through the main method. Before you try to do background threading in an applet (and this can be done), please first read the applet tutorials to see how to create and run applets.
When you get your simple applets working and want to get back to this, don’t have your JApplet class implement Runnable but rather delegate that responsibility to another class.
Please start here: Getting Started with Applets