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: Java animation using pictures help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java animation using pictures help

    Hi I am making an animation in java using a sequence and I am having trouble getting started. Can I do it with this code? I need to know how to make pictures appear on the java applet screen in order. What do I name the pictures that I want to use? What directory do I put the pictures in? What statement/declarations do I have to add to this code? Thanks.


    import java.awt.*;
     
    public class Animate extends javax.swing.JApplet
    implements Runnable {
     
    Image [] picture = new Image [6];
    int totalPictures = 0;
    int current = 0;
    Thread runner;
    int pause = 500;
     
    public void init() {
    	for (int i = 0; i < 6; i++) {
    		String imageText = null;
    		imageText = getParameter("image" + i);
    		if (imageText != null) {
    			totalPictures++;
    			picture[i] = getImage(getCodeBase(), imageText);
    		} else
    			break;
    	}
    	String pauseText = null;
    	pauseText = getParameter("pause");
    	if (pauseText != null) {
    		pause = Integer.parseInt(pauseText);
    	}
    	}
     
    public void paint (Graphics screen) {
    	super.paint(screen);
    	Graphics2D screen2D = (Graphics2D) screen;
    	if (picture[current] != null)
    		screen2D.drawImage(picture[current], 0, 0, this);
    }
     
    public void start() {
    	if (runner == null) {
    		runner = new Thread(this);
    		runner.start();
    	}
    }
     
    public void run() {
    	Thread thisThread = Thread.currentThread();
    	while (runner == thisThread) {
    		repaint();
    		current++;
    		if (current >= totalPictures)
    			current = 0;
    		try {
    			Thread.sleep(pause);
    		} catch (InterruptedException e) { }
    	}
    }
     
    public void stop() {
    	if (runner != null) {
    		runner = null;
    	}
    }
    }


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Java animation using pictures help

    I'm pretty sure that you cannot use the basic ImageIcon as you would in applications, The applet throws a hissy fit about not having permissions to actualy access a client computer for security reasons..

    try here: Java Applet Tutorial - Image Example

    That should help.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Java animation using pictures help

    Quote Originally Posted by NekoSH0GUN View Post
    Hi I am making an animation in java using a sequence and I am having trouble getting started. Can I do it with this code? I need to know how to make pictures appear on the java applet screen in order. What do I name the pictures that I want to use? What directory do I put the pictures in? What statement/declarations do I have to add to this code? Thanks.
    Hi, what are you trying to understand the code? Why don't try it yourself then ask questions for problems you may encounter?

    java exception
    Last edited by hns1984; January 11th, 2012 at 06:54 PM.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Java animation using pictures help

    As i said though.. Begin by researching your error. The J Applet will not allow you to read images from the local client machine no matter if its your own computer. You must use a a different method for Applets for implementing images. I will not give the solution how ever as i mentioned above the link might give you a few tips if you read through it.

Similar Threads

  1. 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, 04:11 AM
  2. Importing gif/pictures
    By javanerd in forum Java Theory & Questions
    Replies: 2
    Last Post: November 1st, 2010, 12:58 AM
  3. Replies: 1
    Last Post: April 1st, 2009, 03:47 PM
  4. How to animate pictures using JCreator?
    By bil_Imma in forum AWT / Java Swing
    Replies: 6
    Last Post: February 13th, 2009, 07:00 AM
  5. How to make java based animation?
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: June 7th, 2008, 07:55 AM