Results 1 to 3 of 3
  1. #1
    musico is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default Animation with colors

    I am a very novice java programmer. I am doing a basic beginners animation exercise of a ball bouncing around the screen. (copy and paste into your own program)

    package ballbounce;

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.*;


    public class BounceThread {
    public static void main(String[] args) {
    JFrame frame = new BounceThreadFrame();
    frame.show();
    }
    }

    class BounceThreadFrame extends JFrame {
    public BounceThreadFrame() {
    setSize(800, 600);
    setTitle("Bounce");


    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });

    Container contentPane = getContentPane();
    canvas = new JPanel();
    contentPane.add(canvas, "Center");
    JPanel p = new JPanel();
    JLabel lbl = new JLabel("");
    p.add(lbl);

    addButton(p, "Start", new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    Ball b = new Ball(canvas);
    b.start();
    }
    });

    addButton(p, "Close", new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    canvas.setVisible(false);
    System.exit(0);
    }
    });

    contentPane.add(p, "South");

    }

    public void addButton(Container c, String title, ActionListener a) {
    JButton b = new JButton(title);
    c.add(b);
    b.addActionListener(a);
    }

    private JPanel canvas;
    }

    class Ball extends Thread
    {
    private JLabel lbl;
    public Ball(JPanel b) {
    box = b;
    }

    public void draw() {
    Graphics g = box.getGraphics();
    g.fillOval(x, y, XSIZE, YSIZE);

    g.dispose();
    }

    public void move() {
    if (!box.isVisible())
    return;

    Graphics g = box.getGraphics();
    g.setXORMode(box.getBackground());
    g.fillOval(x, y, XSIZE, YSIZE);
    g.setColor(Color.red);

    x += dx;
    y += dy;

    Dimension d = box.getSize();
    if (x < 0) {
    x = 0;
    dx = -dx;
    }
    if (x + XSIZE >= d.width) {
    x = d.width - XSIZE;
    dx = -dx;
    }

    if (y < 0) {
    y = 0;
    dy = -dy;
    }
    if (y + YSIZE >= d.height) {
    y = d.height - YSIZE;
    dy = -dy;
    }

    g.fillOval(x, y, XSIZE, YSIZE);


    g.dispose();
    }

    public void run() {
    try {
    draw();
    for (int i = 1; i <= 1000; i++) {
    move();
    sleep(20);
    }
    } catch (InterruptedException e) {
    }


    }

    private JPanel box;

    private static final int frameRate = 60;

    private static final int XSIZE = 10;
    private static final int YSIZE = 10;
    private int x = 100;
    private int y = 0;

    private int dx = 20;
    private int dy = 20;



    }

    The problem I have is having set the color to a non black (default presumably) it retains each image 'frame' of the ball in a different color (usually turquoisy color) so the screen has a full trail of balls.

    How do I prevent this?

    Please go easy on me, I am very new to this.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Custom Painting should be done by overring the paintComponent() method of a JPanel (or JComponent), not by using the getGraphics() method. See: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Animation should be done by using a Swing Timer, not by using a Thread. The tutorial also has a section on Timers.

  3. #3
    musico is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Brilliant thanks!

    fwiw I did a sort of fix by adding the g.fillOval(x, y, XSIZE, YSIZE) any time a color was changed but I was a bit dubious about the whole animation procedure really. I adapted it from a BallBounce tutorial on the net. It's because I have done a lot of pure data in Java (classes etc.) but using that data to make animation displays is a big step.

Similar Threads

  1. Convert 24 bit colors to 16 bit
    By i4ba1 in forum Advanced Java
    Replies: 2
    Last Post: 12-09-2010, 01:23 AM
  2. In Case You Want Colors!
    By angryboy in forum Forum Lobby
    Replies: 0
    Last Post: 08-29-2009, 10:06 PM
  3. Rectangle and Colors
    By urbim in forum Java Applets
    Replies: 0
    Last Post: 07-11-2009, 03:03 PM
  4. Colors and shapes.
    By Torgero in forum New To Java
    Replies: 14
    Last Post: 10-13-2008, 05:25 PM
  5. change colors
    By calblue in forum New To Java
    Replies: 1
    Last Post: 12-02-2007, 11:52 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
  •