在一个小应用程序无法获取服务器状态 [英] Can't get server status on an applet

查看:216
本文介绍了在一个小应用程序无法获取服务器状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试显示服务器状态绿色红色关闭。但是我不能似乎使小程序变色!

  @燮pressWarnings(串行)
公共类MinecraftPinger扩展的Applet {    布尔O =真实的;    公共无效瓶儿()抛出IOException异常,InterruptedException的{        而(真){
            Socket套接字= SocketFactory.getDefault()的createSocket()。
            尝试{
                Socket的(5000);
                socket.connect(新的InetSocketAddress(192.148.1.1,25565));
                socket.close();
                的System.out.println(O);
                视频下载(600000);
                重绘();
            }赶上(的ConnectException E){
                O = FALSE;
                重绘();
                视频下载(600000);            }
        }
    } //结束瓶儿    公共无效漆(图形G){
        尝试{
            如果(O ==真){
                的setSize(100,25);
                的setBackground(Color.GREEN);
            }其他{
                的setSize(100,25);
                的setBackground(Color.RED);
            }
        }赶上(例外前){
            的System.out.println(失败!);        }    }
}


解决方案

您背景的心不是显示,因为你覆盖paint方法。相反,设置背景色尝试使用传递给你的油漆图形对象

 公共无效漆(图形G){
    如果(connectionWorked ==真){
        g.setColor(Color.green);
        g.fillRect(0,0,的getWidth(),的getHeight());
    }其他{
        g.setColor(Color.red);
        g.fillRect(0,0,的getWidth(),的getHeight());
    }
}


由于EJP说你不应该叫EDT时阻塞操作。简单地说,这意味着不这样做是把你的程序(接口/事件线程)的正常执行过程中的时间显著量操作。在你的情况不查询服务器,并从初始化并显示阻止你的小程序。这可以通过使用以下code来避免:

  SwingUtilities.invokeLater(Runnable的新(){
    公共无效的run(){
         //做一些繁重的工作在这里
    }
});


我不明白其中瓶儿()是不断调用。当使用 Applet的类,你需要覆盖的init()方法用于初始化数据。

您可能会像这样结束了。

 进口java.applet.Applet中;
进口java.awt.Color中;
进口java.awt.Graphics;
进口java.net.InetSocketAddress;
进口的java.net.Socket;
进口java.util.logging.Level中;
进口java.util.logging.Logger中;进口javax.net.SocketFactory;
进口javax.swing.SwingUtilities中;公共类MinecraftPinger扩展的Applet {    私人布尔connectionWorked = NULL;
    私人布尔侦测= FALSE;    @覆盖
    公共无效的init(){    SwingUtilities.invokeLater(Runnable的新(){
        公共无效的run(){
            侦测= TRUE;
            startPinging();
        }
    });
    }    公共无效漆(图形G){
        如果(connectionWorked == NULL){
            g.drawString(试图连接...的getWidth()/ 2-40的ge​​tHeight()/ 2-10);
        }否则如果(connectionWorked ==真){
            g.setColor(Color.green);
            g.fillRect(0,0,的getWidth(),的getHeight());
        }其他{
            g.setColor(Color.red);
            g.fillRect(0,0,的getWidth(),的getHeight());
        }
    }    公共无效startPinging(){
        而(侦测){
            尝试{
                Socket套接字= SocketFactory.getDefault()的createSocket()。
                Socket的(2000);
                socket.connect(新的InetSocketAddress(mc.jujucraft.net,25565));
                socket.close();
                connectionWorked = TRUE;
            }赶上(例外前){
                connectionWorked = FALSE;
                。Logger.getLogger(Test.class.getName())日志(Level.SEVERE,空,前);
            }
            重绘();
            尝试{
                视频下载(5000);
            }赶上(InterruptedException的前){
                。Logger.getLogger(Test.class.getName())日志(Level.SEVERE,空,前);
            }
        }
    }
}

im trying to display the server status green is on red is off. However i cant seem to make the applet change color!

@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!");

        }

    }
}

解决方案

Your background isnt showing because you're overriding the paint method. Instead of setting the background color try using the Graphics object passed to you in the paint method

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());
    }
}


As EJP said you should never call blocking operations during the EDT. Put simply this means dont do operations that take a significant amount of time during the normal execution of your program (the interface/events thread). In your case don't query a server and block your applet from initializing and displaying. This can be avoided by using the following code:

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


I dont see where Pinger() is ever called. When using the Applet class you need to overwrite the init() method for initializing data.

You might end up with something like this

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);
            }
        }
    }
}

这篇关于在一个小应用程序无法获取服务器状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