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

I need help with the following assignment: In the assignment you will gain exper

ID: 3821505 • Letter: I

Question

I need help with the following assignment:

In the assignment you will gain experience with the following:

Swing

Event handling in Swing

Swing Timers

There are 2 different interfaces you will need for handling mouse events.

As noted above you need to have the turtle start/stop moving based on:

Whether it is already at the position of the mouse

If the mouse pointer is in the drawing area. This latter one can be handled by using the Point object that the TurtleMouseFollower has.

In my implementation I initially set it to null and reset it to nullwhen the mouse exits the drawing area.

cop2513.turtlemoussefollow: Main +main(StringO args): void edu support: EndWorld cop2513.turtlemousefollow: TurtleMouseFollower edu.gatech.mediacomp: Turtle -FORWARD PIXELS: int +TurtleMouseFollower(): ctor javax.swing: Timer java awt: Point

Explanation / Answer

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

public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showEventDemo();   
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));

headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
  
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());

mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Control in action: Button");

JButton okButton = new JButton("OK");
JButton submitButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");

okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");

okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());

controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);   

mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

if( command.equals( "OK" )) {
statusLabel.setText("Ok Button clicked.");
} else if( command.equals( "Submit" ) ) {
statusLabel.setText("Submit Button clicked.");
} else {
statusLabel.setText("Cancel Button clicked.");
}    
}      
}
}