簡體   English   中英

如何將Java Applet更改為Windows 8.1的屏幕保護程序?

[英]How do you change a Java Applet into a Screensaver for Windows 8.1?

我一直在嘗試為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"));
}       
}

該代碼還使用2個文本文件“打印”到屏幕

要創建獨立的GUI應用程序,請使用java swing或(最新)JavaFX,這樣您就可以創建獨立的Java應用程序,現在您可以在JVM中使用java命令運行gui應用程序。

為了將Java程序設置為在Windows中作為屏幕保護程序運行-您需要打包Java程序(jar),然后創建某種exe / scr,以便可以設置窗口以使用屏幕保護程序來運行。

在下面的鏈接中找到了快速的Google搜索。 也許您也可以對秋千使用相同的過程。

JavaFX屏幕保護程序

類似問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM