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

I need help with my swift homework that I sorta understand but still struggling

ID: 3739384 • Letter: I

Question

I need help with my swift homework that I sorta understand but still struggling with it. Essentially, when the lid is closed, there is no color in the box (light blue). When the lid is opening, the button turns clockwise and color (inside the box) fades in. When the lid is closing, the buttons turns counter clockwise and the color (insde the box) fades out. Only the lid and the button are supposed to move. The lid movement states are only activated by clicking inside of the button. Any help/suggestions are welcomed.

There are four sections of code you need to write, they are marked with a comment ("// FIX #. ADD CODE HERE")
in the update function, in Scene class, in ViewController.swift.
in the checkForButtonPress function in Scene class, in ViewController.swift.
in the render function in the Box class, in Box.swift.
in the nextState function in the Box class, in Box.swift.

//

// ViewController.swift

//

import Cocoa

import Tin

class ViewController: TController {

   

   var scene: Scene!

   override func viewWillAppear() {

       super.viewWillAppear()

       

       view.window?.title = "The Hatch"

       makeView(width: 1000.0, height: 600.0)

       scene = Scene()

       present(scene: scene)

       scene.view?.showStats = false

   }

   

   

   override func mouseUp(with event: NSEvent) {

       scene.checkForButtonPress()

   }

   

}




class Scene: TScene {

   // scene properties here

   var box = Box()

   var button = CircleButton(x: 800, y: 300, radius: 30)

  

   override func update() {

       background(gray: 0.5)

       

       // FIX 1. ADD CODE HERE ****************************************

       // What is needed here:

       // Update the box lid position

       // Render the box

       // If the box is opening, or closing, update the button

       // Render the button

       

       box.render()

       button.render()

       if box.isClosing() == true //unsure of this part?

       {

           button.updateClosing()

       }

       

       

       

   }

   // In this function, check to see if the mouse position is inside

   // the button, if it is, advance the box to its next lid state.

   func checkForButtonPress() {

       

       // FIX 4. ADD CODE HERE ****************************************

       // What is needed here:

       // Test to see if the mouse position is inside the button.

       // Change the box lid to its next lid state.

  

       

   }

   

   

}

Box.swift

//

// Box.swift

// TheHatch

//

//

import Foundation

import Tin


// Possible states for the Box lid

enum LidState {

   case closed

   case opening

   case open

   case closing

}


class Box {

   var x = 400.0

   var y = 150.0

   var width = 200.0

   var wallThickness = 20.0

   var angle = 0.0

   var state = LidState.closed

   var color = 0.7

   

   

   // Update the movement of the lid.

   func update() {

       if state == .opening {

           angle += 0.1

           if angle >= .pi {

               angle = .pi

               nextState()

           }

       }

       else if state == .closing {

           angle -= 0.1

           if angle <= 0.0 {

               angle = 0.0

               nextState()

           }

       }

   }

   

   

   // Render the box and the lid.

   func render() {

       // FIX 2. ADD CODE HERE ****************************************

       // What is needed here:

       // Draw the colored background of the box.

       // Draw the left, bottom, and right edges of box.

       // Draw the box lid. (remember, it rotates!)

       

       rect(x: x, y: y, width: width, height: wallThickness) //bottom

       rect(x: x, y: 170.0, width: wallThickness, height: width) //left

       rect(x: x + width - 20, y: 170.0, width: wallThickness, height: width) //right

   }

   

   

   // Change the box to the next state. The possible state transitions are:

   // closed to opening

   // opening to open

   // open to closing

   // closing to closed

   func nextState() {

       

       // FIX 3. ADD CODE HERE ****************************************

       // What is needed here:

       // Change the state property, to transition it to its next value.

   }

   

   

   func isOpening() -> Bool {

       return state == .opening

   }

   

   

   func isClosing() -> Bool {

       return state == .closing

   }

}


CircleButton.swift

//

// CircleButton.swift

// TheHatch

//

//

import Foundation

import Tin


// This class represents a button, shaped like a circle, which

// can rotate in either direction.

class CircleButton {

   var x: Double

   var y: Double

   var radius: Double

   var angle: Double

   var speed: Double

   

   // Initializer.

   // Inputs are the position and radius of the button.

   init(x: Double, y: Double, radius: Double) {

       self.x = x

       self.y = y

       self.radius = radius

       angle = 0.0

       speed = 0.08

   }

   

   

   // Rotate clockwise when opening.

   func updateOpening() {

       angle -= speed

   }

   

   

   // Rotate counter-clockwise when closing.

   func updateClosing() {

       angle += speed

   }

   

   

   // Draw the button.

   func render() {

       pushState()

       

       fillColor(gray: 0.8)

       strokeColor(gray: 0.1)

       translate(dx: x, dy: y)

       rotate(by: angle)

       ellipse(centerX: 0, centerY: 0, width: radius*2.0, height: radius*2.0)

       line(x1: 0, y1: -radius, x2: 0, y2: radius)

       line(x1: -radius, y1: 0, x2: radius, y2: 0)

       

       popState()

   }

   

   

   // Answers the question: is the input point inside the button?

   // Returns Bool value.

   func isPointInside(positionX: Double, positionY: Double) -> Bool {

       let d = dist(x1: x, y1: y, x2: positionX, y2: positionY)

       if d <= radius {

           return true

       }

       else {

           return false

       }

   }

}



Explanation / Answer

HERE IS THE CODE WITH UPDATION AND FIXING AS YOU WANT

//

// ViewController.swift

//

import Cocoa

import Tin

class ViewController: TController {

   

   var scene: Scene!

