彩色像素线
在组件中,我必须显示几行彩色像素。线条的每个像素都被赋予一种颜色。什么样的元件适合构建线路或者什么样的元件适合容纳像素?
In a component I must display several lines of colored pixels. Each pixel of the line is given a color. What kind of component is suitable to build the line or what kind of component is suitable to hold pixels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需扩展
JComponent
并在paintComponent()
方法中绘制线条/像素即可。Just extend
JComponent
and paint the lines/pixels in thepaintComponent()
method.对我来说,最好的(但可能不是最简单的)方法是实现一个自定义的 Paint 类,它允许设置颜色区域 - 有点像 GradientPaint 类,但更灵活。
然后,您将在绘制线条之前调用
Graphics2D.setPaint(myPaint)
。Paint
实现可以提供一个带有start
和end
的方法setColorForRegion(double start, double end, Color color)
取 0.0 到 1.0 之间的值来标记线上的区域。实现 Paint 类可能有点复杂,但好处是,您可以调整线条大小并在任何方向上绘制它们,同时保留颜色图案。
To me the best (but maybe not easiest) way would be implementing a custom
Paint
class that allows for setting colour regions - a bit like theGradientPaint
classes but more flexible.Then you would call
Graphics2D.setPaint(myPaint)
just before you draw the line.The
Paint
implementation could offer a methodsetColorForRegion(double start, double end, Color color)
withstart
andend
taking values between 0.0 and 1.0 to mark a region on the line.Might be a bit complicated to implement the
Paint
class, but the benefit is, that you can resize the lines and draw them in any direction while preserving the color pattern.