簡體   English   中英

Java LibGDX SwingWorker凍結GUI(塊上傳器)

[英]Java LibGDX SwingWorker Freezes GUI (Chunk Uploader)

我目前正在使用塊上載器(在Java中)將文件上載到私有服務器。 一切正常,但在應用程序上載文件時,它凍結了窗口。 上載后,一切都恢復正常,但在恢復過程中沒有恢復。 我知道它可以上傳,因為它可以正常工作並且可以在控制台中顯示進度。 因此,我嘗試將所有計算移到不同的線程上。 它仍然可以正常運行,但是凍結了。 此外,我讀到了有關SwingWorker的文章,我嘗試了一下,但是它仍然凍結了主GUI。

這是SwingWorker的類(所有方法都井井有條,因此您不會迷路,每個方法都包含一個解釋目的的注釋)(很多代碼,但是您實際上只需要查看BeginUpload()方法) ,因為這是循環所在的位置)

public class ChunkUploader extends SwingWorker<String, Object> {
    public String Text="";
    public String Text2="";

    boolean Uploading=false;
    private String Actual="";
    private int CurrentChunk=0;

    private int BytePosition=0;

    private int ChunkSize=100000;
    private String Contents="";
    private String FileName;
    private int PackageNumber;
    private String ByteString;
    private boolean Finalize;
    private String Password;
    private int TotalChunks;
    private byte[] bytes;
    TextPrompt Prompt;

    public double Progress=0;

    public boolean done=false;
    public ChunkUploader(String FileName) {
    //Prepares the thing
        this.FileName=FileName;
        Prompt = new TextPrompt();
    }

    @Override
    protected String doInBackground() throws Exception {
    //Calls the first method
            PrepareUpload(FileName);
        return null;
    }

    private void PrepareUpload(String FileName) {
    //Loads the file and then prompts for a password;
        if(Gdx.files.absolute(FileName).exists()) {
            Actual=FileName;
            System.out.println(FileName);///C:/T/Image.png
            Uploading=true;
            String pp[]=FileName.split("/");
            this.FileName=pp[pp.length-1];
            System.out.println(this.FileName);///C:/T/Forgive.txt
            FileHandle h=Gdx.files.absolute(Actual);
            Text = "Loading file..."+Actual;
            bytes = h.readBytes();
            Text2="";
            Prompt.Prepare("Password", this);
            Gdx.input.getTextInput(Prompt, "Input Dynamic Password:", "");
        }
    }

    public void PromptReturn(String Return, String Text) {
    //Reads the user input into the variable Password. Starts the upload
        if(Return.equals("Password")) {
            Password=Text;
            BeginUpload();
        }
    }

