Quantcast
Channel: Can't draw figures in Java instantly - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Can't draw figures in Java instantly

$
0
0

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 button 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!

}
//...
}

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images