首页 \ 问答 \ ImageView只显示循环中的最后一个图像(ImageView only showing last image in loop)

ImageView只显示循环中的最后一个图像(ImageView only showing last image in loop)

你好,我正在做的是,我从客户端接收图像并在循环中显示它..数据被正确接收,但只有最后收到的图像显示循环结束时..

这不是线程中的主控制器。 我不知道什么是错的,因为它正在接收数据,它应该在每次迭代时更新....

这是代码:

@FXML  ImageView currentframe;
public void run()
{
        System.out.println("hi controller");  
        System.out.println("creating socket");
        DatagramSocket serverSocket=null; 
        try {
            serverSocket = new  DatagramSocket(1001);
        } catch (SocketException ex) {
            Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("created socket");

        byte[] receiveData = new byte[50000]; 

        //byte[] sendData  = new byte[1024]; 
        //BufferedImage img = null;
        //Image image = null;

        int counter = 0;

        while(counter<20){
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
            try {  
                serverSocket.receive(receivePacket);
            } catch (IOException ex) {
                Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
            }
            //System.out.println("packetreceive");
            BufferedImage img = null;

            try {
                img = ImageIO.read(new ByteArrayInputStream(receivePacket.getData()));
            } catch (IOException ex) {
                Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
            }

            //System.out.println("packetreceive1");
            System.out.println("received packet length "+receivePacket.getLength());

            Image image= SwingFXUtils.toFXImage(img, null); 

            Image tempImg = image;

            Platform.runLater(() -> currentframe.setImage(tempImg));


            /*
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
            }*/

            //currentframe.setImage(image);
            //threadclass t = new threadclass(currentframe, image);
            //t.start();

           //Platform.runLater(null);

            counter++;
            System.out.println("IMAGE SET ON FRAME...");
        }
        //System.out.println("ssdcd" );
        // currentframe.setImage(image);
        //System.out.println("last packet ");
}

这里是我如何开始在控制器中的线程:

@FXML
public void btn(ActionEvent event) throws IOException, InterruptedException
{
        threadclass t = new threadclass(currentframe);
        t.run();
    }

Hello this what i'm doing is that i'm receiving images from client and displaying it in the loop .. data is correctly received but only last received image is shown when loop finishes..

this is not the main controller this is in thread. I don't know whats wrong as it's receiving data and it should update on every iteration....

Here is code:

@FXML  ImageView currentframe;
public void run()
{
        System.out.println("hi controller");  
        System.out.println("creating socket");
        DatagramSocket serverSocket=null; 
        try {
            serverSocket = new  DatagramSocket(1001);
        } catch (SocketException ex) {
            Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("created socket");

        byte[] receiveData = new byte[50000]; 

        //byte[] sendData  = new byte[1024]; 
        //BufferedImage img = null;
        //Image image = null;

        int counter = 0;

        while(counter<20){
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
            try {  
                serverSocket.receive(receivePacket);
            } catch (IOException ex) {
                Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
            }
            //System.out.println("packetreceive");
            BufferedImage img = null;

            try {
                img = ImageIO.read(new ByteArrayInputStream(receivePacket.getData()));
            } catch (IOException ex) {
                Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
            }

            //System.out.println("packetreceive1");
            System.out.println("received packet length "+receivePacket.getLength());

            Image image= SwingFXUtils.toFXImage(img, null); 

            Image tempImg = image;

            Platform.runLater(() -> currentframe.setImage(tempImg));


            /*
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(threadclass.class.getName()).log(Level.SEVERE, null, ex);
            }*/

            //currentframe.setImage(image);
            //threadclass t = new threadclass(currentframe, image);
            //t.start();

           //Platform.runLater(null);

            counter++;
            System.out.println("IMAGE SET ON FRAME...");
        }
        //System.out.println("ssdcd" );
        // currentframe.setImage(image);
        //System.out.println("last packet ");
}

here is how i'm starting thread in the controller:

@FXML
public void btn(ActionEvent event) throws IOException, InterruptedException
{
        threadclass t = new threadclass(currentframe);
        t.run();
    }

原文:https://stackoverflow.com/questions/47417877
更新时间:2022-10-28 16:10

最满意答案

在C ++中,输出流的precision属性的效果取决于流的floatfield格式标志的设置。 您的C ++示例将该标志设置为fixed ,这似乎与printf()%f段描述符相对应。 我说“似乎”因为我找不到任何需要完全对应的文档,但我预计在实践中它们确实完全一致。

在C printf()格式中,precision是每个输出字段的属性。 它在小数点和转换说明符(此处为f )之间的字段描述符中表示,可能在两者之间有长度修饰符。 假设xdoublefloat的数组,那么C ++语句的C模拟也是如此

printf("%.2f\n", x[i]);

要么

fprintf(stdout, "%.2f\n", x[i]);

(这些是等价的)。 这些与C ++不同,因为浮点格式和精度不会保留为stdout持久属性。


In C++, the effect of the precision property of an output stream depends on the stream's setting of the floatfield format flag. Your C++ example sets that flag to fixed, which seems to correspond to the %f field descriptor in printf(). I say "seems to" because I'm having trouble finding any documentation that requires exact correspondence, but I would anticipate that in practice they do correspond exactly.

In a C printf() format, the precision is a property of each output field. It is expressed in the field descriptor between a decimal point and the conversion specifier (f here), possibly with a length modifier between. Supposing that x is an array of either double or float, the C analog of your C++ statement is therefore either

printf("%.2f\n", x[i]);

or

fprintf(stdout, "%.2f\n", x[i]);

(these are equivalent). These differ from the C++, however, in that the floating-point format and precision are not retained as persistent properties of stdout.

相关问答

更多
  • 如果明确地将 double为int ,则小数部分将被截断。 例如: int x = (int) 4.97542; //gives 4 only int x = (int) 4.23544; //gives 4 only 此外,如果您希望返回double值,您也可以使用Math.floor()方法来舍入值。 If you explicitly cast double to int, the decimal part will be truncated. For example: int x = (i ...
  • 恐怕我不知道如何保证一个正确的圆形双精度立方体根,但可以提供一个几乎正确圆整的问题。 换句话说,最大误差似乎非常接近0.5 ulp。 Peter Markstein,“IA-64和基本功能:速度和精度”(Prentice-Hall,2000) 提出了高效的基于FMA的技术,用于对倒数,平方根和倒数平方根进行正确舍入,但不包括这方面的立方根。 一般来说,Markstein的方法需要一个初步结果,在最终舍入序列之前精确到1 ulp以内。 我没有数学能力来将他的技术扩展到立方体根部的四舍五入,但在我看来原则上这应 ...
  • 首先 - 不要将货币保持在double使用中,而不是使用decimal 。 每次。 然后使用“C0”作为格式说明符: decimal numba = 5212.6312M; string s = numba.ToString("C0"); First - don't keep currency in a double - use a decimal instead. Every time. Then use "C0" as the format specifier: decimal numba = 521 ...
  • 我建议为了简单起见,你将它分为三部分: int wholeNumber; //for getting the left part of the number int decimalPartTwoPlaces; //for assigning the right part to two characters double pi = 3.14159265d; wholeNumber = Math.Round(pi); decimalPartTwoPlaces = Math.Round( ( pi - (dou ...
  • C ++中cout的默认精度为6位数,因此2001.3666将显示为2001.37,但201.3666应显示为201.367。 你可以像这样增加精度: #include ... cout << "you entered " << setprecision(10) << numberWithDecimalPlaces << endl; The default precision for cout in C++ is 6 digits, and so 2001.3666 will disp ...
  • 因为你所做的就是整数除法。 .NET有3种类型的划分。 从7.7.2分部运营商 整数除法 浮点除法 十进制除法 整数除法: 除法将结果舍入为零,结果的绝对值是最大可能的整数,小于两个操作数的商的绝对值。 你可以使用其中之一; double d = (39 * 10) / 100d double d = (39d * 10d) / 100d double d = (double)(39 * 10) / 100 Because what you done is here integer division. . ...
  • 在Fortran和C之间传递数组是一个非常重要的问题。 特定的C和Fortran编译器很重要。 我看到的第一个问题是你指定double来匹配real*4 。 这几乎在所有平台上都无效。 将C函数声明为: int test_func (float *a) 这可能适用于某些平台,尽管许多Fortran编译器传递“数组描述符”的地址而不是数组本身。 查看Fortran编译器的文档。 Passing arrays between Fortran and C is a non-trivial problem. Th ...
  • 编辑:这必须是GCC 4.7.2错误。 该代码在GCC 4.8.1中工作正常,但在4.7.2中失败。 此代码( ideone.com上的示例 )适用于GCC 4.8.1: #include #include int main() { double one_third = 1.0/3.0; std::cout.precision(4); std::fesetround(FE_DOWNWARD); std::cout << one_thi ...
  • 在C ++中,输出流的precision属性的效果取决于流的floatfield格式标志的设置。 您的C ++示例将该标志设置为fixed ,这似乎与printf()的%f段描述符相对应。 我说“似乎”因为我找不到任何需要完全对应的文档,但我预计在实践中它们确实完全一致。 在C printf()格式中,precision是每个输出字段的属性。 它在小数点和转换说明符(此处为f )之间的字段描述符中表示,可能在两者之间有长度修饰符。 假设x是double或float的数组,那么C ++语句的C模拟也是如此 p ...
  • 你可以组合形状来得到这个。通过将圆角矩形与普通矩形相结合,你可以制作一个没有底部两个圆角的矩形。 例如 public class example extends JFrame{ public example() { this.setUndecorated(true); this.getContentPane().setBackground(Color.red); Area shape1 = new Area(new RoundRectangle2D ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。