开发者

Can't draw figures in Java instantly

Good day! I develop program client-server and met one problem and i really don't know how to solve it.

So, i have few buttons. When butto开发者_运维百科n clicks then info sends to the server, server does some work and sends result. Listener of button receives that and then call method of other class which must draw the result on screen.

So, here is problem. Server sends me few results and program must draw it instantly. But it doesn't do that! It waits till all messages will come and ONLY then draws result.

So i want to know how to draw result on the screen instantly!

Code: Listener of Button:

public class ShowFrame extends JFrame
{
    startButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
       {
            try 
            {
                messageToServer.println("Start");

                while( true )
                {
                    fserver = answerOfServer.readLine(); //Get result from server

                    if ( fserver.equals("Finish") )
                    {
                        break;
                    }
                    if( fserver.equals("Busy 1") )
                    {
                        ShowFrame.this.stuff.setBusy( 1 );
                    }
                    if( fserver.equals("Busy 2") )
                    {
                        ShowFrame.this.stuff.setBusy( 2 );
                    }
                    //...Same code


                }
            } catch (IOException ex) {
                Logger.getLogger(ShowFrame.class.getName()).log(Level.SEVERE, null, ex);
      }

    DrawStuff stuff = new DrawStuff();
    //...
}

Class which draw result on the screen:

public class DrawStuff extends JComponent
{

public DrawStuff()
{
    s1 = false;
    s2 = false;
    s3 = false;
    s4_1 = false;
    s4_2 = false;
    s4_3 = false;
}

@Override
public void paintComponent( Graphics g )
{
    Graphics2D g2 = (Graphics2D) g;
    //...
        if ( s1 )                     
        {
                g2.draw(line1_of_P1);
                g2.draw(line2_of_P1);
        }
        //...
 }

public void setBusy( int i ) //If such id found then figure will be drawn by prog.
{
    if      ( i == 1 )
    {
        s1   = true;
    }
    else if ( i == 2 )
    {
        s2   = true;
    }
    else if ( i == 3 )
    {
        s3   = true;
    }
    else if ( i == 4 )
    {
        s4_1 = true;
    }
    else if ( i == 5 )
    {
        s4_2 = true;
    }
    else if ( i == 6 )
    {
        s4_3 = true;
    }
    this.repaint(); //DOESN'T WORK AS IT MUST!

}
//...
}


You execute reading in event dispatch thread and it's not good. You should do it in separate thread (because it is long running task). See http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html


You need to execute the actual actions within a separate thread so the event dispatch thread can keep on running (it needs to be running to paint the screen). You can create and start a new thread running by using the following:

    Thread newThread = new Thread(new Runnable() {

    @Override
    public void run() {
      // put your actions to perform in here
    }});

    newThread.start();

Call this in your action listener.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