public void whilePlaying()
{
tm.start();
System.out.println( "Game started!" );
while ( true )
{
String message = input.nextLine();
System.out.println( message );
if ( message.substring( 0, 1 ).equals( "x" ) )
{
double work = Double.parseDouble( message.substring( 1 ) );
avatar2.setX( work );
}
if ( message.substring( 0, 1 ).equals( "y" ) )
{
double work = Double.parseDouble( message.substring( 1 ) );
avatar2.setY( work );
}
}
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
doDrawing( g );
System.out.println( "doing drawing" );
Toolkit.getDefaultToolkit().sync();
}
private void doDrawing( Graphics g )
{
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage( avatar.getImage(), (int)avatar.getX(), (int)avatar.getY(), this );
g2d.drawImage( avatar2.getImage(), (int)avatar2.getX(), (int)avatar2.getY(), this );
}
private class TAdapter extends KeyAdapter
{
@Override
public void keyPressed( KeyEvent e )
{
avatar.keyPressed( e );
}
@Override
public void keyReleased( KeyEvent e )
{
avatar.keyReleased( e );
}
}
@Override
public void actionPerformed( ActionEvent e )
{
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
output.println( "x" + avatar.getX() );
output.println( "y" + avatar.getY() );
avatar.move();
System.out.println( "repainting" );
System.out.println( "repainted" );
repaint();
}
} );
}
}
我有一台接受其中2个客户端的服务器。当我在whilePlaying中删除while循环时,重绘工作。但是,我需要while循环来从服务器接收信息。我运行代码并从actionPerformed中“重新绘制”和“重新绘制”不断显示,但paintComponent中的“绘图”从未显示出来。