Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the code provided below. The first steps being to create classes as listed. a. The first class would be FlyingObject (Eclipse will put this

Using the code provided below. The first steps being to create classes as listed. a. The first class would be FlyingObject (Eclipse will put this class in the file FlyingObject.java). This is a base class for different components which fly. b. The second class would be TIEFighter (Eclipse will put this class in the file TIEFighter.java). So TIEFighter class should inherit properties from FlyingObject class. c. The third class would be Galaxy (Eclipse will put this class in the file Galaxy.java). Here we will portray of war of the galaxy. [This java is provided below] d. The fourth class would be be StarWars (Eclipse will put this class in the file StarWars.java). This class will launch the game. [This java is provided below] Additionally Part 1 and Part 2 listed below

image text in transcribed

image text in transcribed The goal being only to create 10 TIE Fighters on the screen which will appear randomly inside the gaming panel and start to move from right to left. The Image below being the TIE Fighters

image text in transcribed ----------------------------------------StarWars.java--------------------------------------------------

import java.awt.EventQueue; import javax.swing.JFrame;

public class StarWars extends JFrame {

public StarWars() {

initUI(); }

private void initUI() {

add(new Galaxy());

setResizable(false); pack();

setTitle("A Star War Game"); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

public static void main(String[] args) {

EventQueue.invokeLater(() -> { StarWars ex = new StarWars(); ex.setVisible(true); }); } }

----------------------------------------Galaxy.java--------------------------------------------------

/* University of Central Florida * COP 3330 Spring 2019 * Assignment #2 * Author: */

import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import javax.swing.JPanel; import javax.swing.Timer; import java.util.Random;

public class Galaxy extends JPanel implements ActionListener {

private Timer timer; private boolean inGame; private final int B_WIDTH = 500; private final int B_HEIGHT = 400; private final int DELAY = 15;

// START YOUR CODE HERE -- Declare or INITIALIZE REQUIRED VARIABLES, such as ArrayList, RandomNumber, etc. // HINT: ~ approx. 3 lines of code

// END YOUR CODE HERE

public Galaxy() {

initGalaxy(); }

private void initGalaxy() {

setFocusable(true); setBackground(Color.BLACK); inGame = true;

setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT)); initFighter();

timer = new Timer(DELAY, this); timer.start(); }

public void initFighter() {

// START YOUR CODE HERE -- INSTANTIATE THE TIEFIGHTER ARRAYLIST, EACH FIGHTER APPEAR RANDOMLY (X COORDINATE BETWEEN 0 TO B_WIDTH, Y COORDINATE BETWEEN 0 TO B_HEIGHT) // HINT: ~ approx. 6-10 lines of code

//END YOUR CODE HERE

}

@Override public void paintComponent(Graphics g) { super.paintComponent(g);

int visibleAliens = 0; if (inGame) {

drawObjects(g);

} else { for (TIEFighter alien : fighters) { if (alien.isVisible()) { visibleAliens++; } }

}

Toolkit.getDefaultToolkit().sync(); }

private void drawObjects(Graphics g) {

for (TIEFighter alien : fighters) { if (alien.isVisible()) { g.drawImage(alien.getImage(), alien.getX(), alien.getY(), this); } }

g.setColor(Color.WHITE); g.drawString("Fighters Present: " + fighters.size(), 5, 15); }

@Override public void actionPerformed(ActionEvent e) {

inGame(); updateFighters(); repaint(); }

private void inGame() {

if (!inGame) { timer.stop(); } }

private void updateFighters() {

if (fighters.isEmpty()) {

inGame = false; return; }

for (int i = 0; i

TIEFighter a = fighters.get(i);

if (a.isVisible()) { a.move(); } else { fighters.remove(i); } } }

}

From the implementation point of view, here are certain points which could be helpful: 1. The base class FlyingObject has the following features: a. X coordinate position from where it will appear. (int) b. Y coordinate position from where it will appear. (int) c. Height of the fiying object (int) d. Width of the flying object (int) e. A Boolean indicating whether the object is visible or not f. An image for representing the object. (Ilmage). Please import appropriate library g Aconstructor which can take x and y positions and use them to initialize X, and Y h. A method which will load and set the image with the specified image for that to use Image type. position. The constructor also set the visible to true to make the object visible. flying object. Hint: Please make use of java.awt.lmage' and 'javax.swing.Imagelcon' external libraries for image operations. A method which will obtain the the height and width of the associated image and set the height and weight variable. You have to use the image-getHeightl0 and similar width method to obtain that properties I. The class diagram looks like the following (-means private. means public, and " means protected x: int y: int width: int height: int visible: Boolean ecconstructorn>FlyingObject (x: int, y: int) String): void #extra getimage): Image getx(): int gety): int isVisible(): Boolean 2. The TIEFighter class will Inherit all the properties from FlyingObject, along with the extras: a. A final int variable INITIAL-X 500. It represent the maxium value of the x exis. b. A method initFighter) which uses the loadlmage() method to set an image name. As all the TIEFighter should look same, an image name is passed from this method to the loadimage) method. It also calls the extractimageDimensions) method after initializing the image and obtain the image dimensions for the fighter A constructor which will initialize each fighter A method which will move the fighter from right to left in a horizontal straight line. When a fighter will touch left boundary of the game panel, it must C. d. disappear from there and reappear from the right boundary of the game panel. You have to check if x becomes less than 0, re-initialize x to INTIAL-X. final INITIAL X: int

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions