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
最满意答案
在C ++中,输出流的
precision
属性的效果取决于流的floatfield
格式标志的设置。 您的C ++示例将该标志设置为fixed
,这似乎与printf()
的%f
段描述符相对应。 我说“似乎”因为我找不到任何需要完全对应的文档,但我预计在实践中它们确实完全一致。在C
printf()
格式中,precision是每个输出字段的属性。 它在小数点和转换说明符(此处为f
)之间的字段描述符中表示,可能在两者之间有长度修饰符。 假设x
是double
或float
的数组,那么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 thefloatfield
format flag. Your C++ example sets that flag tofixed
, which seems to correspond to the%f
field descriptor inprintf()
. 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 thatx
is an array of eitherdouble
orfloat
, the C analog of your C++ statement is therefore eitherprintf("%.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 ...
-
计算正确舍入的/几乎正确舍入的浮点立方根(Computing a correctly rounded / an almost correctly rounded floating-point cubic root)[2023-05-27]
恐怕我不知道如何保证一个正确的圆形双精度立方体根,但可以提供一个几乎正确圆整的问题。 换句话说,最大误差似乎非常接近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.结束圆形双(C. Conclusion of the rounded double)[2023-12-03]
在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 ...