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

Please help me make the fan and clock rotate in Java. The fan should have a star

ID: 3759819 • Letter: P

Question

Please help me make the fan and clock rotate in Java. The fan should have a start and stop button and the clock should constantly rotate with time. The code that I have so far is below. The clock already displays actuate time when you initially run the program but it does not continue to rotate.


import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;

public class Motion extends JFrame {

// Fan class
public static class ArcsPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);

int x = xCenter - radius;
int y = yCenter - radius;

g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
}
}

// Clock class
public static class StillClock extends JPanel {
private int hour;
private int minute;
private int second;

/** Construct a default clock with the current time*/
public StillClock() {
setCurrentTime();
}

/** Construct a clock with specified hour, minute, and second */
public StillClock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}

/** Return hour */
public int getHour() {
return hour;
}

/** Set a new hour */
public void setHour(int hour) {
this.hour = hour;
repaint();
}

/** Return minute */
public int getMinute() {
return minute;
}

/** Set a new minute */
public void setMinute(int minute) {
this.minute = minute;
repaint();
}

/** Return second */
public int getSecond() {
return second;
}

/** Set a new second */
public void setSecond(int second) {
this.second = second;
repaint();
}

@Override /** Draw the clock */
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Initialize clock parameters
int clockRadius =
(int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;

// Draw circle
g.setColor(Color.black);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
2 * clockRadius, 2 * clockRadius);
g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);

// Draw second hand
int sLength = (int)(clockRadius * 0.8);
int xSecond = (int)(xCenter + sLength *
Math.sin(second * (2 * Math.PI / 60)));
int ySecond = (int)(yCenter - sLength *
Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);

// Draw minute hand
int mLength = (int)(clockRadius * 0.65);
int xMinute = (int)(xCenter + mLength *
Math.sin(minute * (2 * Math.PI / 60)));
int yMinute = (int)(yCenter - mLength *
Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);

// Draw hour hand
int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter + hLength *
Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter - hLength *
Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.green);
g.drawLine(xCenter, yCenter, xHour, yHour);
}

public void setCurrentTime() {
// Construct a calendar for the current date and time
Calendar calendar = new GregorianCalendar();

// Set current hour, minute and second
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}

// Make panels to hold elements
public static JPanel leftPanel = new JPanel();
public static JPanel rightPanel = new JPanel();
public static JPanel buttonPanel = new JPanel();
public static ArcsPanel fanPanel = new ArcsPanel();
public static StillClock clockPanel = new StillClock();

// Make buttons and label
public static JButton start = new JButton("Start");
public static JButton stop = new JButton("Stop");
public static JLabel digitalTime = new JLabel("12:32 PM");

// Make timers
// public static Timer fanTimer = new Timer(1, TimerListener());
//public static Timer clockTimer = new Timer(1000, TimerListener());

public static void assembleGUI() {
fanPanel.setPreferredSize(new Dimension(200, 200));
clockPanel.setPreferredSize(new Dimension(200, 200));
leftPanel.setLayout(new BorderLayout());
rightPanel.setLayout(new BorderLayout());
buttonPanel.setLayout(new BorderLayout());
buttonPanel.add(start, BorderLayout.WEST);
buttonPanel.add(stop, BorderLayout.EAST);
leftPanel.add(fanPanel, BorderLayout.NORTH);
leftPanel.add(buttonPanel, BorderLayout.SOUTH);
rightPanel.add(clockPanel, BorderLayout.NORTH);
digitalTime.setHorizontalAlignment(JLabel.CENTER);
rightPanel.add(digitalTime, BorderLayout.SOUTH);
}

/*
public static class clockTimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {

}
}

*/

public static void main(String[] args) {
Motion frame = new Motion();
frame.setTitle("Fan : Clock");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
assembleGUI();
frame.setLayout(new BorderLayout());
frame.add(leftPanel, BorderLayout.WEST);
frame.add(rightPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
}

Explanation / Answer

import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
public class Motion extends JFrame {
// Fan class
public static class ArcsPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
int x = xCenter - radius;
int y = yCenter - radius;
g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
}
}
// Clock class
public static class StillClock extends JPanel {
private int hour;
private int minute;
private int second;
/** Construct a default clock with the current time*/
public StillClock() {
setCurrentTime();
}
/** Construct a clock with specified hour, minute, and second */
public StillClock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
/** Return hour */
public int getHour() {
return hour;
}
/** Set a new hour */
public void setHour(int hour) {
this.hour = hour;
repaint();
}
/** Return minute */
public int getMinute() {
return minute;
}
/** Set a new minute */
public void setMinute(int minute) {
this.minute = minute;
repaint();
}
/** Return second */
public int getSecond() {
return second;
}
/** Set a new second */
public void setSecond(int second) {
this.second = second;
repaint();
}
@Override /** Draw the clock */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Initialize clock parameters
int clockRadius =
(int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
// Draw circle
g.setColor(Color.black);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
2 * clockRadius, 2 * clockRadius);
g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);
// Draw second hand
int sLength = (int)(clockRadius * 0.8);
int xSecond = (int)(xCenter + sLength *
Math.sin(second * (2 * Math.PI / 60)));
int ySecond = (int)(yCenter - sLength *
Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);
// Draw minute hand
int mLength = (int)(clockRadius * 0.65);
int xMinute = (int)(xCenter + mLength *
Math.sin(minute * (2 * Math.PI / 60)));
int yMinute = (int)(yCenter - mLength *
Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);
// Draw hour hand
int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter + hLength *
Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter - hLength *
Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.green);
g.drawLine(xCenter, yCenter, xHour, yHour);
}
public void setCurrentTime() {
// Construct a calendar for the current date and time
Calendar calendar = new GregorianCalendar();
// Set current hour, minute and second
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}

// Make panels to hold elements
public static JPanel leftPanel = new JPanel();
public static JPanel rightPanel = new JPanel();
public static JPanel buttonPanel = new JPanel();
public static ArcsPanel fanPanel = new ArcsPanel();
public static StillClock clockPanel = new StillClock();
// Make buttons and label
public static JButton start = new JButton("Start");
public static JButton stop = new JButton("Stop");
public static JLabel digitalTime = new JLabel("12:32 PM");
// Make timers
// public static Timer fanTimer = new Timer(1, TimerListener());
//public static Timer clockTimer = new Timer(1000, TimerListener());
public static void assembleGUI() {
fanPanel.setPreferredSize(new Dimension(200, 200));
clockPanel.setPreferredSize(new Dimension(200, 200));
leftPanel.setLayout(new BorderLayout());
rightPanel.setLayout(new BorderLayout());
buttonPanel.setLayout(new BorderLayout());
buttonPanel.add(start, BorderLayout.WEST);
buttonPanel.add(stop, BorderLayout.EAST);
leftPanel.add(fanPanel, BorderLayout.NORTH);
leftPanel.add(buttonPanel, BorderLayout.SOUTH);
rightPanel.add(clockPanel, BorderLayout.NORTH);
digitalTime.setHorizontalAlignment(JLabel.CENTER);
rightPanel.add(digitalTime, BorderLayout.SOUTH);
}
/*
public static class clockTimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {

}
}

*/
public static void main(String[] args) {
Motion frame = new Motion();
frame.setTitle("Fan : Clock");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
assembleGUI();
frame.setLayout(new BorderLayout());
frame.add(leftPanel, BorderLayout.WEST);
frame.add(rightPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
}

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