我想根据从另一个线程收到的数据在我的面板上绘制一个图像.我确信数据和随后的像素数组运行良好,但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 …Run Code Online (Sandbox Code Playgroud) 我们有一个遗留的要求,即将现在新迁移的intID值存储到一个guid类型中,以便在ID不可知的数据类型上使用(基本上是利用"全局唯一"部分的旧代码,guid以便在一列中包含所有可能的ID /领域).
由于这个要求,有一个后续要求guid,以人类可读的方式嵌入entites的整数ID .这很重要,目前正在阻止我直接处理字节值.
目前,我有以下内容:
public static byte[] IntAsHexBytes(int value)
{
return BitConverter.GetBytes(Convert.ToInt64(value.ToString(), 16));
}
public static Guid EmbedInteger(int id)
{
var ib = IntAsHexBytes(id);
return new Guid(new byte[]
{
0,0,0,0,0,0,1,64,ib[7],ib[6],ib[5],ib[4],ib[3],ib[2],ib[1],ib[0]
});
}
Run Code Online (Sandbox Code Playgroud)
它把的视觉表示int为十六进制值(value.ToString()),转换其为long(Convert.ToInt64(value.ToString(), 16))和抓斗从字节long展平为byte[]用于创建guid在一个特定的结构.
所以给予int的42,当你把42作为一个十六进制和转换是到long你66,和上的66字节给出,将成为guid给:
"00000000-0000-4001-0000-000000000042"
Run Code Online (Sandbox Code Playgroud)
和int中379932126得出:
"00000000-0000-4001-0000-000379932126"
Run Code Online (Sandbox Code Playgroud)
因此最终结果是将整数放入guid最后12位数中,因此它在视觉上看起来像整数42(即使基础整数值为66). …
如果这是一个简单的问题,请原谅我; 我不能用通用的方式来表达它来搜索答案.
考虑以下代码:
var task = Task.Factory.StartNew(() => Whatever());
task.ContinueWith(Callback, TaskScheduler.FromCurrentSynchronizationContext())
Run Code Online (Sandbox Code Playgroud)
回调方法执行时究竟如何确定?
它会等到主线程当前完成正在做的事情,还是会在异步调用完成后立即调用?并且这个回调是否会在主线程返回之前执行的任何操作之前完全执行?
我有一个类构造函数,它接受一个IList<IElement>参数.
在创建类的新实例时,我能够传递一个IElement[]而不是IList<IElement>那个有意义的东西?
我正在开发一个Silverlight应用程序,我过度使用了观察者模式.在我的实现中,我创建了两个接口IObservable<T>和IObserver<T>.前者包含将观察者附加到观察者的方法.后者有一个方法Notify(IObservable<T> observable, ...),observer.Notify(this, ...)当observable改变了状态时,observable 将自身作为参数传递.
现在我偶然发现了"事件",对我来说,似乎这就是观察者模式的一个实现,只是代表而不是前面提到的Notify方法.是对的吗?
我不太了解代表,并且不想花费数小时来重写我的代码只是为了最终得到与它已经做同样事情的代码.另一方面,事件可能优于基于接口的观察者模式.我错过了什么吗?
我有一个简单的接口,两个类实现它:
public interface IMovable { }
public class Human : IMovable { }
public class Animal : IMovable { }
Run Code Online (Sandbox Code Playgroud)
以下泛型方法导致编译时错误: Cannot convert type 'Human' to 'T'
public static T DoSomething<T>(string typeCode) where T : class, IMovable
{
if (typeCode == "HUM")
{
return (T)new Human(); // Explicit cast
}
else if (typeCode == "ANI")
{
return (T)new Animal(); // Explicit cast
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当使用as关键字时,一切都很好:
public static T DoSomething<T>(string typeCode) where T : …Run Code Online (Sandbox Code Playgroud) 在研究另一个问题时:" 为什么没有Nullable <T> .Equals(T值)方法? "我做了一个扩展方法Nullable<T>,暴露了一个通用的Equals<T>(T)方法:
public static class NullableExtensions
{
public static bool Equals<T>(this T? left, T right) where T : struct, IEquatable<T>
{
if (!left.HasValue)
return false;
return right.Equals(left.Value);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这样称呼它:
double? d = null;
d.Equals(0.0);
Run Code Online (Sandbox Code Playgroud)
Equals(object)用拳击调用基地,正如你在IL中看到的那样:
IL_0000: nop
IL_0001: ldloca.s d
IL_0003: initobj valuetype [mscorlib]System.Nullable`1<float64>
IL_0009: ldloca.s d
IL_000b: ldc.r8 0.0
IL_0014: box [mscorlib]System.Double
IL_0019: constrained. valuetype [mscorlib]System.Nullable`1<float64>
IL_001f: callvirt instance bool [mscorlib]System.Object::Equals(object)
Run Code Online (Sandbox Code Playgroud)
如果我更改调用以显式声明泛型类型:
d.Equals<double>(0.0);
Run Code Online (Sandbox Code Playgroud)
它调用我的扩展方法:
IL_0000: nop
IL_0001: ldloca.s d
IL_0003: …Run Code Online (Sandbox Code Playgroud) StringBuilder.Append()通过使用char[]分配在线程的堆栈来使用指针按字符构建字符串,可以优化广泛的操作吗?
unsafe
{
const Int32 width = 1024;
Char* msg = stackalloc Char[width];
Int32 index = 0;
property = Environment.MachineName;
for (Int32 i = 0; i < property.Length; i++)
msg[index++] = property[i];
return new String(msg, 0, width);
}
Run Code Online (Sandbox Code Playgroud)
与使用相比,这提供了约25%的改善,StringBuilder并且因此不太令人满意.
在C++中,我们可以使用宏或constexpr(如C++ 11所述).我们在C#中可以做些什么?
有关上下文,请参阅"无法声明..."注释:
static class Constant
{
// we must ensure this is compile time const, have to calculate it from ground...
public const int SIZEOF_TEXUTRE_RGBA_U8C4_640x480 = 4 * sizeof(byte) * 640 * 480;
// Cannot declare compile time constant as following in C#
//public const int SIZEOF_TEXUTRE_RGBA_U8C4_640x480_2 = 4 * PixelType._8UC4.PixelSize() * 640 * 480;
}
public static class PixelTypeMethods
{
public static /*constexpr*/ int PixelSize(this PixelType type)
{
int value = (int)type;
int unit_size = value & 0xFF; …Run Code Online (Sandbox Code Playgroud)