Java Animation

Discussion in 'Java' started by Alioune, Aug 4, 2017.

  1. Alioune

    Alioune

    Joined:
    Jul 19, 2017
    Messages:
    13
    Likes Received:
    2
    Location:
    Phoenix, AZ
    I am trying to create a bouncing animation similar to this one:

    [​IMG]

    This is the code that I've written:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class GraphicsPractice implements ActionListener
    {

    private static JFrame frame;
    private static JPanel panel;
    private static Timer timer;
    private static ImageIcon image;
    private static final int WIDTH = 300, HEIGHT = 100, IMAGE_SIZE = 35;
    private static int x, y, moveX, moveY;
    private static final int DELAY = 20;
    private static Graphics g;


    public static void main(String[] args)
    {
    frame = new JFrame("Traveling Head");
    frame.setVisible(true);
    frame.setLocation(300 , 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new JPanel();
    timer = new Timer(DELAY, new GraphicsPractice());
    image = new ImageIcon("Head.gif");
    image.paintIcon(panel, g, x, y);

    x = 0;
    y = 40;
    moveX = moveY = 3;

    panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    panel.setBackground(Color.black);
    timer.addActionListener(new GraphicsPractice());
    timer.start();

    frame.getContentPane().add(panel);
    frame.pack();

    }

    public void actionPerformed(ActionEvent event)
    {
    x += moveX;
    y += moveY;

    if(x <= 0 || x >= WIDTH-IMAGE_SIZE)
    {
    moveX *= -1;
    }

    if(y <= 0 || y >= HEIGHT-IMAGE_SIZE)
    {
    moveY *= -1;
    }

    panel.repaint();
    }

    }

    I'm having problems with the paintIcon method. How do I use this method?
     
    Alioune, Aug 4, 2017
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Oracle3001
    Replies:
    0
    Views:
    786
    Oracle3001
    Oct 27, 2003
  2. Tristan
    Replies:
    6
    Views:
    1,749
    John C. Bollinger
    Dec 1, 2003
  3. Thanasis
    Replies:
    2
    Views:
    1,077
    Thanasis
    Jan 11, 2004
  4. Thanasis \(sch\)

    awt book+java animation book

    Thanasis \(sch\), Oct 13, 2004, in forum: Java
    Replies:
    1
    Views:
    948
    Alex Hunsley
    Oct 14, 2004
  5. Doug Miller
    Replies:
    3
    Views:
    879
    David Hilsee
    Feb 14, 2005
  6. shekharpati@gmail.com

    How to save an image from an java Animation/Applet

    shekharpati@gmail.com, Jun 20, 2005, in forum: Java
    Replies:
    1
    Views:
    1,262
    Andrew Thompson
    Jun 20, 2005
  7. steveh44
    Replies:
    2
    Views:
    688
    markspace
    Apr 19, 2011
  8. George
    Replies:
    1
    Views:
    206
    Jay B
    Oct 2, 2003
Loading...