Hello. I tried to compile the code below, but recived the following error: Still
ID: 3762687 • Letter: H
Question
Hello. I tried to compile the code below, but recived the following error:
StillClock.java uses or overrides a deprecated API.
Recompile with -Xlint:deprecation for details.
PLEASE fix the error, and be sure that the code COMPILES and programme RUNS before sending it to me.
99 THANKS!!!!!!!
CODE:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.lang.Thread;
import java.util.Calendar;
import java.util.GregorianCalendar;
class DrawArcs extends JPanel {
private int angle1 = -15;
private int angle2 = 75;
private int angle3 = 165;
private int angle4 = 255;
public static int spinspeed;
public DrawArcs() {
Runnable spinner = new SpinDrawArcs();
final Thread thread1 = new Thread(spinner);
thread1.start();
JButton start = new JButton("On");
JButton high = new JButton("high");
JButton medium = new JButton("medium");
JButton low = new JButton("low");
JButton stop = new JButton("off");
add(start);
add(high);
add(medium);
add(low);
add(stop);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg) {
thread1.resume();
}
});
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg) {
thread1.suspend();
}
});
high.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg) {
spinspeed = 0;
}
});
medium.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg) {
spinspeed = 10;
}
});
low.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg) {
spinspeed = 150;
}
});
setSize(300, 300);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(30, 60, 250, 250);
g.fillArc(40, 70, 230, 230, angle1, 30);
g.fillArc(40, 70, 230, 230, angle2, 30);
g.fillArc(40, 70, 230, 230, angle3, 30);
g.fillArc(40, 70, 230, 230, angle4, 30);
}
class SpinDrawArcs implements Runnable {
public void run() {
try {
while (true) {
angle1 = (angle1 - 1) % 360;
angle2 = (angle2 - 1) % 360;
angle3 = (angle3 - 1) % 360;
angle4 = (angle4 - 1) % 360;
repaint();
Thread.sleep(DrawArcs.spinspeed);
}
} catch (InterruptedException ex) {
System.out.println("Problem while putting thread to sleep.");
}
}
}
}
class StillClock extends JPanel {
int hour;
int minute;
int second;
/** Construct a default clock with the current time */
public StillClock() {
setCurrentTime();
Runnable clock = new RunClock();
Thread thread1 = new Thread(clock);
thread1.start();
setSize(300, 300);
}
/** 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;
Runnable clock = new RunClock();
final Thread thread1 = new Thread(clock);
thread1.start();
JButton start = new JButton("On");
JButton stop = new JButton("off");
add(start);
add(stop);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg) {
thread1.resume();
}
});
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg) {
thread1.suspend();
}
});
setSize(300, 300);
}
/** 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();
}
/** 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.BLACK);
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);
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
class RunClock implements Runnable {
public void run() {
try {
while (true) {
if (hour > 12)
hour = hour % 12;
if (hour == 0)
hour = 12;
second++;
if (second == 60) {
minute++;
second = 0;
if (minute == 60) {
hour++;
minute = 0;
}
}
repaint();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Problem while putting thread to sleep.");
}
}
}
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setTitle("Clock and DrawArcs");
frame.setLayout(new GridLayout(1, 2));
frame.add(new DrawArcs());
frame.add(new StillClock(23, 50, 55));
frame.setSize(700, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation / Answer
The error means that you are using an outdated API
just recompile again that class with the -Xlint:deprecation option.
The compiler will then tell you what deprecated API you are using or overriding.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.