+1 (315) 557-6473 

Program That Creates A 2d Animation of a Moving Spaceship Using Java Programming Language Assignment Solution.


Instructions

Objective
Write a program that creates a 2d animation of a moving spaceship using Java programming language.

Requirements and Specifications

Drew the spaceship here(in uploaded java file) it is moving using keylistener L, U, R, D it is moving in up down left and right , I also need to enlarge and shrink that spaceship. I completed till up down left and right but was not able to enlarge and shrink that spaceship. I need only this feature. It should be shrunk by pressing any key and should be enlarged by pressing any key. Along with this feature, there is a 3 building at the bottom of that canvas, I need one of that building to be jumped(one time jump on pressing one time) by pressing any key.
So you need to do is: Shrink and enlarge spaceship AND jump any building by pressing any key.
Screenshots
program creates a spaceship in Java programming language
program creates a spaceship in Java programming language 1

Source Code

import java.awt.*;

import java.awt.event.*;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;

/**

 *

 * @author Yashkumar Chadasania

 * CS07580_10/12/2020

 */

public class MovingSpace extends JPanel

   implements KeyListener, ActionListener {

   Timer t=new Timer(5,this);

   int x=0, y=0, velx=0, vely=0;

   int width = 200;

   int height=200;

   public MovingSpace() {

      t.start();

      addKeyListener(this);

      setFocusable(true);

      setFocusTraversalKeysEnabled(false);

   }

   @Override

  public void paintComponent( Graphics g ) {

      //cleans the screen

      super.paintComponent(g);

      Graphics2D g2= (Graphics2D) g;

     //g2.fillRect(x, y, 40, 40);

     super.paintComponent(g);

    g2.setColor(Color.GREEN);

    g2.fillArc(125+x, 140+y, 150, 50, 45, 360);

    g2.setColor(Color.black);

    g2.drawArc(125+x, 140+y, 150, 50, 50, -280);

    g2.setColor(Color.GREEN);

    g2.fillArc(150+x, 110+y, 100, 75, 0, 180);

    g2.setColor(Color.black);

    g2.drawArc(150+x, 110+y, 100, 75, 0, 180);

    g2.setColor(Color.black);

    g2.fillArc(160+x, 130+y, 15, 25, 0, 360);

    g2.setColor(Color.red);

    g2.fillArc(180+x, 135+y, 15, 25, 0, 360);

    g2.setColor(Color.yellow);

    g2.fillArc(200+x, 135+y, 15, 25, 0, 360);

    g2.setColor(Color.cyan);

    g2.fillArc(220+x, 130+y, 15, 25, 0, 360);

    g2.setColor(Color.blue);

    g2.fillArc(25, 270, 350, 80, 0, 180);

    g2.setColor(Color.black);

    g2.fillRect(110, 235, 25, 50);

    g2.fillRect(140, 220, 25, 70);

    g2.fillRect(170, 235, 25, 50);

    g2.setColor(Color.magenta);

    g2.setFont(new Font("TimesRoman", Font.BOLD,12));

    g2.drawString("Yashkumar's", 160+x, 170+y);

    g2.drawString("Spaceship", 160+x, 180+y);

  }

      public void actionPerformed(ActionEvent e){

        x = x+velx;

        y = y+vely;

        repaint();

    }

    public void up(){

        vely=-1;

        velx=0;

    }

    public void down(){

        vely=1;

        velx=0;

    }

    public void left(){

        vely=0;

        velx=-1;

    }

    public void right(){

        vely=0;

        velx=1;

        }

    public void enlarge(){

        x+=width*2;

        y+=height*2;

    }

   @Override

    public void keyPressed(KeyEvent e){

        int code = e.getKeyCode();

        if (code == KeyEvent.VK_L){

            up();

        }

    if (code == KeyEvent.VK_U){

           down();

}

     if (code == KeyEvent.VK_R){

           left();

}

      if (code == KeyEvent.VK_D){

           right();

      }

       if (code == KeyEvent.VK_Q){

           enlarge();}

    }

   @Override

    public void keyTyped(KeyEvent e) {}

   @Override

    public void keyReleased(KeyEvent e)

    {

        velx=0;

        vely=0;

    }

    public static void main(String[] args) {

        JFrame jp1 = new JFrame();

        MovingSpace a = new MovingSpace();

        a.setFocusable(true);

        a.requestFocusInWindow();

        jp1.getContentPane().add(a, BorderLayout.CENTER);

        jp1.setSize(new Dimension(500,400));

        jp1.setVisible(true);

        jp1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

   private void setFocusTraversalKeysEnable(boolean b) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public void mouseClicked(MouseEvent e) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public void mousePressed(MouseEvent e) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public void mouseReleased(MouseEvent e) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public void mouseEntered(MouseEvent e) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public void mouseExited(MouseEvent e) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public void mouseDragged(MouseEvent e) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    public void mouseMoved(MouseEvent e) {

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    private static class keyEvent {

        public keyEvent() {

        }

        private int getKeycode() {

            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

        }

    }

    private static class Public {

        public Public() {

        }

    }

}