添加的 BufferedImage 只有 2x2 像素
我有一个脚本,它使用线程(animationThread)循环(几乎像幻灯片一样)矢量对象(flipBook),并将它们添加到 JPanel 中。然而,添加的图像只有 2x2 像素大。 我已验证图像的尺寸为 50x50,但它们似乎无法正确显示。
以下是 Thread 实例背后的一些代码。我不完全确定哪些代码有利于查找源代码。
public void startThread() {
if (flipWidth != 0 && flipHeight != 0) {
System.out.println("[ AnimationAsset ] " + "We're starting the thread");
Runnable r = new Runnable() {
@Override
public void run() {
runWork();
}
};
animationThread = new Thread(r, "AnimationThread");
animationThread.start();
going = true;
}
}
private void runWork() {
try {
while (going) {
repaint();
flipIndex = (flipIndex + 1) % numFlips;
System.out.println("[ AnimationAsset ] flipIndex: " + flipIndex);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("[ AnimationAsset ] " + "Interrupted");
}
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
System.out.println("[ AnimationAsset ] " + flipIndex);
((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);
}
I have a script that cycles (almost like a slideshow) through a Vector object (flipBook), using a Thread (animationThread), and adds them to a JPanel. However, the added image is only 2x2 pixels large.
I've verified the images are 50x50, but they don't appear to be properly showing.
Here's some of the code going on behind the Thread instance. I'm not entirely sure which code would be beneficial for finding the source for.
public void startThread() {
if (flipWidth != 0 && flipHeight != 0) {
System.out.println("[ AnimationAsset ] " + "We're starting the thread");
Runnable r = new Runnable() {
@Override
public void run() {
runWork();
}
};
animationThread = new Thread(r, "AnimationThread");
animationThread.start();
going = true;
}
}
private void runWork() {
try {
while (going) {
repaint();
flipIndex = (flipIndex + 1) % numFlips;
System.out.println("[ AnimationAsset ] flipIndex: " + flipIndex);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("[ AnimationAsset ] " + "Interrupted");
}
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
System.out.println("[ AnimationAsset ] " + flipIndex);
((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 Swing 代码。这是 AWT 代码。
使用 Swing 时,永远不会以这种方式重写 update() 和 Paint() 方法。删除此代码并重新开始。
为了在 Swing 中执行此操作,我将使用带有图标的 JLabel 并将标签添加到框架中。
然后,要在 SWing 中制作动画,您应该使用 Swing Timer 。
当计时器触发时,您只需使用标签的 setIcon(...) 方法即可用新图标替换旧图标。
This is not a Swing code. This is AWT code.
You would never override the update() and paint() methods this way when using Swing. Get rid of this code and start over.
To do this in Swing I would use a JLabel with an Icon and add the label to the frame.
Then, to do animation in SWing your should use a Swing Timer.
When the timer fires you simply use the setIcon(...) method of the label to replace the old icon with your new icon.
嗯,你有一个奇怪的绘图代码,
你可以看到
.BufferedImage,%20java.awt.image.BufferedImageOp,%20int,%20int%29" rel="nofollow">此处的文档 最常见的图像绘制方法是在 JComponent(通常是 JLabel)上绘制图像。这是一个组件示例
,因此您可以使用该组件作为常见的摆动对象
祝你好运
Emm you have a strange drawing code as
You can see the docs here... to use Graphic2D drawImage() method right
the most common way of image drawing is to paint image right on JComponent, as a rule, the JLabel. Here is a component example
so here you can use the component as a common swing object
Good luck