无法立即用Java绘制图形
再会! 我开发客户端-服务器程序并遇到一个问题,我真的不知道如何解决它。
所以,我的按钮很少。单击按钮时,信息会发送到服务器,服务器会执行一些工作并发送结果。按钮的侦听器接收该消息,然后调用其他类的方法,该方法必须在屏幕上绘制结果。
所以,这里有问题。服务器向我发送的结果很少,程序必须立即绘制它。但它并没有这样做!它会等待所有消息到来,然后才绘制结果。
所以我想知道如何立即在屏幕上绘制结果!
代码: 按钮的监听器:
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();
//...
}
在屏幕上绘制结果的类:
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!
}
//...
}
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!
}
//...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在事件调度线程中执行读取,这不好。您应该在单独的线程中执行此操作(因为它是长时间运行的任务)。请参阅 http://download.oracle.com/javase/tutorial/uiswing/并发/index.html
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:
Call this in your action listener.