0 Replies - 524 Views - Last Post: 15 June 2014 - 12:25 PM Rate Topic: -----

#1 The_VeryBest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-June 14

Java Animation Problem #2

Posted 15 June 2014 - 12:25 PM

Hey guys, I figured out the problem i had in my previous topic, however no wi have another problem. I want the enemy (swiper) to come in from the left or the right randomly and animate across the screen. I cannot figure out how to do this. Again, any help is appreciated, thanks in advanced! :helpsmilie:
:bigsmile:

P.S: the zip file of my project ----> http://www36.zippysh...99799/file.html

// The "BankGame" class.

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


public class BankGame extends JFrame implements KeyListener, ActionListener
{
    ImageIcon backGnd, ironManLeft, ironManRight, fireBallL, fireBallR, swiperL, swiperR;
    private Image doubleImage;
    private Graphics doubleGraph;

    boolean leftKey;
    boolean rightKey;

    int playerX = 400, playerY = 50;
    int fireBallXR = 745, fireBallXL = 430, fireBallY = 320;
    int swiperXR, swiperXL, swiperY;
    int random, pos = 1;



    Timer tmR = new Timer (1, this);

    Container frame;

    JTextArea paintPanel;

    public BankGame ()
    {

        super ("BankGame");   // Set the frame's name
        setSize (1280, 720);     // Set the frame's size
        frame = getContentPane ();
        Toolkit directory = Toolkit.getDefaultToolkit ();

        backGnd = new ImageIcon ("Bank.png");
        ironManLeft = new ImageIcon ("IronManLeft.png");
        ironManRight = new ImageIcon ("IronManRight.png");
        swiperL = new ImageIcon ("swiperLeft.png");
        swiperR = new ImageIcon ("swiperRight.png");
        fireBallL = new ImageIcon ("fireballLeft.png");
        fireBallR = new ImageIcon ("fireballRight.png");

        leftKey = false;
        rightKey = false;
        paintPanel = new JTextArea ();  // add a text area to paint your pics
        paintPanel.addKeyListener (this);  // add it as a listener to your keys
        paintPanel.setEditable (false);
        frame.add (paintPanel);
        setResizable (false);
        setVisible (true);                // Show the frame
        setDefaultCloseOperation (EXIT_ON_CLOSE);

    } // Constructor


    public void actionPerformed (ActionEvent e)
    {

        random = (int) (Math.random () * 2 + 1);

        if (fireBallXR > 1280)
        {

            rightKey = false;
            fireBallXR = 745;


        }
        if (fireBallXL < -100)
        {

            leftKey = false;
            fireBallXL = 420;

        }

        if (random == 1)
        {
            swiperXR = swiperXR - 10;

        }


        if (random == 2)
        {
            swiperXL = swiperXL + 10;

        }
        if (rightKey == true)
        {
            fireBallXR = fireBallXR + 10;
        }
        if (leftKey == true)
        {
            fireBallXL = fireBallXL - 10;
        }


        repaint ();

    }


    public void keyPressed (KeyEvent e)  //needs to be here because the class implements KeyListener
    {

        //System.out.println (e.getKeyCode ());
        if (e.getKeyCode () == KeyEvent.VK_LEFT)
        {


            leftKey = true;

        }
        else if (e.getKeyCode () == KeyEvent.VK_RIGHT)
        {
            rightKey = true;


        }

        repaint ();  //repaint the window everytime you press a key

    }


    public void paint (Graphics g)  // Double Buffer method
    {

        doubleImage = createImage (getWidth (), getHeight ());
        doubleGraph = doubleImage.getGraphics ();

        paintComponent (doubleGraph);

        g.drawImage (doubleImage, 0, 0, this);

    }


    public void paintComponent (Graphics g)
    {

        tmR.start ();

        backGnd.paintIcon (this, g, 0, 0);


        if (random == 1)
        {
            swiperXR = 0;
            swiperY = 320;
            swiperR.paintIcon (this, g, swiperXR, swiperY);

        }


        if (random == 2)
        {
            swiperXL = 745;
            swiperY = 320;

            swiperL.paintIcon (this, g, swiperXL, swiperY);

        }
        if (rightKey == true)
        {
            pos = 2;


            backGnd.paintIcon (this, g, 0, 0);

            ironManRight.paintIcon (this, g, playerX, playerY);

            fireBallR.paintIcon (this, g, fireBallXR, fireBallY);



        }
        else if (leftKey == true)
        {

          
            pos = 1;
            backGnd.paintIcon (this, g, 0, 0);

            ironManLeft.paintIcon (this, g, playerX, playerY);

            fireBallL.paintIcon (this, g, fireBallXL, fireBallY);




        }
        else if (pos == 1)
        {

            ironManLeft.paintIcon (this, g, playerX, playerY);
        }
        else if (pos == 2)
        {

            ironManRight.paintIcon (this, g, playerX, playerY);
        }


    } // paint method


    public static void main (String[] args)
    {
        new BankGame ();      // Create a BankGame frame
    } // main method













    public void keyReleased (KeyEvent e)  //needs to be here because the class implements KeyListener
    {

    }


    public void keyTyped (KeyEvent e)       //needs to be here because the class implements KeyListener
    {

    }
} // BankGame class


This post has been edited by andrewsw: 15 June 2014 - 01:05 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1