Jump to content

Animation Help

- - - - -

  • Please log in to reply
3 replies to this topic

#1
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
import java.awt.Graphics;

import java.awt.Image;


import javax.swing.*;


@SuppressWarnings("serial")

public class MainAnimation extends JFrame {

	public static void main(String[] args) {

		new MainAnimation();

	}

	private MainAnimation() {

		this.setSize(600, 610);

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		Animator animator = new Animator();

		Animator animator2 = new Animator();

		this.add(animator);

		this.add(animator2);

		this.setVisible(true);

		while (true) {

			try {Thread.sleep(20);} catch (InterruptedException e) {}

			animator.repaint();

			animator2.repaint();

			this.repaint();

		}

	}

	private class Animator extends JPanel {

		int x = (int) ((Math.random() * 560) + 1);

		int y = (int) ((Math.random() * 560) + 1);

		ImageIcon imageIcon = new ImageIcon("ball.png");

		Image image = imageIcon.getImage();

		boolean plus = true;

		boolean yplus = true;

		public void paint(Graphics g) {

			if (plus) {

				x += 10;

			} else {

				x -= 10;

			}

			if (x >= 560) {

				plus = false;

			}

			if (x <= 0) {

				plus = true;

			}

			if (yplus) {

				y += 10;

			} else {

				y -= 10;

			}

			if (y >= 560) {

				yplus = false;

			}

			if (y <= 0) {

				yplus = true;

			}

			g.drawImage(image, y, x, 20, 20, this);

		}

	}

}


It only animates one ball. (Not two, like I want it to.)

---------- Post added at 10:57 AM ---------- Previous post was at 10:57 AM ----------

And yes I know x and y are messed up. XD

---------- Post added at 10:58 AM ---------- Previous post was at 10:57 AM ----------

y represents the x location and x represents the y location. :P

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I'd change the design of your program.
Inside of your MainFrame, I'd have 1 JPanel that could be called BallContainer.

BallContainer would extend JPanel. This is the panel that will contain the balls.
The BallContainer class would then contain an ArrayList of Ball objects.
Then you'd need to create a Ball class.

The Ball class would contain the x, y coordinates AND any image data.
The Ball class would also contain the drawing method used to draw this ball onto the BallContainer.


So for each ball in the BallContainer, you'll just need to call the appropriate draw() method.

Simple class diagram for what I'm talking about:
Attached File  ClassDiagram.png   5.96K   10 downloads

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
And there

Quote

[LEFT][COLOR=#333333]this.add(animator);
[/COLOR][COLOR=#333333]this.add(animator2);[/COLOR][/LEFT]
you screw things up by overwriting animator with animator2 :)

#4
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
OH! thanks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users