Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

DisplayWindow import java.awt.*; import javax.swing.*; /** * A class that puts a

ID: 3909272 • Letter: D

Question

DisplayWindow

import java.awt.*;
import javax.swing.*;
/**
* A class that puts a graphics window on your display
*/
public class DisplayWindow extends JFrame{
/**
* Content pane that will hold the added Jpanel
*/
private Container c;
/**
* DisplayWindow constructor - no parameters
*/
public DisplayWindow(){
super("Display");
c = this.getContentPane();
}

public DisplayWindow(String titleText){
super(titleText);
c = this.getContentPane();
}
  
/**
* Adds panel to content pane
* @parameter the panel to be added
*/
public void addPanel(JPanel p){
c.add(p);
}
/**
* consolidates the frame, makes it visible,
* causes program termination when window is closed manually
*/
public void showFrame(){
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

------------------------------------------------------------------------------------------------------------------------------------

SaucerDriver

/** The driver class that instantiates the DisplayWindow
* and SaucerPanel, adds the panel to the window, then
* makes the call to showFrame to display the panel.
*/
public class SaucerDriver{

public static void main(String[] args){
DisplayWindow d = new DisplayWindow("Saucer");
SaucerPanel p = new SaucerPanel();
d.addPanel(p);
d.showFrame();
}
}

---------------------------------------------------------------------------------------------------------------

SaucerPanel

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

public class SaucerPanel extends JPanel implements ActionListener{
//environmental features
private final static int panelWidth=500;
private final static int panelHeight=400;
private final static int groundHeight = 50;
private final static int groundYpos = panelHeight-groundHeight;
private final static Color groundColor=new Color(0, 128, 0);
private final static Color skyColor = new Color(182, 227, 252);
private final static Color buildingColor = Color.lightGray;
//building one
private final static int buildingOneXpos=200;
private final static int buildingOneYpos=200;
private final static int buildingOneHeight=150;
private final static int buildingOneWidth=30;
//building two
private final static int buildingTwoXpos=350;
private final static int buildingTwoYpos=250;
private final static int buildingTwoHeight=100;
private final static int buildingTwoWidth=15;
//saucer
private final static int saucerWidth = 90;
private final static int saucerHeight = 40;
//how far to move the saucer each tick
private final static int saucerMoveIncrement = 5;
private final static int initialSaucerXpos = 10;
private final static int initialSaucerYpos = 50;
private final static Color saucerColor = Color.black;
private String saucerHoveringStatus = "hovering...";
private String saucerLandedStatus = "landed!";
private String saucerStatus = saucerHoveringStatus;
private int saucerXpos = initialSaucerXpos;
private int saucerYpos = initialSaucerYpos;
private boolean hovering = true;// state of saucer
private String direction; //direction of saucer
//GUI components
private JButton upBtn = new JButton("Up");
private JButton downBtn = new JButton("Down");
private JButton leftBtn = new JButton("Left");
private JButton rightBtn = new JButton("Right");
/**********************************************
** TODO 2: add the Reset button so its fully functional
**********************************************/
// used to generate random dots as "debris" from a crash
private Random r = new java.util.Random();
// Timer object with a delay of 100ms
private Timer timer = new Timer(100,this);

public SaucerPanel(){
setPreferredSize(new Dimension(panelWidth, panelHeight));
setBackground(skyColor);
setOpaque(true);
this.add(upBtn);  
this.add(downBtn);
this.add(leftBtn);
this.add(rightBtn);
upBtn.addActionListener(this);
downBtn.addActionListener(this);
leftBtn.addActionListener(this);
rightBtn.addActionListener(this);
}

public void paintComponent(Graphics g){
super.paintComponent(g);
drawGround(g);
drawBuildings(g);
if (hovering)
drawSaucer(g, saucerXpos, saucerYpos);
else
drawCrash(g);
}
  
private void drawSaucer(Graphics g, int xPos, int yPos){
g.setColor(saucerColor);
g.fillOval(xPos, yPos, saucerWidth, saucerHeight);
//15pixels added to yPos+saucerHeight to make status appear under saucer
g.drawString(saucerStatus, xPos, (yPos+saucerHeight+15));
}
  
private void drawBuildings(Graphics g){
g.setColor(buildingColor);
g.fillRect(buildingOneXpos, buildingOneYpos, buildingOneWidth, buildingOneHeight);
g.fillRect(buildingTwoXpos, buildingTwoYpos, buildingTwoWidth, buildingTwoHeight);
}
  
private void drawGround(Graphics g){
g.setColor(groundColor);
g.fillRect(0, groundYpos, panelWidth, groundHeight);
}
  
//draw a bunch of points to simulate debris
private void drawCrash(Graphics g){
g.setColor(Color.black);
for(int i = 0; i < 400; i++)
ranPoints(10,350,g);
}
  
//generates the debris
private void ranPoints(int x, int y, Graphics g){
int xLoc = r.nextInt(500);
int yLoc = y - r.nextInt(350);
g.fillOval(xLoc,yLoc,6,6);
}

// true if the saucer landed between the buildings
private boolean landedInBounds(int xPos){
return(xPos>=(buildingOneXpos+buildingOneWidth)
&& xPos<= (buildingTwoXpos-saucerWidth));
}
  
// test if the saucer touches either building
private boolean saucerTouchingBuilding(int xPos, int yPos){
boolean result=false;
//calculate the saucer "boundary" points:
int sauceRx=xPos+saucerWidth;
int sauceLx=xPos;
int sauceLy=yPos+saucerHeight;
int sauceUy=yPos;
// building 1
if((buildingOneXpos<sauceRx && buildingOneXpos>sauceLx) && (sauceLy>=buildingOneYpos) )
result = true;
if((buildingOneXpos<sauceRx && (buildingOneXpos+buildingOneWidth)>sauceLx) && (sauceLy>=buildingOneYpos) )
result = true;
// building 2
if((buildingTwoXpos<sauceRx && buildingTwoXpos>sauceLx) && (sauceLy>=buildingTwoYpos) )
result = true;
if((buildingTwoXpos<sauceRx && (buildingTwoXpos+buildingTwoWidth)>sauceLx) && (sauceLy>=buildingTwoYpos) )
result = true;
return result;
}
  
public void actionPerformed(ActionEvent e){
/**********************************************
******* TODO 1: implement this method body *****
**********************************************/
}
}

Overvievw In the saucer game, the player moves a flying saucer in one of four directions in an attempt to land it on the ground between two buildings- the landing area. The saucer crashes if it touches the ground anywhere outside of the landing area between the two buildings or if it touches the top or either side of the buildings, or if it touches the top, right or left-hand sides of the panel. The saucer starts in the "hovering" state, and ends in either the "landed" or "crashed" state. Saucer Up Down LftightReset hovering...

Explanation / Answer

I have implemented the SaucerPanel TODO sections. Please let me know if you have any issue

public class SaucerPanel extends JPanel implements ActionListener {

// environmental features

private final static int panelWidth = 500;

private final static int panelHeight = 400;

private final static int groundHeight = 50;

private final static int groundYpos = panelHeight - groundHeight;

private final static Color groundColor = new Color(0, 128, 0);

private final static Color skyColor = new Color(182, 227, 252);

private final static Color buildingColor = Color.lightGray;

// building one

private final static int buildingOneXpos = 200;

private final static int buildingOneYpos = 200;

private final static int buildingOneHeight = 150;

private final static int buildingOneWidth = 30;

// building two

private final static int buildingTwoXpos = 350;

private final static int buildingTwoYpos = 250;

private final static int buildingTwoHeight = 100;

private final static int buildingTwoWidth = 15;

// saucer

private final static int saucerWidth = 90;

private final static int saucerHeight = 40;

// how far to move the saucer each tick

private final static int saucerMoveIncrement = 5;

private final static int initialSaucerXpos = 10;

private final static int initialSaucerYpos = 50;

private final static Color saucerColor = Color.black;

private String saucerHoveringStatus = "hovering...";

private String saucerLandedStatus = "landed!";

private String saucerStatus = saucerHoveringStatus;

private int saucerXpos = initialSaucerXpos;

private int saucerYpos = initialSaucerYpos;

private boolean hovering = true;// state of saucer

private String direction; // direction of saucer

// GUI components

private JButton upBtn = new JButton("Up");

private JButton downBtn = new JButton("Down");

private JButton leftBtn = new JButton("Left");

private JButton rightBtn = new JButton("Right");

private JButton resetBtn = new JButton("Reset");

/**********************************************

** TODO 2: add the Reset button so its fully functional

**********************************************/

// used to generate random dots as "debris" from a crash

private Random r = new java.util.Random();

// Timer object with a delay of 100ms

private Timer timer = new Timer(100, this);

public SaucerPanel() {

setPreferredSize(new Dimension(panelWidth, panelHeight));

setBackground(skyColor);

setOpaque(true);

this.add(upBtn);

this.add(downBtn);

this.add(leftBtn);

this.add(rightBtn);

this.add(resetBtn);

upBtn.addActionListener(this);

downBtn.addActionListener(this);

leftBtn.addActionListener(this);

rightBtn.addActionListener(this);

resetBtn.addActionListener(this);

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

drawGround(g);

drawBuildings(g);

if (hovering)

drawSaucer(g, saucerXpos, saucerYpos);

else

drawCrash(g);

}

private void drawSaucer(Graphics g, int xPos, int yPos) {

g.setColor(saucerColor);

g.fillOval(xPos, yPos, saucerWidth, saucerHeight);

// 15pixels added to yPos+saucerHeight to make status appear under

// saucer

g.drawString(saucerStatus, xPos, (yPos + saucerHeight + 15));

}

private void drawBuildings(Graphics g) {

g.setColor(buildingColor);

g.fillRect(buildingOneXpos, buildingOneYpos, buildingOneWidth, buildingOneHeight);

g.fillRect(buildingTwoXpos, buildingTwoYpos, buildingTwoWidth, buildingTwoHeight);

}

private void drawGround(Graphics g) {

g.setColor(groundColor);

g.fillRect(0, groundYpos, panelWidth, groundHeight);

}

// draw a bunch of points to simulate debris

private void drawCrash(Graphics g) {

g.setColor(Color.black);

for (int i = 0; i < 400; i++)

ranPoints(10, 350, g);

}

// generates the debris

private void ranPoints(int x, int y, Graphics g) {

int xLoc = r.nextInt(500);

int yLoc = y - r.nextInt(350);

g.fillOval(xLoc, yLoc, 6, 6);

}

// true if the saucer landed between the buildings

private boolean landedInBounds(int xPos) {

return (xPos >= (buildingOneXpos + buildingOneWidth) && xPos <= (buildingTwoXpos - saucerWidth));

}

// test if the saucer touches either building

private boolean saucerTouchingBuilding(int xPos, int yPos) {

boolean result = false;

// calculate the saucer "boundary" points:

int sauceRx = xPos + saucerWidth;

int sauceLx = xPos;

int sauceLy = yPos + saucerHeight;

int sauceUy = yPos;

// building 1

if ((buildingOneXpos < sauceRx && buildingOneXpos > sauceLx) && (sauceLy >= buildingOneYpos))

result = true;

if ((buildingOneXpos < sauceRx && (buildingOneXpos + buildingOneWidth) > sauceLx)

&& (sauceLy >= buildingOneYpos))

result = true;

// building 2

if ((buildingTwoXpos < sauceRx && buildingTwoXpos > sauceLx) && (sauceLy >= buildingTwoYpos))

result = true;

if ((buildingTwoXpos < sauceRx && (buildingTwoXpos + buildingTwoWidth) > sauceLx)

&& (sauceLy >= buildingTwoYpos))

result = true;

return result;

}

public void actionPerformed(ActionEvent e) {

/**********************************************

******* TODO 1: implement this method body *****

**********************************************/

if (e.getSource() == timer) {

if (direction == "right") {

saucerXpos += saucerMoveIncrement;

}

if (direction == "left") {

saucerXpos -= saucerMoveIncrement;

}

if (direction == "up") {

saucerYpos -= saucerMoveIncrement;

}

if (direction == "down") {

saucerYpos += saucerMoveIncrement;

}

if ((saucerXpos == 0) || (saucerXpos + saucerWidth == 500)) {

hovering = false;

}

if (saucerYpos == 0) {

hovering = false;

}

if (saucerYpos + saucerHeight == 350) {

if (landedInBounds(saucerXpos) == true) {

direction = null;

saucerStatus = saucerLandedStatus;

} else {

hovering = false;

}

}

if (saucerTouchingBuilding(saucerXpos, saucerYpos) == true) {

hovering = false;

}

}

if (e.getSource() == rightBtn) {

direction = "right";

}

if (e.getSource() == leftBtn) {

direction = "left";

}

if (e.getSource() == upBtn) {

direction = "up";

}

if (e.getSource() == downBtn) {

direction = "down";

}

if (e.getSource() == resetBtn) {

direction = null;

saucerXpos = 10;

saucerYpos = 50;

saucerStatus = saucerHoveringStatus;

hovering = true;

}

repaint();

}

}