Results 1 to 3 of 3
Thread: Need help please...
- 07-17-2011, 08:59 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
Need help please...
I wrote a simple program. It suppose to run on applet. When I run, it says
" Applet not initialized" Please help me... Where is the problem??
Java Code://############ Applet Part import java.awt.*; import javax.swing.*; public class CarApp extends JApplet { public CarApp() { add(new CarControl()); } public static void main(String[] args) { CarApp applet = new CarApp(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Car App"); frame.add(applet, BorderLayout.CENTER); frame.setSize(500, 500); frame.setVisible(true); } } ///###################################### // == CarControl class import java.awt.*; import java.awt.event.*; import java.awt.color.*; import javax.swing.*; public class CarControl extends JFrame { public CarControl() { add(new CarMove()); } class CarMove extends JPanel { // set starting values private int x = 50; private int y = 50; public int RADIUS = 5; private int direction = 1; private int speed =300; //Set the timer for the car private Timer timer = new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) { x += 10; if (x>190) { x=-15; y=50; } repaint(); } }); public CarMove() { timer.start(); } public void reverse() { direction = -direction; } public void setSpeed(int ms) { //speed = ms; speed =30; timer.setDelay(speed); } // Creating the car body public void paintComponent(Graphics g) { super.paintComponent(g); //Draw rectangle g.setColor(Color.blue); g.fill3DRect(x+20, y-10, 10, 10, true); g.setColor(Color.red); g.fill3DRect(x, y, 50, 15,true); // Draw circle g.setColor(Color.black); g.fillOval(x + 5 +RADIUS, y + 10 + RADIUS, 2 * RADIUS, 2 * RADIUS); g.setColor(Color.black); g.fillOval(x + 25+ RADIUS, y + 10 + RADIUS, 2 * RADIUS, 2 * RADIUS); } } }
- 07-17-2011, 09:07 PM #2
What command are you using to execute your code?
I see a main() method in a JApplet. There is some confusion here.
Applets do not use a main() method. They have a set of methods that the browser/appletviewer calls.
- 07-19-2011, 12:58 AM #3
As alread mentioned you are mixing java methods with applet methods. You can delete the constructor of CarApp and the main method with its content and replace the code with the following
So now java is able to run the applet because your code has the init() method. for more details follow this link: http://download.oracle.com/javase/tu...ts/applet.htmlJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CarControl extends JApplet { public void init() { this.add(new CarMove()); this.setSize(500, 500); } class CarMove extends JPanel { // copy the code of CarMove here


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks