Results 1 to 3 of 3
  1. #1
    Beavotropper2 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Applet not working correctly?

    I'm new to java and wanted to start with a simple animation applet. The applet consists of a red circle being moved across the screen (to the right), but for some reason the ball never moves. I'm not sure if my run method is ever starting. Some input on this problem would be much appreciated. :)

    Java Code:
    package def;
    
    import java.applet.*;
    import java.awt.*;
    
    public class BallApplet extends Applet implements Runnable{
    	
    	int x_pos = 20;
    	int y_pos = 100;
    	int radius = 20;
    	private Image dbImage;
    	private Graphics dbg;
    	
    	public void init(){
    		setBackground (Color.black);
    	}
    	
    	public void start(){
    		Thread th1 = new Thread();
    		th1.start();
    	}
    	
    	public void stop(){}
    	
    	public void destroy(){}
    	
    	public void run(){
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
    		while(true){
    			x_pos++;
    			repaint();
    			
    			try{
    				Thread.sleep(20);
    			}catch(InterruptedException ex){}
    		
    		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    		}
    	}
    	
    	public void paint(Graphics g){
    		g.setColor(Color.red);
    		
    		g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    	}
    	
    	public void update(Graphics g){
    		if(dbImage==null){
    			dbImage = createImage(this.getSize().width, this.getSize().height);
    			dbg = dbImage.getGraphics();
    		}
    		
    		dbg.setColor(getBackground());
    		dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
    		
    		dbg.setColor(getForeground());
    		paint(dbg);
    	}
    }
    and the html file is...

    Java Code:
    <html>
    <body>
    <p><applet code ="BallApplet.class" width=700 height=400>
    </applet></p>
    </body>
    </html>
    Attached Files Attached Files
    Last edited by Fubarable; 04-18-2011 at 06:29 AM. Reason: mod edit: code tags corrected

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't ever call paint directly. Do all of your graphics in the paint method itself.

    Question: why AWT and not Swing which while not fully up to date, is much more up to date, robust and flexible than AWT.

  3. #3
    Beavotropper2 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Are you referring to repaint in the run method or paint in the update method?

    (Thanks for such a quick reply!)

Similar Threads

  1. drawString() method not working correctly
    By tnixon22 in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 08:57 PM
  2. Error Checking not working correctly
    By RickAintree in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 01:54 PM
  3. My rotate 2d pos method isnt working correctly..
    By Addez in forum New To Java
    Replies: 5
    Last Post: 12-01-2009, 09:04 AM
  4. Gueesing Game Almost done, but not working correctly
    By mbnumba6 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 03:01 AM
  5. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 04:45 PM

Posting Permissions

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