添加的 BufferedImage 只有 2x2 像素

发布于 2024-11-28 09:56:58 字数 1269 浏览 1 评论 0原文

我有一个脚本,它使用线程(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吾性傲以野 2024-12-05 09:56:58

并将它们添加到 JPanel

这不是 Swing 代码。这是 AWT 代码。

使用 Swing 时,永远不会以这种方式重写 update() 和 Paint() 方法。删除此代码并重新开始。

为了在 Swing 中执行此操作,我将使用带有图标的 JLabel 并将标签添加到框架中。

然后,要在 SWing 中制作动画,您应该使用 Swing Timer

当计时器触发时,您只需使用标签的 setIcon(...) 方法即可用新图标替换旧图标。

and adds them to a JPanel

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.

最舍不得你 2024-12-05 09:56:58

嗯,你有一个奇怪的绘图代码,

((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);

你可以看到

.BufferedImage,%20java.awt.image.BufferedImageOp,%20int,%20int%29" rel="nofollow">此处的文档 最常见的图像绘制方法是在 JComponent(通常是 JLabel)上绘制图像。这是一个组件示例

public class MyLabel extends JLabel
{

private Image image;

public MyLabel(Image image)
{

this.image=image;

}

public void paintComponent(Graphics g)
{

  g.drawImage(this.image,x,y,width,height,null);

}

}

,因此您可以使用该组件作为常见的摆动对象

public class MyPanel extends JPanel
{
 public MyPanel()
{
  Image image=null;
 try{
  image=ImageIO.read(new File("image.png"));
  }
 catch (IOException e) {
 }

  this.add(new MyLabel(image));

}

}

祝你好运

Emm you have a strange drawing code as

((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);

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

public class MyLabel extends JLabel
{

private Image image;

public MyLabel(Image image)
{

this.image=image;

}

public void paintComponent(Graphics g)
{

  g.drawImage(this.image,x,y,width,height,null);

}

}

so here you can use the component as a common swing object

public class MyPanel extends JPanel
{
 public MyPanel()
{
  Image image=null;
 try{
  image=ImageIO.read(new File("image.png"));
  }
 catch (IOException e) {
 }

  this.add(new MyLabel(image));

}

}

Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文