2014-01-06 60 views
-1

即時嘗試顯示服務器狀態綠色處於關閉狀態。不過,我似乎無法使小程序改變顏色!無法獲取小應用程序上的服務器狀態

@SuppressWarnings("serial") 
public class MinecraftPinger extends Applet { 

    boolean O = true; 

    public void Pinger() throws IOException, InterruptedException { 

     while (true) { 
      Socket socket = SocketFactory.getDefault().createSocket(); 
      try { 
       socket.setSoTimeout(5000); 
       socket.connect(new InetSocketAddress("192.148.1.1", 25565)); 
       socket.close(); 
       System.out.println(O); 
       Thread.sleep(600000); 
       repaint(); 
      } catch (ConnectException e) { 
       O = false; 
       repaint(); 
       Thread.sleep(600000); 

      } 
     } 
    }//ends Pinger 

    public void paint(Graphics g) { 
     try { 
      if (O == true) { 
       setSize(100, 25); 
       setBackground(Color.GREEN); 
      } else { 
       setSize(100, 25); 
       setBackground(Color.RED); 
      } 
     } catch (Exception ex) { 
      System.out.println("Fail!"); 

     } 

    } 
} 
+2

請在這裏發表您的代碼;不在第三方網站上。 –

+0

我不認爲你的計算機的IP地址是'192.148.1.1',我想你混淆了你的計算機IP與路由器 –

+0

,IP是假的,使小程序紅,但小程序不會變紅 – superhamster

回答

2

你的心不是背景放映,因爲你重寫paint方法。相反,設置背景顏色的請嘗試使用傳遞給你的Graphics對象在paint方法

public void paint(Graphics g) { 
    if (connectionWorked == true) { 
     g.setColor(Color.green); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
    } else { 
     g.setColor(Color.red); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
    } 
} 

正如EJP說,你不應該調用EDT時阻塞操作。簡單地說,這意味着不要在正常執行程序(接口/事件線程)期間花費大量時間進行操作。在你的情況下,不要查詢服務器並阻止你的小程序進行初始化和顯示。這可以通過使用下面的代碼來避免:

SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
     // do some heavy lifting here 
    } 
}); 

我沒有看到Pinger()是不斷調用。當使用Applet類時,您需要覆蓋用於初始化數據的init()方法。

你可能最終得到這樣的

import java.applet.Applet; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.net.InetSocketAddress; 
import java.net.Socket; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.net.SocketFactory; 
import javax.swing.SwingUtilities; 

public class MinecraftPinger extends Applet { 

    private Boolean connectionWorked = null; 
    private boolean pinging = false; 

    @Override 
    public void init() { 

    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      pinging = true; 
      startPinging(); 
     } 
    }); 
    } 

    public void paint(Graphics g) { 
     if (connectionWorked == null) { 
      g.drawString("Attempting Connection...", getWidth()/2-40, getHeight()/2-10); 
     } else if (connectionWorked == true) { 
      g.setColor(Color.green); 
      g.fillRect(0, 0, getWidth(), getHeight()); 
     } else { 
      g.setColor(Color.red); 
      g.fillRect(0, 0, getWidth(), getHeight()); 
     } 
    } 

    public void startPinging() { 
     while (pinging) { 
      try { 
       Socket socket = SocketFactory.getDefault().createSocket(); 
       socket.setSoTimeout(2000); 
       socket.connect(new InetSocketAddress("mc.jujucraft.net", 25565)); 
       socket.close(); 
       connectionWorked = true; 
      } catch (Exception ex) { 
       connectionWorked = false; 
       Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      repaint(); 
      try { 
       Thread.sleep(5000); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 
+0

非常感謝! – superhamster

+0

如果能幫助解決問題,請[接受](http://meta.stackexchange.com/a/65088/155831)。 –

相關問題