    private void BeginUpload() {
    //Splits the data to chunks and encodes it Base64. Sends the data to Return();

        Text = "Total size: "+Integer.toString(Contents.length());
        TotalChunks=(int) Math.ceil(bytes.length/ChunkSize);
        TotalChunks++;
        String CurrentSend="";
        CurrentChunk=0;

        String All="";

        Text2="Starting Upload...";

        for(int i=0; i<TotalChunks; i++) {
                byte[] temp=GetByteRange(CurrentChunk*ChunkSize, GetWidth(CurrentChunk*ChunkSize, bytes.length, ChunkSize));
                System.out.print(Double.toString(100*BytePosition/bytes.length)+"% complete.");
                Text = Double.toString(100*BytePosition/bytes.length)+"% complete.";
                BytePosition=GetWidth(CurrentChunk*ChunkSize, bytes.length, ChunkSize);

                CurrentSend=Base64.getEncoder().encodeToString(temp);CurrentChunk, false, Password, TotalChunks, FileName);
                String p="";
                while(p.isEmpty()) {
                    try {
                        p=Return(CurrentSend, CurrentChunk, false);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                All = All+CurrentSend;
                CurrentChunk=CurrentChunk+1;
        }
        Text2="Finalizing...";
        String p="";
        while(p.isEmpty()) {
            try {
                p=Return(CurrentSend, TotalChunks, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Text2="Upload should be complete.";
    }

    public byte[] GetByteRange(int Start, int End) {
        byte[] temp = new byte[End-Start];

        for(int i=Start; i<End; i++) {
            temp[i-Start] = bytes[i];
        }

        return temp;
    }

    public int GetWidth(int Position, int Size, int ChunkSize) {
        if(Position+ChunkSize<=Size) {
            int p=ChunkSize+Position;
            return p;
        }
        else {
            return Size;
        }
    }

    public String Return(String ByteString, int PackageNumber, boolean finalize) throws Exception {
    //Sends the file data to the server.
        String url = "http://www.stackoverflow.com/";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String urlParameters = "Pass="+Password+"&FileName="+FileName+"&PackageNumber="+Integer.toString(PackageNumber)+"&FinalPackageNum="+TotalChunks+"&Bytes="+ByteString;
        if(finalize==true) {
            urlParameters=urlParameters+"&State=Final&FinalPackageNum="+Integer.toString(TotalChunks);
        }
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(urlParameters.getBytes(StandardCharsets.UTF_8));
        wr.flush();
        wr.close();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        con.disconnect();
        System.out.println(response.toString());
        System.out.println("___________________");
        return response.toString();
    }
}

這是創建ChunkUploader類的LibGDX屏幕。

public class FileUploadScreen implements Screen {
    String FilePath;
    String Text="";
    String Text2="";

    ChunkUploader Uploader;
    OrthographicCamera camera;

    MyGame game;
    public FileUploadScreen(String FilePath, MyGame game) {
        this.game=game;
        camera = new OrthographicCamera();
        camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        this.FilePath = FilePath;
        Uploader = new ChunkUploader(FilePath);
        Uploader.execute();
    }
    @Override
    public void render(float delta) {
        Text=Float.toString(delta);
        game.MainFont.setColor(Color.WHITE);
        game.Screen.set(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.update();
        game.batch.setProjectionMatrix(camera.combined);

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        game.batch.begin();
            game.MainFont.draw(game.batch, Text, 100, game.Screen.y-50);
            game.MainFont.draw(game.batch, Text2, 100, game.Screen.y-100);
        game.batch.end();
    }
}

出於很好的考慮,這是文本提示:

public class TextPrompt implements TextInputListener {
            private String Text="";
            public boolean Active;
            private String Return;
            private ChunkUploader parent;

            public TextPrompt() {
                Active = false;
            }
            public void Prepare(String Return, ChunkUploader Parent) {
                Active = true;
                this.Return=Return;
                this.parent=Parent;
            }
            @Override
            public void input(String text) {
                Text = text;
                Active = false;
                parent.PromptReturn(Return, Text);
            }
            @Override
            public void canceled() {
                Text = "";
                Active = false;
            }
            public String GetInput() {
                return Text;
            }
}

同樣,只要文件未上傳,一切都將正常運行。 我只是感到困惑,因為如果它是在一個單獨的線程上載,則它不應與GUI沖突。 有人可以告訴我我做錯了什么嗎?

好吧,看來這有一些問題。 首先,ChunkUploader類中的所有方法都需要由doInBackground()調用。 當您查看PromptReturn方法內部時,BeginUpload(); 叫做。

要解決此問題,我們需要有一點創意。 以下是應進行的更改:

需要創建一個布爾變量,可以通過doInBackground()和PromptReturn()進行訪問。 其初始值應為false。

接下來,對前幾個方法進行更改:

private boolean Begin = false;

public void PromptReturn(String Return, String Text) {
//Reads the user input into the variable Password. Starts the upload
    if(Return.equals("Password")) {
        Password=Text;
        //BeginUpload();
        Begin = true;
    }
}

@Override
protected String doInBackground() throws Exception {
    //Calls the first method, and waits to call the second.
    PrepareUpload(FileName);
    boolean run=true;
    while(run) {
        if(Begin) {
            BeginUpload()
            run=false;
        }
    }
    return null;
}

此外,值得注意的是,您使用的Base64編碼器僅適用於桌面應用程序。 您應該考慮切換到此Base64編碼器/解碼器。 它通過使用方式來滿足您當前的所有需求,並且易於實現。 只需將Base64.java復制到您的項目中,您現在就可以將其用於桌面和android了。

暫無
暫無

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

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