Objective: Using the provided code, write a Java program that simulates the movement of different types of robots using

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Objective: Using the provided code, write a Java program that simulates the movement of different types of robots using

Post by answerhappygod »

Objective:
Using the provided code, write a Java program that simulates themovement of different types of robots using multiple threads on agraphical user interface (GUI). There are 4 types of robots (simple(provided), left and right, diagonal, and wavy) that must updatetheir position on a separate thread, and their movement isdetermined by the type of robot detailed in the requirements.
Requirements:
X = X + Speed_X
Y = Initial_Y + SIN(X * Period) * Amplitude
Provided code:
import java.lang.Thread;import java.awt.*;
public class SimpleRobot extends Thread //Extends (inheritance)the functionality of Thread in order to update the Robot'sposition{private int x,y;//Location of the Robotprivate Color rColor = Color.DARK_GRAY;//Default Colorpublic static final int ROBOT_SIZE = 15;//Robot's are 15 pixelcirclespublic static final int TIME_DELAY = 30;//Update is called every 30milliseconds.public SimpleRobot(int aX, int aY, Color aC){this.setX(aX);this.setY(aY);this.setrColor(aC);}public int getX() {return x;}
public void setX(int x) {this.x = x;}
public int getY() {return y;}
public void setY(int y) {this.y = y;}
public Color getrColor() {return rColor;}
public void setrColor(Color rColor) {this.rColor = rColor;}/*** Overrides the method run in Thread. This calls the method updateand sleeps the thread for 30 milliseconds before calling updateagain.*/public void run(){while(true){//Calls update on each robotthis.update();//Sleeps this thread for 30 milliseconds before updatingagaintry {Thread.sleep(TIME_DELAY);}catch(Exception e){e.printStackTrace();}}}/*** This updates the position of the robot based on the robot'stype.* This is meant to be overridden for other extended robottypes.*/public void update(){//TODO determine how the robot will move. This type of robot doesnot move but other may need to override this, and set the X and Ycoordinates.}}
Provided code:
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;
public class RobotThreadSimulator{public static final int FRAME_DIM = 512; //Image dimension assumedto be fixedpublic static RobotPanel rP = new RobotPanel();//Creates a newRobot Panel to be added to the Frame.public static JFrame frame = new JFrame("Robot ThreadSimulation");public static final int FRAME_TIMER_DELAY = 10;//10millisecondspublic static void main(String[] args) {//Frame for the programframe.setLayout(new FlowLayout());//Simple Layout for theComponentsframe.setBounds(0,0,FRAME_DIM,FRAME_DIM);//Initializes the Robot PanelrP.init();//Adds it to the frameframe.add(rP);//Ensures that it closes all threads after the frame isclosedframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Now we can see it!frame.setVisible(true);//Begin AnimatingstartAnimation();}/*** Used to call repaint on the frame using a timer (another thread)with a delay.*/public static void startAnimation(){//Sets up an ActionListener to be used by the TimerActionListener al = new ActionListener(){//Overrides ActionListener's method actionPerformed using anAnonymous Classpublic void actionPerformed(ActionEvent e){frame.repaint();//Calls repaint on the Frame to redraw it and itscomponents}};//Creates a timer with the timer delay and then actionlistenerTimer timer = new Timer(FRAME_TIMER_DELAY,al);//Starts the timer threadtimer.start();}
}
Provided code:
import java.awt.*;import javax.swing.*;
public class RobotPanel extends JPanel //Extends (inheritance)the functionality of the JPanel Component in order to drawRobots.{public static final int DEF_R_SIZE = 8; //Default number ofRobotsprivate SimpleRobot[] robots = new SimpleRobot[DEF_R_SIZE];//Creates the array of robots to be updated and drawn/*** Initializes elements of the JPanel. This method is to be calledbefore added to a JFrame.*/public void init(){//Set's the JPanel's Preferred Size to be the same as theFrame.super.setPreferredSize(newDimension(RobotThreadSimulator.FRAME_DIM,RobotThreadSimulator.FRAME_DIM));//TODO Add Robots to the Array//2 Simple Robots
//2 Left Right Robots
//2 Diagonal Robots
//2 Wavy Robots
//See assignment requirements for more details//TODO Start each robot thread
}/***Overrides JPanel's paintComponent method in order to draw eachrobot.*/public void paintComponent(Graphics g){//Calling super class' paintComponents firstsuper.paintComponent(g);//For each robot in the array of robotsfor(SimpleRobot r : robots){if(r == null)continue;//Sets the drawing colorg.setColor(r.getrColor());//Draws the oval to the JPanelg.fillOval(r.getX(),r.getY(),SimpleRobot.ROBOT_SIZE,SimpleRobot.ROBOT_SIZE);}}}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply