Potřebuji poradit, jak by šlo udělat, aby stejně "rychle" pracoval, ale tolik nezasekával počítač, šlo by to nějak?
Kód v 1. třídě:
► Zobrazit spoiler
Kód: Vybrat vše
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.ScrollPane;
import java.awt.TrayIcon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DefaultCaret;
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
//Komponenty do okna
static JButton historie = new JButton("Historie");
static JLabel cas = new JLabel("00:00:00");
//konec komponentu
//SystemTray
private static DisplaySystemTray DST;
// Kolik sekund je PC aktivni
public static int DelkaAktivity = 0;
//uziv. jmeno
public static String username = null;
// ma se pristi sekundu pricist aktivita?
private static boolean PocitatAktivitu = false;
// kalendar pro cas a datum
private static Calendar cal;
// instance, kdyz budou potreba volat funkce
private static Frame instance;
// Jaky cas byl pri nastarvovani programu?
public static String StartTime;
// jaky cas byl pri vypnuti programu?
public static String ShutDownTime = "00:00:00";
// jake datum bylo pro nastartovani programu?
public static String StartDate;
// jake datum bylo pri vypnuti programu?
public static String ShutDownDate = "00. 00. 0000";
// co je v souboru s casy?
private static String fileWithTimes = "";
// kde je soubor s casy?
public static File FILE = new File(
"C:/Program Files/JakDlouhoJsemNaPc/times.txt");
public static Frame getInstance() {
return instance;
}
public Frame() {
super("Jak dlouho jsem na pc?");
instance = this;
DST = new DisplaySystemTray();
DST.trayIcon.displayMessage("Started!", "Program byl úspěšně spuštěn!", TrayIcon.MessageType.INFO);
}
public static void main(String args[]) {
cal = Calendar.getInstance();
final SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
final SimpleDateFormat sdfDate = new SimpleDateFormat("dd. MM. YYYY");
StartDate = sdfDate.format(cal.getTime());
StartTime = sdfTime.format(cal.getTime());
StartTryPocitat();
StartPocitani();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(FILE));
String timedLine;
while ((timedLine = reader.readLine()) != null) {
timedLine = timedLine.replaceAll("\n\r", "");
timedLine = timedLine.replaceAll("\n", "");
timedLine = timedLine.replaceAll("\r", "");
fileWithTimes += timedLine + "\r\n";
}
reader.close();
} catch (FileNotFoundException e) {
new File("C:/Program Files/JakDlouhoJsemNaPc").mkdirs();
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
SaveFile();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
SaveFile();
}
}));
Frame f = new Frame();
f.StartFrame();
}
public void StartFrame() {
this.setVisible(false);
this.setLayout(new FlowLayout());
this.setSize(200, 75);
this.setResizable(false);
//pridani funkci k komponentum
historie.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
JFrame f = new JFrame("Historie");
JTextArea ta = new JTextArea();
f.setSize(500, 500);
JScrollPane sp = new JScrollPane();
sp.setBounds(23,40,394,191);
ta.setText(fileWithTimes);
f.setVisible(true);
f.getContentPane().add(sp);
sp.setViewportView(ta);
}
});
//konec
//nastaveni komponentu
//konec
//pridani komponentu
this.add(cas);
this.add(historie);
//konec
}
//pricita cas
public static void StartPocitani() {
Thread StartPocitani = new Thread(new Runnable() {
long lastOutput = System.currentTimeMillis();
@Override
public void run() {
int pocet = 0;
while (true) {
long currentTick = System.currentTimeMillis();
if ((currentTick - lastOutput) > 1000) {
if (PocitatAktivitu) {
DelkaAktivity++;
lastOutput = currentTick;
pocet++;
cas.setText(getPCActivatedTimeInString());
int[] casy = getPCActivatedTime();
if (casy[2] == 1 && casy[1] == 30 && casy[0] == 0){
DST.trayIcon.displayMessage("Cas", "Už uplynula hodina a půl, měl/a bys jít od počítače", TrayIcon.MessageType.WARNING);
}
if (pocet == 20) {
pocet = 0;
SaveFile();
}
}
}
}
}
});
StartPocitani.start();
}
//zjistuje, jestli ma pocitat dal nebo pockat na aktivitu pc
public static void StartTryPocitat() {
Thread StartTryPocitat = new Thread(new Runnable() {
long lastOutput = System.currentTimeMillis();
@Override
public void run() {
int processesAtStart = 0;
ArrayList<String> processes = new ArrayList<String>();
while (true) {
Process p = null;
try {
if (username == null){
username = System.getProperty("user.name");
}
p = Runtime.getRuntime().exec(
"tasklist /fi \"SESSIONNAME eq Console\" /fi \"USERNAME eq "+username+"\" /fo csv /nh");
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
InputStream pOutput = p.getInputStream();
BufferedReader pReader = new BufferedReader(
new InputStreamReader(pOutput));
String timedString;
int linesReaded =0;
processes.clear();
try {
while ((timedString = pReader.readLine()) != null) {
if (linesReaded == 0){
linesReaded++;
}else if (timedString.contains("task") || timedString.contains("host")){
linesReaded++;
}else {
System.out.println(timedString);
processes.add(timedString);
linesReaded++;
}}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
if (processesAtStart == 0) {
processesAtStart = processes.size();
}
if (processes.size() > processesAtStart) {
PocitatAktivitu = true;
System.out.println("Aktivita .... " + processes.size() + " aktivních procesů");
} else if (processes.size() < processesAtStart) {
processesAtStart--;
} else {
PocitatAktivitu = false;
System.out.println("Bez aktivity .... " + processes.size() + " aktivních procesů");
}
}
}
}
);
StartTryPocitat.start();
}
public static int[] getPCActivatedTime() {
int cislo = DelkaAktivity;
int sekundy = cislo;
int minuty;
int hodiny;
minuty = cislo / 60;
if (minuty > 0) {
hodiny = minuty / 60;
if (hodiny > 0) {
sekundy -= (minuty * 60);
minuty -= (hodiny * 60);
} else {
hodiny = 0;
sekundy -= (minuty * 60);
}
} else {
hodiny = 0;
}
int[] i = { sekundy, minuty, hodiny };
return i;
}
public static String getPCActivatedTimeInString() {
int[] activatedTime = getPCActivatedTime();
String sekundy = "" + activatedTime[0];
String minuty = "" + activatedTime[1];
String hodiny = "" + activatedTime[2];
if (activatedTime[2] < 10) {
// hodiny
hodiny = "0" + activatedTime[2];
}
if (activatedTime[1] < 10) {
// minuty
minuty = "0" + activatedTime[1];
}
if (activatedTime[0] < 10) {
// sekundy
sekundy = "0" + activatedTime[0];
}
return (hodiny + ":" + minuty + ":" + sekundy);
}
public static boolean SaveFile() {
try {
cal = Calendar.getInstance();
final SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
final SimpleDateFormat sdfDate = new SimpleDateFormat(
"dd. MM. YYYY");
ShutDownTime = sdfTime.format(cal.getTime());
ShutDownDate = sdfDate.format(cal.getTime());
FileWriter fw = new FileWriter(FILE);
fw.write(fileWithTimes);
fw.write("Je možné, že čas vypnutí se liší a to maximálně o 20 sekund\n\r");
fw.write("Datum zapnutí: " + StartDate + "\n\r");
fw.write("Datum vypnutí: " + ShutDownDate + "\n\r");
fw.write("Čas zapnutí: " + StartTime + "\n\r");
fw.write("Čas vypnutí: " + ShutDownTime + "\n\r");
fw.write("Jak dlouho byl počítač používán: "
+ getPCActivatedTimeInString() + "\n\r");
fw.write("=====================================================\n\r");
fw.close();
System.out.println("File SAVED success!");
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("File SAVED unsuccess!");
return false;
}
}
}
Kód v 2. třídě:
► Zobrazit spoiler
Kód: Vybrat vše
import java.awt.Image;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.swing.ImageIcon;
public class DisplaySystemTray {
public static TrayIcon trayIcon;
public DisplaySystemTray(){
StartSystemTray();
}
public void StartSystemTray(){
if (!SystemTray.isSupported()){
System.out.println("Tray is not supported");
System.exit(0);
return;
}
final PopupMenu popup = new PopupMenu();
trayIcon = new TrayIcon(getTrayIcon("/images/ico.png"));
final SystemTray tray = SystemTray.getSystemTray();
trayIcon.setPopupMenu(popup);
trayIcon.setToolTip("Jak Dlouho Jsem Na PC?");
trayIcon.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
Frame.getInstance().setVisible(true);
}
});
try {
tray.add(trayIcon);
}catch (Exception e){
}
}
public static Image getTrayIcon(String path){
URL url = DisplaySystemTray.class.getResource(path);
return (new ImageIcon(url, "Tray Icon")).getImage();
}
}
Vím, že by to šlo roztahat do mnoha různých tříd, ale podle mě to není až tak potřeba, pokud máte něco proti tomuto, tak mi napište proč, alespoň bych věděl, co změnit. Pokud myslíte orientaci, tak to je jasné, ale tohle je jen menší prográmek, ke kterému se už neplánuji vracet, když bude hotov.
Opravdu si prosím nestěžujte na úpravu, když by to byl větší projekt, dal bych si více záležet.