Вот подробный туториал как создать аналоговые часы в Eclipse с использованием Java Swing:
Шаг 1: Установка Eclipse
Если у вас еще нет Eclipse, вы можете скачать его с официального сайта Eclipse. Установите Eclipse, следуя инструкциям на сайте.
Шаг 2: Создание нового проекта
- Запустите Eclipse.
- Выберите "File" -> "New" -> "Java Project".
- Введите имя проекта (например, "JavaClock") и нажмите "Finish".
Шаг 3: Создание класса JavaClock
- Внутри вашего проекта правой кнопкой мыши нажмите на папку "src".
- Выберите "New" -> "Class".
- Введите имя класса (например, "JavaClock") и убедитесь, что опция "public static void main(String[] args)" отмечена. Нажмите "Finish".
Шаг 4: Вставка кода
Замените содержимое класса JavaClock следующим кодом:
import java.awt.Color;
}
}
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaClock extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
Thread thread = null;
SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());
Date currentDate;
int xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0, lastxh = 0, lastyh = 0;
// Method to draw the clock structure
private void drawStructure(Graphics g) {
g.setFont(new Font("TimesRoman", Font.BOLD, 22));
g.setColor(Color.white);
g.fillOval(xcenter - 150, ycenter - 150, 300, 300);
g.setColor(Color.blue);
g.drawString("W.H.", 113, 300);
g.setColor(Color.black);
g.drawString("9", xcenter - 145, ycenter + 0);
g.drawString("3", xcenter + 135, ycenter + 0);
g.drawString("12", xcenter - 10, ycenter - 130);
g.drawString("6", xcenter - 10, ycenter + 145);
}
// Method to paint the clock hands and update their positions
public void paint(Graphics g) {
int xhour, yhour, xminute, yminute, xsecond, ysecond, second, minute, hour;
drawStructure(g);
currentDate = new Date();
formatter.applyPattern("s");
second = Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern("m");
minute = Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern("h");
hour = Integer.parseInt(formatter.format(currentDate));
xsecond = (int) (Math.cos(second * 3.14 / 30 - 3.14 / 2) * 120 + xcenter);
ysecond = (int) (Math.sin(second * 3.14 / 30 - 3.14 / 2) * 120 + ycenter);
xminute = (int) (Math.cos(minute * 3.14 / 30 - 3.14 / 2) * 100 + xcenter);
yminute = (int) (Math.sin(minute * 3.14 / 30 - 3.14 / 2) * 100 + ycenter);
xhour = (int) (Math.cos((hour * 30 + minute / 2) * 3.14 / 180 - 3.14 / 2) * 80 + xcenter);
yhour = (int) (Math.sin((hour * 30 + minute / 2) * 3.14 / 180 - 3.14 / 2) * 80 + ycenter);
// Erase if necessary, and redraw
g.setColor(Color.magenta);
if (xsecond != lastxs || ysecond != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
}
if (xminute != lastxm || yminute != lastym) {
g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
}
if (xhour != lastxh || yhour != lastyh) {
g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
}
g.setColor(Color.magenta);
g.drawLine(xcenter, ycenter, xsecond, ysecond);
g.setColor(Color.red);
g.drawLine(xcenter, ycenter - 1, xminute, yminute);
g.drawLine(xcenter - 1, ycenter, xminute, yminute);
g.setColor(Color.green);
g.drawLine(xcenter, ycenter - 1, xhour, yhour);
g.drawLine(xcenter - 1, ycenter, xhour, yhour);
lastxs = xsecond;
lastys = ysecond;
lastxm = xminute;
lastym = yminute;
lastxh = xhour;
lastyh = yhour;
}
// Start the thread
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
// Stop the thread
public void stop() {
thread = null;
}
// Run method for the thread
public void run() {
while (thread != null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
repaint();
}
thread = null;
}
// Update method to call the paint method
public void update(Graphics g) {
paint(g);
}
// Main method to create the JFrame and start the clock
public static void main(String args[]) {
JFrame window = new JFrame();
Color c = new Color(118, 73, 190);
window.setBackground(c);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(0, 0, 400, 400);
JavaClock clock = new JavaClock();
window.getContentPane().add(clock);
window.setVisible(true);
clock.start();
}
}
jШаг 5: Запуск приложения
- Нажмите правой кнопкой мыши на классе JavaClock.
- Выберите "Run As" -> "Java Application".
Теперь у вас должны появиться аналоговые часы на экране!
iл поможет вам создать аналоговые часы с использованием Java Swing в Eclipse. Если у вас возникнут вопросы, не стесняйтесь задавать их! Подписывайтесь на канал!