Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: Need help please...

  1. #1
    etiger is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default 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);
    	}
      }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,567
    Rep Power
    23

    Default

    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.
    java.catalog likes this.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by etiger View Post
    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??

    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


    Java 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
    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.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •