2012-04-04 3 views
5

काम करने में विफल रहता है मैं अपने पैनल पर एक अन्य थ्रेड से प्राप्त डेटा के आधार पर एक छवि खींचना चाहता था। मुझे यकीन है कि डेटा और परिणामी पिक्सेल सरणी अच्छी तरह से काम करती है, लेकिन पेंट() कभी काम नहीं करेगी। क्या कोई मुझे बता सकता है कि यहां क्या गलत हो रहा है?repaint()

import javax.swing.*; 
import java.awt.*; 
import java.awt.image.*; 

/** Create an image from a pixel array. **/ 
public class PicturePlaza extends JApplet 
{ 
    ImagePanel fImagePanel; 
    ReadCom readComPort; 
    Thread readPortThread; 

    public void init() { 
    // initiate the read port thread so that it can receive data 
    readComPort = new ReadCom(); 
    readPortThread = new Thread(readComPort,"ReadCom"); 
    readPortThread.start(); 

    Container content_pane = getContentPane(); 
    fImagePanel = new ImagePanel(); 
    content_pane.add (fImagePanel); 

    } 

    // Tell the panel to create and display the image, if pixel data is ready. 
    public void start() { 
     while(true){ 
      if(readComPort.newPic){ 
       fImagePanel.go(); 
      } 
      try { 
        Thread.sleep(4000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 


/** Create an image from a pixel array. **/ 
    class ImagePanel extends JPanel{ 
     Image fImage; 
     int fWidth = ReadCom.row, fHeight = ReadCom.col;  

     void go() {   
        //update the image if newPic flag is set to true     
        fImage = createImage (new MemoryImageSource (fWidth, fHeight, ReadCom.fpixel, 0, fWidth)); 
        repaint(); 
        readComPort.newPic = false; //disable the flag, indicating the image pixel has been used                
     } 

     /** Paint the image on the panel. **/ 
     public void paintComponent (Graphics g) { 
     super.paintComponent (g);  
     g.drawImage (fImage, 0, 0, this); 
     } 
    } 
} 

धन्यवाद

+9

'थ्रेड.sleep (4000); 'ईडीटी (इवेंट डिस्पैच थ्रेड) को अवरुद्ध न करें - जब ऐसा होता है तो जीयूआई' फ्रीज 'होगा। 'थ्रेड.sleep (एन)' को दोहराने के बजाय कार्यों को दोहराने के लिए स्विंग 'टाइमर 'या लंबे समय तक चलने वाले कार्यों के लिए' स्विंगवर्कर 'को लागू करने के बजाय। अधिक जानकारी के लिए देखें [स्विंग में Concurrency] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)। –

+0

आपकी तत्काल प्रतिक्रिया के लिए धन्यवाद। लेकिन अगर मैं बस थ्रेड.sleep (4000) कथन को हटा देता हूं, तो यह भी काम करने में विफल रहता है। उसका क्या कारण है? – Daniel

+1

यदि आप केवल मेरे द्वारा प्रदान किए गए लिंक पढ़ते हैं, और सिफारिशों को लागू करते हैं, तो क्या होता है? –

उत्तर

0

repaint(); प्रयास करें और तब validate(); अपने एप्लेट में (PicturePlaza)।

1

repaint() पर बस थोड़ा सा नोट। repaint() स्क्रीन की एक पेंट शेड्यूल करता है, यह हमेशा मेरे अनुभव में तुरंत नहीं करेगा। मैंने पाया कि सबसे अच्छा समाधान सीधे paint() पर कॉल करना है।

Graphics g; 
g = getGraphics(); 
paint(g); 

मैंने इसे अपने कोड में कॉल करने के लिए एक नया फ़ंक्शन के रूप में रखा जब मैं इसे तुरंत पेंट करना चाहता था। इसके अलावा यह स्क्रीन पर पिछले ग्राफिक्स को मिटा नहीं देगा, आपको इसे मैन्युअल रूप से करना होगा।

संबंधित मुद्दे