gpt4 book ai didi

java - 动态更新 BufferedImage 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 04:20:19 25 4
gpt4 key购买 nike

我正在尝试在 JPanel 上渲染动态变化的图像。我尝试了很多事情,但我根本不知道该怎么做,也不知道我做错了什么,我快要疯了。我是 Swing 新手,这是我的程序中很小但很重要的一部分,它使我能够看到自己在做什么。

简单的要求是下面提到的代码中最初在 JPanel 中渲染的图片应该在每次更新时使用随机颜色进行更新。我无法更新图像。相反,我在尝试更新的图像中间出现了另一个正方形,并且该小正方形动态变化。我不知道我做错了什么。请让我知道如何纠正下面的代码,以便我可以更新整个图像。

谢谢。

代码如下

(MainTest.java)

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

public class MainTest extends JFrame {
static ImageEditor img ;
JPanel panel;
Timer to;

public MainTest()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(265, 329);
getContentPane().setLayout(null);
img = new ImageEditor();
panel = new ImageEditor();
panel.setBounds(10, 11, 152, 151);
panel.add(img);
getContentPane().add(panel);

JButton btnIterate = new JButton("Iterate");
btnIterate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {


ThirdClass t = new ThirdClass(img);
to = new Timer(10,t);
to.start();
}

});
btnIterate.setBounds(10, 230, 89, 23);
getContentPane().add(btnIterate);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
MainTest frame = new MainTest();
//frame.getContentPane().add(imgx);
frame.setVisible(true);
}
});

}
}

(ImageEditor.java)
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

@SuppressWarnings("serial")
public class ImageEditor extends JPanel {

private static BufferedImage img = new BufferedImage(100, 100, 1);
public ImageEditor() {
render();
}

public void paintComponent(Graphics g) {
if (img == null)
super.paintComponents(g);
else
g.drawImage(img, 0, 0, this);
}


public void render() {

float cellWidth = 10;
float cellHeight = 10;

int imgW = img.getWidth();
int imgH = img.getHeight();
float r, g, b;
Graphics2D g2 = img.createGraphics();
g2.setBackground(Color.black);
g2.clearRect(0,0,imgW,imgH);
for (int x=0; x<100; x++) {
for (int y=0; y<100; y++) {
r = (float)Math.random();
g = (float)Math.random();
b = (float)Math.random();
g2.setColor(new Color(r,g,b));
g2.fillRect((int)(x*cellWidth), (int)(y*cellHeight),
(int)cellWidth+1, (int)cellHeight+1);
}
}
g2.setColor(Color.black);
g2.dispose();
repaint();
}

public BufferedImage getImage() {
if (img == null)
img = (BufferedImage)createImage(100, 100);

return img;
}

public void setImage(BufferedImage bimg) {
img = bimg;
}
}


(ThirdClass.java)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ThirdClass implements ActionListener {

static ImageEditor mock;

public ThirdClass(ImageEditor img)
{
mock = img;
Train();
}

public void Train()
{
mock.render();
}

@Override
public void actionPerformed(ActionEvent arg0) {
Train();
}

}

最佳答案

从代码中:

img =  new ImageEditor();
panel = new ImageEditor();
panel.setBounds(10, 11, 152, 151);
panel.add(img);
getContentPane().add(panel);

您将 img 定义为 ImageEditor,但您可能希望将其定义为 BufferedImage。然后,您将其添加到 panel,这是另一个 ImageEditor - 但它是通过父类(super class)方法 JPanel.add 添加的,当您可能意味着使用您编写的方法,ImageEditor.setImage()

编辑:总结一下:

您所描述的更新像素的小块是 BufferedImage (img.img),位于您的 ImageEditor img 中,位于转到ImageEditor 面板 内部,该面板本身位于小程序的内容 Pane 内。

  • MainTest 类中的 static ImageEditor img 替换为 BufferedImage img

  • MainTest 的无参数构造函数中的 img = new ImageEditor 替换为 img = new BufferedImage()

  • MainTest 的无参数构造函数中的 panel.add(img) 替换为 panel.setImage(img)

  • ThirdClass 类中的 static ImageEditor mock 替换为 BufferedImage mock

  • ThirdClass 构造函数中的 ImageEditor img 参数替换为 BufferedImage img

  • 替换涉及 img 的任何其他内容,以将其正确处理为 BufferedImage

另一件事,虽然这个问题本身并不是任何程序故障的原因,但您应该将 ThirdClass 中的 Train() 方法重命名为类似 train() 因此它符合正确的 java 风格。

关于java - 动态更新 BufferedImage 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17199879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com