   override func viewWillAppear() {

       super.viewWillAppear()

       

       view.window?.title = "The Hatch"

       makeView(width: 1000.0, height: 600.0)

       scene = Scene()

       present(scene: scene)

       scene.view?.showStats = false

   }

   

   

   override func mouseUp(with event: NSEvent) {

       scene.checkForButtonPress()

   }

   

}




class Scene: TScene {

   // scene properties here

   var box = Box()

   var button = CircleButton(x: 800, y: 300, radius: 30)

  

   override func update() {

       background(gray: 0.5)

       

       // FIX 1. ADD CODE HERE ****************************************

Button b=new Button("List"); // you can use button or box,whatever you want

b.addViewController(new ViewController() {

public void view performed( ViewEvent event){

new box Thread().start();

}

)};

btnUpdate.addActionListener(new ActionListener(){

public void actionperformed(ActionEvent e){

int i= box.getselectedRow();

if(i>0)

{

model.setValueAt(textId.getText(),i,0);

else{

System.out.println("update error");

}frame.setvisible(true);

public StartBox()

{

if(Button b1= new Button("click");

b.add ActionListener(new ActionPerfromed() {

public void action performed( ActionEvent event){

super(" first box");

setDefaultCloseoperation(" frame.EXIT_ON_CLOSE);

b.addActionListener(this);

b.setActionCommand("open");

add(b);

}

else

{ super(" second Gui");

setDefaultCloseApplication(Frame.EXIT_ON_CLOSE);

add(new JLabel(" Empty JFrame" ));

setvisible(true);

}

       box.render()

       button.render()

       if box.isClosing() == true //unsure of this part?

       {

           button.updateClosing()

       }

       

       

       

   }

   // In this function, check to see if the mouse position is inside

   // the button, if it is, advance the box to its next lid state.

   func checkForButtonPress() {

       

       // FIX 4. ADD CODE HERE ****************************************

public void mouseEntered(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e)
{
x = e.getX();
y = e.getY();
str = "Mouse Exited";
repaint();
}

}

   

}

/

// Box.swift

// TheHatch

//

//

import Foundation

import Tin


// Possible states for the Box lid

enum LidState {

   case closed

   case opening

   case open

   case closing

}


class Box {

   var x = 400.0

   var y = 150.0

   var width = 200.0

   var wallThickness = 20.0

   var angle = 0.0

   var state = LidState.closed

   var color = 0.7

   

   

   // Update the movement of the lid.

   func update() {

       if state == .opening {

           angle += 0.1

           if angle >= .pi {

               angle = .pi

               nextState()

           }

       }

       else if state == .closing {

           angle -= 0.1

           if angle <= 0.0 {

               angle = 0.0

               nextState()

           }

       }

   }

   

   

   // Render the box and the lid.

   func render() {

       // FIX 2. ADD CODE HERE ****************************************

  public class MyComponent extends Component{
  public void paint(Graphics g){
  int height = 350;
  int width = 150;

g.drawRect(10,10,height,width);
}

}

       // Draw the left, bottom, and right edges of box.

       // Draw the box lid. (remember, it rotates!)

       

       rect(x: x, y: y, width: width, height: wallThickness) //bottom

       rect(x: x, y: 170.0, width: wallThickness, height: width) //left

       rect(x: x + width - 20, y: 170.0, width: wallThickness, height: width) //right

   }

   

   

   // Change the box to the next state. The possible state transitions are:

// closing to

   func nextState() {

       

       // FIX 3. ADD CODE HERE ****************************************

       // What is needed here:

       // Change the state property, to transition it to its next value.

class MyThread extends Thread

{

static String message[] =

{ "Java", "is", "basic."};

    public MyThread(String id)

    {

        super(id);

    }

    public void run()

    {

        String name = getName();

        for (int i=0;i<message.length;++i) {

           randomWait();

           System.out.println(name + message[i]);

        }

    }

    void randomWait()

    {

        try {

           sleep((long)(4000*Math.random()));

        } catch (InterruptedException x) {

           System.out.println("Error!");

        }

    }

}

   }

   

   

   func isOpening() -> Bool {

       return state == .opening

   }

   

   

   func isClosing() -> Bool {

       return state == .closing

   }

}

CircleButton.swift

//

// CircleButton.swift

// TheHatch

//

//

import Foundation

import Tin


// This class represents a button, shaped like a circle, which

// can rotate in either direction.

class CircleButton {

   var x: Double

   var y: Double

   var radius: Double

   var angle: Double

   var speed: Double

   

   // Initializer.

   // Inputs are the position and radius of the button.

   init(x: Double, y: Double, radius: Double) {

       self.x = x

       self.y = y

       self.radius = radius

       angle = 0.0

       speed = 0.08

   }

   

   

   // Rotate clockwise when opening.

   func updateOpening() {

       angle -= speed

   }

   

   

   // Rotate counter-clockwise when closing.

   func updateClosing() {

       angle += speed

   }

   

   

   // Draw the button.

   func render() {

       pushState()

       

       fillColor(gray: 0.8)

       strokeColor(gray: 0.1)

       translate(dx: x, dy: y)

       rotate(by: angle)

       ellipse(centerX: 0, centerY: 0, width: radius*2.0, height: radius*2.0)

       line(x1: 0, y1: -radius, x2: 0, y2: radius)

       line(x1: -radius, y1: 0, x2: radius, y2: 0)

       

       popState()

   }

   

   

   // Answers the question: is the input point inside the button? YES

   // Returns Bool value.

   func isPointInside(positionX: Double, positionY: Double) -> Bool {

       let d = dist(x1: x, y1: y, x2: positionX, y2: positionY)

       if d <= radius {

           return true

       }

       else {

           return false

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote