如何将Java Applet更改为Windows 8.1的屏幕保护程序?

时间:2022-12-06 22:49:52

I have been trying for quite awhile to create a custom screen saver for windows 8.1. However through my research I have fond next to nothing on the subject, and any information I have found has either been outdated or from sources that are less than trust worthy. I have been using java for around 2 years now, but I'm still relatively new to programming in general, also I am currently using eclipse as a compiler, please help. Here is what I have currently:

我一直在尝试为Windows 8.1创建自定义屏幕保护程序。然而,通过我的研究,我对此问题几乎一无所知,而且我发现的任何信息要么已经过时,要么来自不值得信任的来源。我已经使用java大约2年了,但是我对编程总体上还是比较新的,我目前也在使用eclipse作为编译器,请帮忙。这是我目前的情况:

    import java.applet.Applet;
    import java.awt.*;
    import java.util.*;
    import java.io.*;

    @SuppressWarnings("serial") 
    public class CatScreenSaver extends Applet  
    {
private int runs;
private Scanner c;
private Scanner c2;
private Font myFont;
private Font error;
private int yLoc;
private boolean dir;
private Random randgen; 
private Color col;  
private int sleep;
private boolean start;
public CatScreenSaver() throws FileNotFoundException 
{
    runs = 0;
    yLoc = 570;
    dir = false;
    c = new Scanner(new File("cat.txt"));
    myFont = new Font("Consolas", 10, 10);
    error = new Font("Consolas", 20, 20);
    c2 = new Scanner(new File("cat2.txt"));
    randgen = new Random(); 
    sleep = 64;
    start=true;     
}   
public void init()
{
    col =Color.LIGHT_GRAY;
    setSize(4000,650);
    setBackground(Color.BLACK); 
}
public void paint(Graphics g)
{
    g.setFont(error);
    g.setColor(Color.red);
    g.drawString("We are experiencing technical difficulties, Please wait...", 250, 50);
    g.drawString("In the mean time enjoy this dancing cat!", 250, 70);
    if(!dir && !start)
    {
        try {
            printCat(g);
        } catch (FileNotFoundException | InterruptedException e) {          
            e.printStackTrace();
        }
    }
    else if(dir && !start)
    {
        try {
            printCat2(g);
        } catch (FileNotFoundException | InterruptedException e) {          
            e.printStackTrace();
        }
    }
    else if(!dir && start)
    {
        try {
            printSCat(g);
        } catch (FileNotFoundException | InterruptedException e) {          
            e.printStackTrace();
        }
    }
    else if(dir && start)
    {
        try {
            printSCat2(g);
        } catch (FileNotFoundException | InterruptedException e) {          
            e.printStackTrace();
        }
    }
}
public void printSCat(Graphics g) throws FileNotFoundException, InterruptedException
{
    g.setColor(col);
    g.setFont(myFont);
    int lines;          
    lines = c.nextInt();
    for(int i = 0; i < lines; i++)
    {
        String str = c.nextLine();              
        g.drawString(str, yLoc, (i*10));                
    }           
    runs++;
    if(runs == 31)
    {
        dir = true;         
        setScan();
    }
    Thread.sleep(sleep);
    repaint();
}
public void printSCat2(Graphics g) throws FileNotFoundException, InterruptedException
{
    g.setColor(col);
    g.setFont(myFont);
    int lines;          
    lines = c2.nextInt();
    for(int i = 0; i < lines; i++)
    {
        String str = c2.nextLine();             
        g.drawString(str, yLoc, (i*10));                
    }           
    runs--;
    if(runs == 0)
    {
        dir = false;            
        setScan();
        start = false;
    }
    Thread.sleep(sleep);
    repaint();
}
public void printCat(Graphics g) throws FileNotFoundException, InterruptedException
{
    g.setColor(col);
    g.setFont(myFont);
    int lines;          
    lines = c.nextInt();
    for(int i = 0; i < lines; i++)
    {
        String str = c.nextLine();              
        g.drawString(str, yLoc, (i*10));                
    }       
    yLoc-=20;       
    runs++;
    if(runs == 31)
    {
        dir = true;
        changeColor();
        setScan();
    }
    Thread.sleep(sleep);
    repaint();
}   
public void printCat2(Graphics g) throws FileNotFoundException, InterruptedException
{
    g.setColor(col);
    g.setFont(myFont);
    int lines;
    lines = c2.nextInt();
    for(int i = 0; i < lines; i++)
    {
        String str = c2.nextLine();             
        g.drawString(str, yLoc, (i*10));                
    }
    runs--;
    yLoc+=20;
    if(runs==0)
    {
        dir = false;
        changeColor();
        setScan();
    }
    Thread.sleep(sleep);            
    repaint();
}
public void changeColor()
{
    int rand = randgen.nextInt(10)-1;
    if(rand==1)
        col = Color.DARK_GRAY;
    else if(rand==2)
        col = Color.green;
    else if(rand==3)
        col=Color.BLUE;
    else if(rand==4)
        col = Color.CYAN;
    else if(rand==5)
        col =Color.MAGENTA;
    else if(rand==6)
        col =Color.YELLOW;
    else if(rand==7)
        col =Color.PINK;
    else if(rand==8)
        col =Color.ORANGE;
    else if(rand==9)
        col =Color.LIGHT_GRAY;
    else
        col =Color.WHITE;
}
public void setScan()throws FileNotFoundException 
{
    c = new Scanner(new File("cat.txt"));
    c2 = new Scanner(new File("cat2.txt"));
}       
}

Also the code uses 2 text files to "print" to the screen

此外,代码使用2个文本文件“打印”到屏幕上

1 个解决方案

#1


To create standalone GUI application please use java swing or (latest) JavaFX, then you would be able to create standalone java application, now you can run gui application using java command inside JVM.

要创建独立的GUI应用程序,请使用java swing或(最新的)JavaFX,然后您就可以创建独立的Java应用程序,现在您可以在JVM中使用java命令运行gui应用程序。

In order to setup you java program to run as screensaver in windows - you need to package java program (jar) then create some sort of exe/scr so that you can setup window to use your screensaver to run.

为了设置java程序在Windows中作为屏幕保护程序运行 - 你需要打包java程序(jar)然后创建一些exe / scr,以便你可以设置窗口来使用你的屏幕保护程序来运行。

Quick google search found below link. Probably you can use same process with swing as well.

在链接下方找到快速谷歌搜索。可能你也可以使用相同的过程来摆动。

JavaFX Screensaver

Similiar question

#1


To create standalone GUI application please use java swing or (latest) JavaFX, then you would be able to create standalone java application, now you can run gui application using java command inside JVM.

要创建独立的GUI应用程序,请使用java swing或(最新的)JavaFX,然后您就可以创建独立的Java应用程序,现在您可以在JVM中使用java命令运行gui应用程序。

In order to setup you java program to run as screensaver in windows - you need to package java program (jar) then create some sort of exe/scr so that you can setup window to use your screensaver to run.

为了设置java程序在Windows中作为屏幕保护程序运行 - 你需要打包java程序(jar)然后创建一些exe / scr,以便你可以设置窗口来使用你的屏幕保护程序来运行。

Quick google search found below link. Probably you can use same process with swing as well.

在链接下方找到快速谷歌搜索。可能你也可以使用相同的过程来摆动。

JavaFX Screensaver

Similiar question