我是Java编程的新手,但是我已经用其他语言编写过代码。我遇到一个问题,无法调用包含某些绘图说明的def rolling_window(array, window_size):
itemsize = array.itemsize
# broadcast window_size so it will still work with a scalar:
window_size = np.broadcast_to(window_size,[2,])
shape = (array.shape[0] - window_size[0] + 1,
array.shape[1] - window_size[1] + 1,
window_size[0], window_size[1])
# the following would also work:
# shape = (*np.array(array.shape) - window_size + 1, *window_size)
strides = (array.shape[1] * itemsize, itemsize,
array.shape[1] * itemsize, itemsize)
return np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)
roller = np.arange(1,16).reshape(3,5)
>>> print(roller)
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]]
run = rolling_window(roller,[2,3])
>>> run
array([[[[ 1, 2, 3],
[ 6, 7, 8]],
[[ 2, 3, 4],
[ 7, 8, 9]],
[[ 3, 4, 5],
[ 8, 9, 10]]],
[[[ 6, 7, 8],
[11, 12, 13]],
[[ 7, 8, 9],
[12, 13, 14]],
[[ 8, 9, 10],
[13, 14, 15]]]])
方法。我希望能够在计时器函数中调用它。代码如下:
Paint()
任何帮助将不胜感激。另外,您可以提供的任何提示也会有所帮助。感谢您可以提前提供的帮助。
答案 0 :(得分:1)
通过覆盖paintComponent(...)
而不是paint(...)来完成自定义绘制。您还需要调用super.paintComponent(g)
作为第一个语句,以确保绘制背景。
Java区分大小写,因此您需要确保覆盖适当的方法。重写方法之前,应始终在行上使用@Override
。如果您输入错误,编译器会告诉您。
您应该使用Swing Timer
进行动画制作。 Swing组件的更新应在事件调度线程(EDT)上完成。 Swing计时器将自动在EDT上执行代码。
不要在计时器中使用while (true)
循环。使用计时器的目的是使计时器成为循环。您只需在每次计时器触发时执行代码。
在计时器的ActionListener
中,您可以更改变量的值以提供动画,然后调用repaint()
,这将使您的面板重新粉刷。
变量名称不应以大写字母开头。注意论坛如何突出显示您的变量名,因为它认为它们是类名。这很混乱。学习Java约定并遵循它们。
阅读Swing Tutorial,了解Swing的基础知识。关于以下内容:a)Concurrency in Swing
b)How to Use Swing Timers
c)Custom Painting
。