Skip to content

Instantly share code, notes, and snippets.

@uvlad7
Last active March 1, 2019 13:48
Show Gist options
  • Save uvlad7/38eda6d95ebd4642515280d105bdaec5 to your computer and use it in GitHub Desktop.
Save uvlad7/38eda6d95ebd4642515280d105bdaec5 to your computer and use it in GitHub Desktop.
УП 3.1
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Clock extends JFrame implements ActionListener {
private PaintPanel clockFace;
private BufferedImage clock;
private Timer timer;
private Time time;
private Clip tick;
//center 172, 300;
public Clock() throws HeadlessException {
super("Clock");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(new ImageIcon("src\\gear.png").getImage());
clockFace = new PaintPanel(360, 480);
clockFace.setDoubleBuffered(true);
clock = ImageIO.read(new File("src\\clock.png"));
Graphics2D graphics = (Graphics2D) clock.getGraphics();
graphics.setColor(Color.BLACK);
graphics.setStroke(new BasicStroke(6));
graphics.drawOval(29, 157, 286, 286);
timer = new Timer(1000, this);
tick = AudioSystem.getClip();
tick.open(AudioSystem.getAudioInputStream(new File("src\\tick.wav")));
tick.loop(Integer.MAX_VALUE);
time = new Time();
clockFace.loadImage(clock);
draw();
add(clockFace);
pack();
timer.start();
tick.start();
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException | IOException |
UnsupportedAudioFileException | LineUnavailableException e) {
System.err.println(e.getMessage());
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == timer) {
time.plus();
clockFace.loadImage(clock);
draw();
}
}
private void draw() {
Graphics2D graphics = (Graphics2D) clockFace.getBuffer().getGraphics();
graphics.setColor(Color.BLACK);
graphics.setStroke(new BasicStroke(6));
graphics.drawLine(172, 300,
172 + (int) Math.round(80 * Math.sin(Math.toRadians(time.hours * 30))),
300 - (int) Math.round(80 * Math.cos(Math.toRadians(time.hours * 30))));
graphics.setStroke(new BasicStroke(4));
graphics.drawLine(172, 300,
172 + (int) Math.round(100 * Math.sin(Math.toRadians(time.minutes * 6))),
300 - (int) Math.round(100 * Math.cos(Math.toRadians(time.minutes * 6))));
graphics.setColor(Color.RED);
graphics.setStroke(new BasicStroke(2));
graphics.drawLine(172, 300,
172 + (int) Math.round(120 * Math.sin(Math.toRadians(time.seconds * 6))),
300 - (int) Math.round(120 * Math.cos(Math.toRadians(time.seconds * 6))));
graphics.setColor(Color.BLACK);
graphics.setStroke(new BasicStroke(10));
graphics.drawOval(167, 295, 10, 10);
clockFace.repaint();
}
}
public class Main {
public static void main(String[] args) {
Clock clock = new Clock();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class PaintPanel extends JPanel {
private BufferedImage bufferedImage;
public PaintPanel(int width, int height) {
setPreferredSize(new Dimension(width, height));
}
@Override
public void setPreferredSize(Dimension dimension) {
super.setPreferredSize(dimension);
bufferedImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
graphics.setBackground(new Color(1, 1, 1, 0f));
graphics.clearRect(0, 0, dimension.width, dimension.height);
graphics.dispose();
}
@Override
protected void paintComponent(Graphics graphics) {
graphics.drawImage(bufferedImage, 0, 0, null);
}
public BufferedImage getBuffer() {
return bufferedImage;
}
public void loadImage(BufferedImage buf) {
bufferedImage.createGraphics().setColor(Color.WHITE);
bufferedImage.createGraphics().fillRect(0, 0, getWidth(), getHeight());
bufferedImage.createGraphics().drawImage(buf, 0, 0, null);
repaint();
}
}
import java.time.LocalDateTime;
public class Time {
int seconds;
int minutes;
int hours;
public Time() {
LocalDateTime time = LocalDateTime.now();
seconds = time.getSecond();
minutes = time.getMinute();
hours = time.getHour() % 12;
}
public void plus() {
if (seconds == 59) {
seconds = 0;
if (minutes == 59) {
minutes = 0;
if (hours == 11) {
hours = 0;
} else {
hours++;
}
} else {
minutes++;
}
} else {
seconds++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment