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 17 of 17

Thread: Java animation code

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

    Default Java animation code

    Hello I am new to the forum and hope you can help me I put my code and then the errors I receive thank you so much
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.Timer;
    public class Square extends JFrame {
    public Star() {
    setSize(1000,1000);
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(String[] args){
    new Star();
    }
    }
    class Doit extends JPanel {
    Timer timer;
    int x, y;
    public Doit() {
    setBackground(Color.RED);
    x = 30;
    y = 70;
    timer = new Timer(25, this);
    timer.start();
    }
    public void paint(Graphics g) {
    g.drawRect(x, y, 500, 500);
    g.fillRect(x, y, 500, 500);
    }
    while(x<1000) {
    x++;
    repaint();
    }
    }

    Star.java:5: error: invalid method declaration; return type required
    public Star() {
           ^
    Star.java:29: error: illegal start of type
    while(x<1000) {
    ^
    Star.java:29: error: illegal start of type
    while(x<1000) {
            ^
    Star.java:29: error: <identifier> expected
    while(x<1000) {
                ^
    Star.java:29: error: ')' expected
    while(x<1000) {
                 ^
    Star.java:29: error: ';' expected
    while(x<1000) {
                   ^
    Star.java:30: error: illegal start of type
    x++;
     ^
    Star.java:30: error: <identifier> expected
    x++;
       ^
    Star.java:30: error: ';' expected
    x++;
        ^
    Star.java:31: error: illegal start of type
    repaint();
           ^
    Star.java:31: error: <identifier> expected
    repaint();
            ^
    Star.java:33: error: class, interface, or enum expected
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    public Star() {
    That looks like the syntax for a class's constructor. Is it in a class named: Star?

    For the other errors, make sure the statements are inside of a method.

    The code is not properly formatted making it hard to read and understand what it does.
    Nested statements should be indented 3-4 spaces to show the nesting logic. All statements should NOT start in the first column.

    Please Edit the code and fix the formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    Quote Originally Posted by Norm View Post
    That looks like the syntax for a class's constructor. Is it in a class named: Star?

    For the other errors, make sure the statements are inside of a method.

    The code is not properly formatted making it hard to read and understand what it does.
    Nested statements should be indented 3-4 spaces to show the nesting logic. All statements should NOT start in the first column.

    Please Edit the code and fix the formatting.
    Hey! I redid the code as you asked, and now I only get one error. Could you help me figure out how to fix this error? Below is the code.

    import javax.swing.*;
     
    import java.awt.*;
     
    import java.awt.event.ActionListener;
     
    import javax.swing.Timer;
     
    public class Square extends JFrame
    {
     
        public Square()
        {
     
            setSize(1000,1000);
     
            setVisible(true);
     
            setResizable(false);
     
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
        }
     
     
        public static void main(String[] args)
        {
     
            new Square();
     
     
        }
     
     
     
    }
     
     
    class Board extends JPanel
    {
     
        Timer timer;
     
        int x, y;
     
        public Board()
        {
     
            setBackground(Color.RED);
     
            x = 30;
     
            y = 70;
     
            timer = new Timer(25, this);
     
            timer.start();
     
     
        }
     
     
        public void paint(Graphics g)
        {
     
            g.drawRect(x, y, 500, 500);
     
            g.fillRect(x, y, 500, 500);
     
            while(x&lt;
            1000)
            {
     
                x++;
     
                repaint();
     
     
            }
     
     
     
        }
     
     
     
     
    }
    now, I get the error:

    Square.java:23: error: constructor Timer in class Timer cannot be applied to given types;
    timer = new Timer(25, this);
            ^
      required: int,ActionListener
      found: int,Board
      reason: actual argument Board cannot be converted to ActionListener by method invocation conversion

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    argument Board cannot be converted to ActionListener
    The compiler is saying that Board is not an ActionListener.
    Read the API doc for the Timer class to see what data types are required for its constructor.

    See the tutorial on how to write an action listener:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    With Swing classes you should override the paintComponent() method, not the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    Quote Originally Posted by Norm View Post
    The compiler is saying that Board is not an ActionListener.
    Read the API doc for the Timer class to see what data types are required for its constructor.

    See the tutorial on how to write an action listener:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    With Swing classes you should override the paintComponent() method, not the paint() method.
    I did and it still doesn't work.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    It still does not work I made it an action listener

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    It still does not work I made it an action listener
    Post the code and the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    ok here is the code..



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
     
    public class Square extends JFrame {
    	public Square() {
    		setSize(1000, 1000);
    		setVisible(true);
    		setResizable(false);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
     
    	public static void main(String[] args) {
    		new Square();
    	}
    }
     
    class Board implements ActionListener {
    	Timer timer;
    	int x, y;
     
    	public Board() {
    		setBackground(Color.RED);
    		x = 30;
    		y = 70;
    		timer = new Timer(25, this);
    		timer.start();
    	}
     
    	public void paint(Graphics g) {
    		g.drawRect(x, y, 500, 500);
    		g.fillRect(x, y, 500, 500);
    		while (x < 1000) {
    			x++;
    			repaint();
    		}
    	}
     
    }


    the errors are:





    Square.java:19: error: Board is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
    class Board implements ActionListener {
    ^
    Square.java:24: error: cannot find symbol
    setBackground(Color.RED);
    ^
    symbol: method setBackground(Color)
    location: class Board
    Square.java:36: error: cannot find symbol
    repaint();
    ^
    symbol: method repaint()
    location: class Board

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    Board is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
    Read the tutorial in post #4 for how to write an action listener.

    cannot find symbol repaint()
    Where are the repaint() and setBackground() methods defined? The compiler can not find their definitions.


    Hint: the class they are in does not extend any component class.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    Where should I define the setBackground and repaint methods?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    Where should I define the setBackground and repaint methods?
    You should not use those names for your methods. Sometimes when you want to override an extended class's methods then you must use the same name for your method.
    When you call methods that are not in a class, then you need a reference to an instance of a class that has those methods. The class Board does not extend a class with those methods.


    If you want to see what Java SE classes have those methods, go to the API doc site,
    click on the Index link in the blue strip at the top right,
    On the index page click on the first letter of the method and the find the method in the list of methods.
    The classes the method is in will be shown next to the method's name.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    Ok. I know i'm not supposed to really explicitly ask, but do you think you could make my code work? I know you're one of the best on the site and could do it in an instance(no pun intended ). Because I have been trying all day and am getting really frustrated is the only reason i'm asking thank you norm

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    What does the code in the Board class want to happen when it calls the methods in question?
    Where are those methods defined? How can the code in the Board class get access to the class object that contains those methods?

    Hint: the Board class is only a listener, it does NOT have any GUI stuff like a background.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    Ok, my new code is:


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
     
    public class Square extends JFrame {
    	public Square() {
    		setSize(1000, 1000);
    		setVisible(true);
    		setResizable(false);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
     
    	public static void main(String[] args) {
    		new Square();
    	}
    }
     
    class Board implements ActionListener {
    	Timer timer;
    	int x, y;
     
    	public Board() {
    		setBackground(Color.RED);
    		x = 30;
    		y = 70;
    		timer = new Timer(25, this);
    		timer.start();
    	}
     
    	public void paint(Graphics g) {
    		g.drawRect(x, y, 500, 500);
    		g.fillRect(x, y, 500, 500);
    		while (x < 1000) {
    			x++;
    		}
                    g.repaint();
     
    	}
     
    }



    now I get three errors;

     
    Square.java:19: error: Board is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
    class Board implements ActionListener {
    ^
    Square.java:24: error: cannot find symbol
    		setBackground(Color.RED);
    		^
      symbol:   method setBackground(Color)
      location: class Board
    Square.java:37: error: cannot find symbol
                    g.repaint();
                     ^
      symbol:   method repaint()
      location: variable g of type Graphics
    3 errors

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    Board is not abstract and does not override ...
    See post#4
    Those look like the same errors the code had before
    What did you change?

    Why does the code call the setBackground method? What component's background does it what to change.
    Board is NOT a component.

    error: cannot find symbol
    g.repaint();
    Read the API doc for the Graphics class. Also see post#11 for how to see what classes a method is defined in.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java animation code

    I can't figure it out could you fix it?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java animation code

    Sorry, I don't write code for OPs.

    Work on the problems one at a time.
    A quick solution for the two methods is to comment them out and do them later.

    That leaves the listener problem. Go back and read the tutorial again and see how it's done.

    Also look at: http://docs.oracle.com/javase/tutori...interface.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  2. Java animation using pictures help
    By NekoSH0GUN in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:49 AM
  3. 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
  4. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM
  5. How to make java based animation?
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: June 7th, 2008, 06:55 AM

Tags for this Thread