Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Animation in JApplet

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Animation in JApplet

    Hi everyone,

    I'm trying to create a very simple animation applet, but it doesn't seem to be working very well. For some reason, the background color isn't changing and the graphics aren't being drawn to the applet. I think the Thread is also causing some issues. Any help would be greatly appreciated.

    import java.awt.*;
    import javax.swing.*;
     
    public class Animation extends JApplet {
     
    	int x = 10;
    	int y = 10;
    	Drawing d;
     
     
    	public void init(){
    		setSize(500, 500);
    		d = new Drawing();
    		Container content = getContentPane();
     
    		content.add(d, BorderLayout.CENTER);
    		content.setVisible(true);
    	}
     
    	public void start(){
    		go(); 
    	}
     
    	public void go(){
     
    		for (int i = 0; i < 330; i++){
    			x++;
    			y++;
    			d.repaint();
     
    			try{
    				Thread.sleep(20);
    			} catch (Exception ex){}
    		}
     
    	}
    	public class Drawing extends JPanel {
    	    public void paintComponent(Graphics g) {
    	         super.paint(g); 
    	         Graphics2D g2 = (Graphics2D) g;
    	         this.setBackground(Color.BLACK);
    	         g.setColor(Color.ORANGE);
    	         g.fillOval(x, y, 30, 30);
    	    }
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Animation in JApplet

    If you wish to do animation, I would recommend using a Swing timer (or if you know what you are doing, use a separate Thread altogether)
    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Swing is single threaded - meaning all things swing (painting, events, etc...) are performed sequentially. In other words, you can stop tasks such as repainting and user interaction by performing long running tasks in this thread

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Animation in JApplet

    I think you can try this to make happen with the loops, recursive function and also with the multithreading concept. If you are familiar with this. That will be better option i think.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Animation in JApplet

    Quote Originally Posted by RaoPatek View Post
    I think you can try this to make happen with the loops, recursive function and also with the multithreading concept. If you are familiar with this. That will be better option i think.
    Hugh? What the heck does a recursive function have to do with animation?

Similar Threads

  1. Components I add to JApplet won't appear!
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2012, 11:47 AM
  2. Sockets in JApplet
    By shrey.haria in forum Java Networking
    Replies: 4
    Last Post: September 5th, 2011, 09:45 AM
  3. JApplet Not Running In Firefox
    By aussiemcgr in forum Java Theory & Questions
    Replies: 13
    Last Post: August 13th, 2011, 08:40 AM
  4. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  5. JApplet containing JButton and a JLabel
    By JuneM in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 08:32 AM

Tags for this Thread